.Net使用Aspose.Words生成Word文档

一.前言

  想要按照模板来将数据导出到Word文档,可以使用插件Aspose.Words。这个插件可以用于Word文档的生成、修改,而不需要在环境中安装Word软件。

二.安装

  安装该插件包。在新版本中,需要在启动程序中执行注册码,否则生成的Word文档会有水印。

new Aspose.Words.License().SetLicense(new MemoryStream(Convert.FromBase64String("PExpY2Vuc2U+CiAgPERhdGE+CiAgICA8TGljZW5zZWRUbz5TdXpob3UgQXVuYm94IFNvZnR3YXJlIENvLiwgTHRkLjwvTGljZW5zZWRUbz4KICAgIDxFbWFpbFRvPnNhbGVzQGF1bnRlYy5jb208L0VtYWlsVG8+CiAgICA8TGljZW5zZVR5cGU+RGV2ZWxvcGVyIE9FTTwvTGljZW5zZVR5cGU+CiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1pdGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+CiAgICA8T3JkZXJJRD4yMDA2MDIwMTI2MzM8L09yZGVySUQ+CiAgICA8VXNlcklEPjEzNDk3NjAwNjwvVXNlcklEPgogICAgPE9FTT5UaGlzIGlzIGEgcmVkaXN0cmlidXRhYmxlIGxpY2Vuc2U8L09FTT4KICAgIDxQcm9kdWN0cz4KICAgICAgPFByb2R1Y3Q+QXNwb3NlLlRvdGFsIGZvciAuTkVUPC9Qcm9kdWN0PgogICAgPC9Qcm9kdWN0cz4KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl0aW9uVHlwZT4KICAgIDxTZXJpYWxOdW1iZXI+OTM2ZTVmZDEtODY2Mi00YWJmLTk1YmQtYzhkYzBmNTNhZmE2PC9TZXJpYWxOdW1iZXI+CiAgICA8U3Vic2NyaXB0aW9uRXhwaXJ5PjIwMjEwODI3PC9TdWJzY3JpcHRpb25FeHBpcnk+CiAgICA8TGljZW5zZVZlcnNpb24+My4wPC9MaWNlbnNlVmVyc2lvbj4KICAgIDxMaWNlbnNlSW5zdHJ1Y3Rpb25zPmh0dHBzOi8vcHVyY2hhc2UuYXNwb3NlLmNvbS9wb2xpY2llcy91c2UtbGljZW5zZTwvTGljZW5zZUluc3RydWN0aW9ucz4KICA8L0RhdGE+CiAgPFNpZ25hdHVyZT5wSkpjQndRdnYxV1NxZ1kyOHFJYUFKSysvTFFVWWRrQ2x5THE2RUNLU0xDQ3dMNkEwMkJFTnh5L3JzQ1V3UExXbjV2bTl0TDRQRXE1aFAzY2s0WnhEejFiK1JIWTBuQkh1SEhBY01TL1BSeEJES0NGbWg1QVFZRTlrT0FxSzM5NVBSWmJRSGowOUNGTElVUzBMdnRmVkp5cUhjblJvU3dPQnVqT1oyeDc4WFE9PC9TaWduYXR1cmU+CjwvTGljZW5zZT4=")));

 三.使用

  Document对象实例化时传入模板路径参数,得到该模板文件的对象doc。使用Save方法,可以将其保存到另外一个路径下。

string filePath = Server.MapPath("~/模板xxx.doc");//路径
string savefile = Server.MapPath("~/生成xxx.doc");
Document doc = new Document(filePath); //载入模板
//......在这里对doc进行操作后,最后才生成work文档。操作如下列举
doc.Save(savefile);

  可以通过Word文档标签,在模板上对应位置进行赋值。(Word文档怎么使用书签,可以上网查找资料)

//判断有无书签
if (doc.Range.Bookmarks.Count > 0)
{
    //赋值时,书签一定需要存在,否则报错
    doc.Range.Bookmarks["xxx书签"].Text = "xxx";
}

  在Word文档中生成table表单。

//创建builder对象,在doc上。
DocumentBuilder builder = new DocumentBuilder(doc);
//移动到doc上对应的书签位置
builder.MoveToBookmark("xxx");
//样式
builder.Font.Bold = true;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
//写标题
builder.Writeln("xxx");

//默认就是第一行,从左往右创建一个单元格
CreateCell(builder, "xx");//方法在下面
CreateCell(builder, "xx");
CreateCell(builder, "xx");
//结束这一行。如果下面创建单元格,默认开启下一行
builder.EndRow();

//重复插入单元格,结束一行的操作,就可以实现table表格。
public static void CreateCell(DocumentBuilder builder, string content)
{
    //插入单元格,直到下一个单元格插入或行结束,builder都是对该单元格进行操作
    builder.InsertCell();
    builder.CellFormat.Borders.LineStyle = LineStyle.Single;
    builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
    builder.CellFormat.Width = 10;
    builder.Font.Size = 10;
    builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.CellFormat.RightPadding = 0;
    builder.CellFormat.LeftPadding = 0;
    builder.CellFormat.HorizontalMerge = CellMerge.First;
    builder.CellFormat.VerticalMerge = CellMerge.None;
    builder.Write(content);
}

 

posted @ 2022-01-29 23:05  shine声  阅读(1116)  评论(1编辑  收藏  举报