首先在word文档中定义bookmark名称,例如我定义了EmployeeTable 和 SICTable。使用
builder.MoveToBookmark("EmployeeTable");定位到EmployeeTable Bookmark
public void GenerateDocument(string DocumentName, string TemplateID)
{
string AsposeLicense = Server.MapPath("~/AsposeLicense/");
Aspose.Words.License license = new License();
license.SetLicense(AsposeLicense + "Aspose.Words.lic");
string FileFlorder = Server.MapPath("~/WorkFolder/Template/");
string SaveFileFlorder = Server.MapPath("~/WorkFolder/GenerateDocument/");
string FileFlorderRoot = Server.MapPath("~/");
var doc = new Aspose.Words.Document(FileFlorder + DocumentName);
//doc.Document.
//doc.
DocumentBuilder builder = new DocumentBuilder(doc);
DataTable dtEmployee = DBHR.GetDocumentEmployee(TemplateID);
if (dtEmployee.Rows.Count > 0)
{
// DataTable dt = JsonConvert.DeserializeObject(EmployeeTable, typeof(DataTable)) as DataTable;
builder.MoveToBookmark("EmployeeTable");
builder.StartTable();
for (int z = 0; z < dtEmployee.Columns.Count; z++)
{
builder.InsertCell();
builder.Write(dtEmployee.Columns[z].ColumnName);
}
builder.EndRow();
for (int x = 0; x < dtEmployee.Rows.Count; x++)
{
for (int y = 0; y < dtEmployee.Columns.Count; y++)
{
builder.InsertCell();
builder.Write(dtEmployee.Rows[x][y].ToString());
}
builder.EndRow();
}
builder.EndTable();
}
Image image = Image.FromFile(FileFlorderRoot + "WorkFolder/Image/word.png"); //在word中显示的图标
builder.InsertOleObject(FileFlorderRoot + "WorkFolder/Image/abc.docx",false,true,image); 插入的附件
DataTable dtSIC = DBHR.SIC.GetDocumentTemplateSIC(TemplateID);
if (dtSIC.Rows.Count > 0)
{
//DataTable SICdt = JsonConvert.DeserializeObject(SICTable, typeof(DataTable)) as DataTable;
builder.MoveToBookmark("SICTable");
builder.StartTable();
for (int z = 0; z < dtSIC.Columns.Count; z++)
{
builder.InsertCell();
builder.Write(dtSIC.Columns[z].ColumnName);
}
builder.EndRow();
for (int x = 0; x < dtSIC.Rows.Count; x++)
{
for (int y = 0; y < dtSIC.Columns.Count; y++)
{
builder.InsertCell();
builder.Write(dtSIC.Rows[x][y].ToString());
}
builder.EndRow();
}
builder.EndTable();
}
doc.Save(SaveFileFlorder + DocumentName+"output.PDF", Aspose.Words.SaveFormat.Pdf);
}