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 = 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

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com