分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

.net core 导出word文档

发布时间:2023-09-06 01:08责任编辑:郭大石关键词:word

Npoi导出word(Peanuts)

标签:C#npoi导出word合并列列样式
2015-10-05 22:162896人阅读评论(0)收藏举报
分类:

版权声明:本文为博主原创文章,未经博主允许不得转载。

一个项目,要做一个从数据库读取数据,然后导出到word,因为涉及到后台数据库的读取,决定用npoi来导出word。

NPOI源码地址:http://npoi.codeplex.com/

NPOI 2.0 api文档:http://www.npoi.info/npoi2tutorial

因为npoi操作word的文章比较少,在由于版本不同,相关的函数可能不一样,这个就需要大家自己去慢慢的探索了。

例如:作者的api文档中

c.字体加粗

r1.SetBold(true);

实际我在调用时,调用的接口是r1c1.IsBold = 12;

我使用的版本是:NPOI v2.2.0.0

需要引用的命名空间如下

using NPOI.XWPF.UserModel;
using NPOI.OpenXmlFormats.Wordprocessing;

最终效果图如下:

关键代码如下:

[csharp]view plaincopy
  1. ///<summary>
  2. ///新增
  3. ///</summary>
  4. ///<paramname="sender"></param>
  5. ///<paramname="e"></param>
  6. protectedvoidbtnPrint_Click(objectsender,EventArgse)
  7. {
  8. //创建document对象
  9. XWPFDocumentdoc=newXWPFDocument();
  10. //创建段落对象
  11. XWPFParagraphp1=doc.CreateParagraph();
  12. p1.Alignment=ParagraphAlignment.CENTER;//字体居中
  13. //创建run对象
  14. //本节提到的所有样式都是基于XWPFRun的,
  15. //你可以把XWPFRun理解成一小段文字的描述对象,
  16. //这也是Word文档的特征,即文本描述性文档。
  17. //来自TonyQuhttp://tonyqus.sinaapp.com/archives/609
  18. XWPFRunrunTitle=p1.CreateRun();
  19. runTitle.IsBold=true;
  20. runTitle.SetText("军检验收单");
  21. runTitle.FontSize=16;
  22. runTitle.SetFontFamily("宋体",FontCharRange.None);//设置雅黑字体
  23. XWPFParagraphp2=doc.CreateParagraph();
  24. XWPFRunrun1=p2.CreateRun();
  25. run1.SetText("军检项目号:");
  26. run1.FontSize=12;
  27. run1.SetFontFamily("华文楷体",FontCharRange.None);//设置雅黑字体
  28. #region头部(6rows)
  29. //基本row12,列5;头部6行,4列
  30. XWPFTabletableTop=doc.CreateTable(6,5);
  31. tableTop.Width=1000*5;
  32. tableTop.SetColumnWidth(0,1300);/*设置列宽*/
  33. tableTop.SetColumnWidth(1,500);/*设置列宽*/
  34. tableTop.SetColumnWidth(2,1000);/*设置列宽*/
  35. tableTop.SetColumnWidth(3,500);/*设置列宽*/
  36. tableTop.SetColumnWidth(4,1700);/*设置列宽*/
  37. tableTop.GetRow(0).MergeCells(1,4);/*合并行*/
  38. tableTop.GetRow(0).GetCell(0).SetParagraph(SetCellText(doc,tableTop,"产品名称"));
  39. tableTop.GetRow(0).GetCell(1).SetParagraph(SetCellText(doc,tableTop,""));
  40. tableTop.GetRow(1).MergeCells(1,4);
  41. tableTop.GetRow(1).GetCell(0).SetParagraph(SetCellText(doc,tableTop,"项目名称"));
  42. tableTop.GetRow(1).GetCell(1).SetParagraph(SetCellText(doc,tableTop,""));
  43. tableTop.GetRow(2).MergeCells(1,4);
  44. tableTop.GetRow(2).GetCell(0).SetParagraph(SetCellText(doc,tableTop,"施工依据",ParagraphAlignment.CENTER,45));
  45. tableTop.GetRow(2).GetCell(1).SetParagraph(SetCellText(doc,tableTop,"",ParagraphAlignment.CENTER,45));
  46. tableTop.GetRow(3).GetCell(0).SetParagraph(SetCellText(doc,tableTop,"检验方式"));
  47. tableTop.GetRow(3).GetCell(1).SetParagraph(SetCellText(doc,tableTop,"独立检验"));
  48. tableTop.GetRow(3).GetCell(2).SetParagraph(SetCellText(doc,tableTop,""));
  49. tableTop.GetRow(3).GetCell(3).SetParagraph(SetCellText(doc,tableTop,"联合检验"));
  50. tableTop.GetRow(3).GetCell(4).SetParagraph(SetCellText(doc,tableTop,""));
  51. tableTop.GetRow(4).MergeCells(3,4);
  52. tableTop.GetRow(4).GetCell(0).SetParagraph(SetCellText(doc,tableTop,"设备名称及编号"));
  53. tableTop.GetRow(4).GetCell(1).SetParagraph(SetCellText(doc,tableTop,""));
  54. tableTop.GetRow(4).GetCell(2).SetParagraph(SetCellText(doc,tableTop,"设备制造厂"));
  55. tableTop.GetRow(4).GetCell(3).SetParagraph(SetCellText(doc,tableTop,""));
  56. //tableTop.GetRow(4).GetCell(3).SetBorderBottom(XWPFtableTop.XWPFBorderType.NONE,0,0,"");
  57. tableTop.GetRow(5).MergeCells(0,4);
  58. CT_Ppara=newCT_P();
  59. XWPFParagraphpCell=newXWPFParagraph(para,tableTop.Body);
  60. pCell.Alignment=ParagraphAlignment.LEFT;//字体居中
  61. XWPFRunr1c1=pCell.CreateRun();
  62. r1c1.SetText("检验要素共9项");
  63. r1c1.FontSize=12;
  64. r1c1.SetFontFamily("华文楷体",FontCharRange.None);//设置雅黑字体
  65. tableTop.GetRow(5).GetCell(0).SetParagraph(pCell);
  66. //table.GetRow(6).GetCell(0).SetParagraph(SetCellText(doc,table,"序号"));
  67. //table.GetRow(6).GetCell(1).SetParagraph(SetCellText(doc,table,"检验要素"));
  68. //table.GetRow(6).GetCell(2).SetParagraph(SetCellText(doc,table,"指标要求"));
  69. //table.GetRow(6).GetCell(3).SetParagraph(SetCellText(doc,table,"实测值"));
  70. //table.GetRow(6).GetCell(4).SetParagraph(SetCellText(doc,table,"测量工具编号及有效期"));
  71. #endregion
  72. #region检验要素列表部分(数据库读取循环显示)
  73. /*打印1页:小于8行数据,创建9行;
  74. *打印2页:大于8小于26行数据,创建27行。增加18
  75. *打印3页:大于26小于44行数据,创建45行。增加18
  76. */
  77. XWPFTabletableContent=doc.CreateTable(45,5);
  78. tableContent.Width=1000*5;
  79. tableContent.SetColumnWidth(0,300);/*设置列宽*/
  80. tableContent.SetColumnWidth(1,1000);/*设置列宽*/
  81. tableContent.SetColumnWidth(2,1000);/*设置列宽*/
  82. tableContent.SetColumnWidth(3,1000);/*设置列宽*/
  83. tableContent.SetColumnWidth(4,1700);/*设置列宽*/
  84. tableContent.GetRow(0).GetCell(0).SetParagraph(SetCellText(doc,tableContent,"序号"));
  85. tableContent.GetRow(0).GetCell(1).SetParagraph(SetCellText(doc,tableContent,"检验要素"));
  86. tableContent.GetRow(0).GetCell(2).SetParagraph(SetCellText(doc,tableContent,"指标要求"));
  87. tableContent.GetRow(0).GetCell(3).SetParagraph(SetCellText(doc,tableContent,"实测值"));
  88. tableContent.GetRow(0).GetCell(4).SetParagraph(SetCellText(doc,tableContent,"测量工具编号及有效期"));
  89. for(inti=1;i<45;i++)
  90. {
  91. tableContent.GetRow(i).GetCell(0).SetParagraph(SetCellText(doc,tableContent,i.ToString(),ParagraphAlignment.CENTER,50));
  92. tableContent.GetRow(i).GetCell(1).SetParagraph(SetCellText(doc,tableContent,"检验要素",ParagraphAlignment.CENTER,50));
  93. tableContent.GetRow(i).GetCell(2).SetParagraph(SetCellText(doc,tableContent,"指标要求",ParagraphAlignment.CENTER,50));
  94. tableContent.GetRow(i).GetCell(3).SetParagraph(SetCellText(doc,tableContent,"实测值",ParagraphAlignment.CENTER,50));
  95. tableContent.GetRow(i).GetCell(4).SetParagraph(SetCellText(doc,tableContent,"测量工具编号及有效期",ParagraphAlignment.CENTER,50));
  96. }
  97. #endregion
  98. #region底部内容
  99. XWPFTabletableBottom=doc.CreateTable(5,4);
  100. tableBottom.Width=1000*5;
  101. tableBottom.SetColumnWidth(0,1000);/*设置列宽*/
  102. tableBottom.SetColumnWidth(1,1500);/*设置列宽*/
  103. tableBottom.SetColumnWidth(2,1000);/*设置列宽*/
  104. tableBottom.SetColumnWidth(3,1500);/*设置列宽*/
  105. tableBottom.GetRow(0).MergeCells(0,3);/*合并行*/
  106. tableBottom.GetRow(0).GetCell(0).SetParagraph(SetCellText(doc,tableBottom,"附件:",ParagraphAlignment.LEFT,80));
  107. tableBottom.GetRow(0).Height=30;
  108. tableBottom.GetRow(1).MergeCells(0,3);/*合并行*/
  109. tableBottom.GetRow(1).GetCell(0).SetParagraph(SetCellText(doc,tableBottom,"检验结论:",ParagraphAlignment.LEFT,80));
  110. tableBottom.GetRow(1).Height=30;
  111. tableBottom.GetRow(2).GetCell(0).SetParagraph(SetCellText(doc,tableBottom,"施工部门"));
  112. tableBottom.GetRow(2).GetCell(1).SetParagraph(SetCellText(doc,tableBottom,""));
  113. tableBottom.GetRow(2).GetCell(2).SetParagraph(SetCellText(doc,tableBottom,"报验日期"));
  114. tableBottom.GetRow(2).GetCell(3).SetParagraph(SetCellText(doc,tableBottom,""));
  115. tableBottom.GetRow(3).GetCell(0).SetParagraph(SetCellText(doc,tableBottom,"军检次数"));
  116. tableBottom.GetRow(3).GetCell(1).SetParagraph(SetCellText(doc,tableBottom,""));
  117. tableBottom.GetRow(3).GetCell(2).SetParagraph(SetCellText(doc,tableBottom,"军检日期"));
  118. tableBottom.GetRow(3).GetCell(3).SetParagraph(SetCellText(doc,tableBottom,""));
  119. tableBottom.GetRow(4).GetCell(0).SetParagraph(SetCellText(doc,tableBottom,"检验员"));
  120. tableBottom.GetRow(4).GetCell(1).SetParagraph(SetCellText(doc,tableBottom,""));
  121. tableBottom.GetRow(4).GetCell(2).SetParagraph(SetCellText(doc,tableBottom,"军代表"));
  122. tableBottom.GetRow(4).GetCell(3).SetParagraph(SetCellText(doc,tableBottom,""));
  123. #endregion
  124. //保存文件到磁盘WinForm
  125. //stringdocPath=Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,"DocxWord");
  126. //if(!Directory.Exists(docPath)){Directory.CreateDirectory(docPath);}
  127. //stringfileName=string.Format("{0}.doc",HttpUtility.UrlEncode("jjysd"+"_"+DateTime.Now.ToString("yyyyMMddHHmmssfff"),System.Text.Encoding.UTF8));
  128. //FileStreamout1=newFileStream(Path.Combine(docPath,fileName),FileMode.Create);
  129. //doc.Write(out1);
  130. //out1.Close();
  131. #region保存导出WebForm
  132. //Response.Redirect(ResolveUrl(string.Format(@"~\DocxWord\{0}",fileName)));
  133. System.IO.MemoryStreamms=newSystem.IO.MemoryStream();
  134. doc.Write(ms);
  135. Response.AddHeader("Content-Disposition",string.Format("attachment;filename={0}.doc",HttpUtility.UrlEncode("文件名"+"_"+DateTime.Now.ToString("yyyyMMddHHmmssfff"),System.Text.Encoding.UTF8)));
  136. Response.BinaryWrite(ms.ToArray());
  137. Response.End();
  138. ms.Close();
  139. ms.Dispose();
  140. //using(MemoryStreamms=newMemoryStream())
  141. //{
  142. //doc.Write(ms);
  143. //Response.ClearContent();
  144. //Response.Buffer=true;
  145. //Response.ContentType="application/octet-stream";
  146. //Response.AddHeader("Content-Disposition",string.Format("attachment;filename={0}.doc",HttpUtility.UrlEncode("军检验收单"+"_"+DateTime.Now.ToString("yyyyMMddHHmmssfff"),System.Text.Encoding.UTF8)));
  147. //Response.BinaryWrite(ms.ToArray());
  148. ////Response.End();
  149. //Response.Flush();
  150. //doc=null;
  151. //ms.Close();
  152. //ms.Dispose();
  153. //}
  154. #endregion
  155. }

