首页 > 代码库 > ASP.NET 利用第三方插件DocX产生Word 之 打印批量打印

ASP.NET 利用第三方插件DocX产生Word 之 打印批量打印

客户需求:

  接上一篇需求说起~~~

  上一篇说客户需要固定格式打印(定义模板),实现了之后,客户又闲一张一张打印有些麻烦,要进行批量打印。

  客户的需求就是这样,满足了一个又一个~~~没办法,做呗!毕竟客户给钱还是很痛快的!

解决办法:

  还是利用DocX插件进行处理。

  DocX是一个以非常直观简单的方式操作Word 2007/2010文件的轻量级.NET组件。它的速度非常快,而且不需要安装微软的Office软件。

  附上DocX插件官网:https://docx.codeplex.com/

  废话不说,看demo吧!

Demo程式代码:

  demo界面:

  技术分享

 

  ExportWord()  

技术分享
 1 private void ExportWord()
 2     { 
 3         //模板路径
 4         string mWordTemplateSrc = http://www.mamicode.com/Request.PhysicalPath.Replace("ExportWordAPI.aspx", "ExportWordTemplate.docx");
 5          
 6         MemoryStream stream = new MemoryStream();
 7         using (DocX docPrint = DocX.Create(stream, DocumentTypes.Template))
 8         {
 9             DocX doc;
10             string mIds = Request.QueryString["SheetNo"];
11             string[] mSheets = mIds.Split(|);
12             //int iLength = 0;
13             int iPrintCount = 0;
14             int mParagraphsIndex = 0;
15             foreach (string sheetNo in mSheets)
16             {
17                 //iLength++;
18                 if (string.IsNullOrEmpty(sheetNo.Trim())) continue;
19 
20                 iPrintCount++;
21 
22                 //加载模板
23                 doc = DocX.Load(mWordTemplateSrc);
24                 //输出打印内容到模板
25                 ExportWord(ref doc, "FYBX", sheetNo);
26 
27                 if (iPrintCount > 1)
28                 {
29                     mParagraphsIndex = docPrint.Paragraphs.Count - 1;
30                     docPrint.Paragraphs[mParagraphsIndex].InsertPageBreakAfterSelf();
31                     mParagraphsIndex = docPrint.Paragraphs.Count - 1;
32                     docPrint.Paragraphs[mParagraphsIndex].SetLineSpacing(LineSpacingType.Line, 0);
33                     docPrint.Paragraphs[mParagraphsIndex].SetLineSpacing(LineSpacingType.Before, 0);
34                     docPrint.Paragraphs[mParagraphsIndex].SetLineSpacing(LineSpacingType.After, 0);
35                 }
36                 //将打印内容插入打印word中
37                 docPrint.InsertDocument(doc, true);
38             }
39 
40 
41             //由于打印,产生的数据是不能修改的
42             EditRestrictions erReadOnly = EditRestrictions.readOnly;
43             docPrint.AddProtection(erReadOnly, "FYBX"); 
44 
45             docPrint.Save();
46 
47         } 
48 
49         byte[] tAryByte = stream.ToArray();
50         Response.Clear();
51         Response.AddHeader("Content-Length", tAryByte.Length.ToString());
52         Response.AppendHeader("Content-Disposition", string.Format("attachment;filename=ExportWord_{0}.docx", DateTime.Now.ToString("yyMMddHHmmss")));
53         Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
54         Response.BinaryWrite(tAryByte);
55         Response.Flush();
56         Response.End();
57 
58     }
View Code

  ExportWord(ref DocX doc, string pFormID, string pSheetNO)

技术分享
 1 private void ExportWord(ref DocX doc, string pFormID, string pSheetNO)
 2     {
 3         IDictionary par = new Dictionary<string, object>();
 4         string sql = "SELECT * FROM  head WHERE head001=@head001 AND head002=@head002  ";
 5         par.Clear();
 6         par.Add("head001", pFormID);
 7         par.Add("head002", pSheetNO);
 8         DataTable dtHead = SqlDBHelper.ExecuteDataTable(this.SqlServerConnection, sql, par);//单头数据
 9 
10         sql = "SELECT * FROM  body WHERE body001=@body001 AND body002=@body002 ";//单身数据
11         par.Clear();
12         par.Add("body001", pFormID);
13         par.Add("body002", pSheetNO);
14         DataTable dtBody = SqlDBHelper.ExecuteDataTable(this.SqlServerConnection, sql, par);
15 
16 
17         thisReplaceText(ref doc, "head002", dtHead.Rows[0]["head002"].ToString());
18         thisReplaceText(ref doc, "head003", dtHead.Rows[0]["head003"].ToString());
19         thisReplaceText(ref doc, "head004", dtHead.Rows[0]["head004"].ToString());
20         thisReplaceText(ref doc, "head005", dtHead.Rows[0]["head005"].ToString());
21         thisReplaceText(ref doc, "head006", dtHead.Rows[0]["head006"].ToString());
22         thisReplaceText(ref doc, "head008", dtHead.Rows[0]["head008"].ToString());
23         thisReplaceText(ref doc, "head009", dtHead.Rows[0]["head009"].ToString());
24         thisReplaceText(ref doc, "M", dtHead.Rows[0]["head010"].ToString());
25 
26         int bodyRowIndex = 0;
27 
28         int rowLength = 8;
29         Row insertRow = doc.Tables[0].Rows[rowLength];
30 
31         int mRowsLength = 0;
32         foreach (DataRow dr in dtBody.Rows)
33         {
34             mRowsLength += 1;
35             if (mRowsLength <= 5)
36             {
37                 doc.Tables[0].Rows[2 + mRowsLength].Cells[0].Paragraphs[0].Append(dr["body003"].ToString());
38                 doc.Tables[0].Rows[2 + mRowsLength].Cells[1].Paragraphs[0].Append(dr["body004"].ToString());
39                 doc.Tables[0].Rows[2 + mRowsLength].Cells[2].Paragraphs[0].Append(dr["body005"].ToString());
40             }
41             else
42             {
43                 //获取到模板行的对象(一般是一个空行,只有架子没有数据的)
44                 doc.Tables[0].Rows[rowLength + bodyRowIndex].Xml.AddAfterSelf(insertRow.Xml);
45                 bodyRowIndex += 1;
46                 doc.Tables[0].Rows[rowLength + bodyRowIndex].Cells[0].Paragraphs[0].Append(dr["body003"].ToString());
47                 doc.Tables[0].Rows[rowLength + bodyRowIndex].Cells[1].Paragraphs[0].Append(dr["body004"].ToString());
48                 doc.Tables[0].Rows[rowLength + bodyRowIndex].Cells[2].Paragraphs[0].Append(dr["body005"].ToString());
49             }
50         }
51         //删除模板行,要不然会有一个空行
52         doc.Tables[0].RemoveRow(rowLength);
53     }
View Code

 

   代码地址:http://pan.baidu.com/s/1eS5IK2u

 

ASP.NET 利用第三方插件DocX产生Word 之 打印批量打印