利用Aspose.Words打印word文档

Aspose.Words是一个强大的文档处理类库,可在无Microsoft Word环境下生成、修改和打印DOC等格式的文件。本文以一个简单的例子介绍如何通过Aspose.Words在MVC框架中制作word模板,插入域并实现打印。在模板中插入MergeField,然后在控制器中赋值并调用API完成打印。推荐查阅Aspose.Words API文档以了解更多实现方式。
摘要由CSDN通过智能技术生成

          Aspose.Words是一款先进的类库,可以直接在各个应用程序中执行各种文档处理,即使在没有Microsoft word

的情况下,仍然可以生成,更改,转换,渲染和打印word文档,并且支持DOC,PDF等其他格式。

         下面就以  Aspose.Words如何实现打印word文档为例做一个小demo。

1·制作word模板

      在word模板中,在需要插入的地方选择插入--文档部件--域--域名是MergeField--域名是字段名,例如name,点击确定即可。


效果图如下:


2 实现代码:

        由于我是加入MVC框架的,所以在controller中接收的是来自view的实体,不用再经过数据库查找相应的数据,如果在纯三层或几层中实现,直接调用D层,通过D层访问数据库获得即可。

        简单一句话来说,就是一个赋参数的一个过程,然后直接调用该类库下的方法即可。方法有很多,具体的可以参考Aspose.Word的API文档。

      

#region 初婚未育调查报告 +
首先机子上安装有office,在COM中添加引用Microsoft.Word.11.0.Object.Library(或11.0以上) Microsoft.Office.Interop.Word.Application myWordApp = null; Microsoft.Office.Interop.Word.Document doc = null; object Filename = path + "\\" + TaskID + ".docx";//目的文件 object templateFile = System.Windows.Forms.Application.StartupPath + @"\Template.docx";//模板文件,有一个五列一行的表 System.IO.File.Copy(templateFile.ToString(), Filename.ToString(), true);//模板WORD中有一个五列的表头,分别是卡号,串口号,发送指令条数,接收指令条数,收发成功率 myWordApp = new Microsoft.Office.Interop.Word.Application(); doc = myWordApp.Documents.Open(ref Filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); /////显示页码 object oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter; object oFirstPage = true; oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter; myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(ref oAlignment, ref oFirstPage); myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.NumberStyle = Microsoft.Office.Interop.Word.WdPageNumberStyle.wdPageNumberStyleNumberInDash; for (int i = 2; i < 102; i++)//举例100台 { doc.Tables[1].Rows.Add(ref Nothing);//表格增加一行 doc.Tables[1].Cell(i, 1).Range.Text = "250297";//卡号 doc.Tables[1].Cell(i, 2).Range.Text = "COM12";//串口号 doc.Tables[1].Cell(i, 3).Range.Text = "100";//发送指令条数 doc.Tables[1].Cell(i, 4).Range.Text = "99";//接收指令条数 doc.Tables[1].Cell(i, 5).Range.Text = "99%";//收发成功率 } doc.SaveAs(ref Filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); object savechanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;//不保存挂起的更改 ////下面是直接打印文档不显示 //doc.PrintOut(ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); object readOnly=false; object isVisable = true;////文档打开状态为可视 doc = myWordApp.Documents.Open(ref Filename, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisable, ref Nothing, ref Nothing, ref Nothing, ref Nothing); doc.PrintPreview();//打印预览
Aspose.Words 是一个功能强大的 Java 文档处理库,允许开发者在 Java 应用程序中创建、编辑、转换和渲染 Microsoft Word 文档。使用 Aspose.WordsWord 文档末尾添加文本,可以通过以下步骤实现: 1. 首先,需要在项目中引入 Aspose.Words for Java 的库依赖。 2. 加载已存在的 Word 文档。 3. 找到文档的末尾位置,可以通过遍历节来找到最后一个段落,或者直接使用文档末尾的节点。 4. 创建一个新的 Paragraph 节点,并添加到文档的最后。 5. 在新建的 Paragraph 节点中添加所需的文本内容。 6. 最后,保存或更新文档。 以下是一个简单的示例代码,演示如何使用 Aspose.Words for Java 在 Word 文档末尾添加文本: ```java import com.aspose.words.Document; import com.aspose.words.DocumentBuilder; import com.aspose.words.NodeType; public class AddTextToWord { public static void main(String[] args) { // 加载已存在的 Word 文档 Document doc = new Document("example.docx"); // 创建 DocumentBuilder 实例,它可以方便地构建文档内容 DocumentBuilder builder = new DocumentBuilder(doc); // 将文档构建器移动到文档的末尾 builder.moveToDocumentEnd(); // 在文档末尾添加一个新段落 builder.insertParagraph(); // 在新段落中添加文本 builder.write("这是在文档末尾添加的文本。"); // 保存文档 doc.save("updated-example.docx"); } } ``` 在上面的代码中,首先加载了名为 "example.docx" 的文档,然后使用 DocumentBuilder 在文档的末尾插入了一个新段落,并在其中添加了文本内容。最后,将更新后的文档保存为 "updated-example.docx"。
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值