[csharp]view plaincopy
  1. ///<summary>
  2. ///设置字体格式
  3. ///</summary>
  4. ///<paramname="doc"></param>
  5. ///<paramname="table"></param>
  6. ///<paramname="setText"></param>
  7. ///<returns></returns>
  8. publicXWPFParagraphSetCellText(XWPFDocumentdoc,XWPFTabletable,stringsetText)
  9. {
  10. //table中的文字格式设置
  11. CT_Ppara=newCT_P();
  12. XWPFParagraphpCell=newXWPFParagraph(para,table.Body);
  13. pCell.Alignment=ParagraphAlignment.CENTER;//字体居中
  14. pCell.VerticalAlignment=TextAlignment.CENTER;//字体居中
  15. XWPFRunr1c1=pCell.CreateRun();
  16. r1c1.SetText(setText);
  17. r1c1.FontSize=12;
  18. r1c1.SetFontFamily("华文楷体",FontCharRange.None);//设置雅黑字体
  19. //r1c1.SetTextPosition(20);//设置高度
  20. returnpCell;
  21. }
  22. ///<summary>
  23. ///设置单元格格式
  24. ///</summary>
  25. ///<paramname="doc">doc对象</param>
  26. ///<paramname="table">表格对象</param>
  27. ///<paramname="setText">要填充的文字</param>
  28. ///<paramname="align">文字对齐方式</param>
  29. ///<paramname="textPos">rows行的高度</param>
  30. ///<returns></returns>
  31. publicXWPFParagraphSetCellText(XWPFDocumentdoc,XWPFTabletable,stringsetText,ParagraphAlignmentalign,inttextPos)
  32. {
  33. CT_Ppara=newCT_P();
  34. XWPFParagraphpCell=newXWPFParagraph(para,table.Body);
  35. //pCell.Alignment=ParagraphAlignment.LEFT;//字体
  36. pCell.Alignment=align;
  37. XWPFRunr1c1=pCell.CreateRun();
  38. r1c1.SetText(setText);
  39. r1c1.FontSize=12;
  40. r1c1.SetFontFamily("华文楷体",FontCharRange.None);//设置雅黑字体
  41. r1c1.SetTextPosition(textPos);//设置高度
  42. returnpCell;
  43. }


设置table的宽度,以及没列的宽度代码如下:

[csharp]view plaincopy
  1. XWPFTabletableTop=doc.CreateTable(6,5);
  2. tableTop.Width=1000*5;/*设置table宽度,必须设置width,SetColumnWidth才有效*/
  3. tableTop.SetColumnWidth(0,1300);/*设置第一列宽*/
  4. tableTop.SetColumnWidth(1,500);/*设置第二列宽*/
  5. tableTop.SetColumnWidth(2,1000);/*设置列宽*/
  6. tableTop.SetColumnWidth(3,500);/*设置列宽*/
  7. tableTop.SetColumnWidth(4,1700);/*设置列宽*/
和并列以及设置列的高度:

tableTop.GetRow(0).MergeCells(1, 4);/* 合并列 */

XWPFRun r1c1 = pCell.CreateRun();
r1c1.SetText(setText);
r1c1.FontSize = 12;
r1c1.SetFontFamily("华文楷体", FontCharRange.None);//设置雅黑字体
r1c1.SetTextPosition(textPos);//设置高度

NPOI已出现一段时间了,目前版本2.0 Beta 2 [v2.0.5],网上关于NPOI操作xlsx文章较多,而关于docx的几乎没有,尽管NPOI对于Word还不稳定,经过一阵捣鼓后终于实现了表的简单操作:创建表、创建行、创建单元,单元行和列的合并。

环境:vs2010,netframework4

[csharp]view plaincopyprint?
  1. privatevoidbutton1_Click(objectsender,EventArgse)
  2. {
  3. MemoryStreamms=newMemoryStream();
  4. XWPFDocumentm_Docx=newXWPFDocument();
  5. m_Docx=CreatDocxTable();
  6. m_Docx.Write(ms);
  7. ms.Flush();
  8. SaveToFile(ms,"d:\\test.docx");
  9. }
  10. protectedXWPFDocumentCreatDocxTable()
  11. {
  12. XWPFDocumentm_Docx=newXWPFDocument();
  13. XWPFParagraphp0=m_Docx.CreateParagraph();
  14. XWPFRunr0=p0.CreateRun();
  15. r0.SetText("DOCX表");
  16. XWPFTabletable=m_Docx.CreateTable(1,3);//创建一行3列表
  17. table.GetRow(0).GetCell(0).SetText("111");
  18. table.GetRow(0).GetCell(1).SetText("222");
  19. table.GetRow(0).GetCell(2).SetText("333");
  20. XWPFTableRowm_Row=table.CreateRow();//创建一行
  21. m_Row=table.CreateRow();//创建一行
  22. m_Row.GetCell(0).SetText("211");
  23. //合并单元格
  24. m_Row=table.InsertNewTableRow(0);//表头插入一行
  25. XWPFTableCellcell=m_Row.CreateCell();//创建一个单元格,创建单元格时就创建了一个CT_P
  26. CT_Tccttc=cell.GetCTTc();
  27. CT_TcPrctPr=cttc.AddNewTcPr();
  28. ctPr.gridSpan.val="3";//合并3列
  29. cttc.GetPList()[0].AddNewPPr().AddNewJc().val=ST_Jc.center;
  30. cttc.GetPList()[0].AddNewR().AddNewT().Value="abc";
  31. XWPFTableRowtd3=table.InsertNewTableRow(table.Rows.Count-1);//插入行
  32. cell=td3.CreateCell();
  33. cttc=cell.GetCTTc();
  34. ctPr=cttc.AddNewTcPr();
  35. ctPr.gridSpan.val="3";
  36. cttc.GetPList()[0].AddNewPPr().AddNewJc().val=ST_Jc.center;
  37. cttc.GetPList()[0].AddNewR().AddNewT().Value="qqq";
  38. //表增加行,合并列
  39. CT_Rowm_NewRow=newCT_Row();
  40. m_Row=newXWPFTableRow(m_NewRow,table);
我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved