.net core 导出word文档

 

Npoi导出word(Peanuts)

标签: C#npoi导出word合并列列样式
 分类:

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

[csharp] view plain copy
 
  1. /// <summary>  
  2. /// 设置字体格式  
  3. /// </summary>  
  4. /// <param name="doc"></param>  
  5. /// <param name="table"></param>  
  6. /// <param name="setText"></param>  
  7. /// <returns></returns>  
  8. public XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText)  
  9. {  
  10.     //table中的文字格式设置  
  11.     CT_P para = new CT_P();  
  12.     XWPFParagraph pCell = new XWPFParagraph(para, table.Body);  
  13.     pCell.Alignment = ParagraphAlignment.CENTER;//字体居中  
  14.     pCell.VerticalAlignment = TextAlignment.CENTER;//字体居中  
  15.   
  16.     XWPFRun r1c1 = pCell.CreateRun();  
  17.     r1c1.SetText(setText);  
  18.     r1c1.FontSize = 12;  
  19.     r1c1.SetFontFamily("华文楷体", FontCharRange.None);//设置雅黑字体  
  20.     //r1c1.SetTextPosition(20);//设置高度  
  21.   
  22.     return pCell;  
  23. }  
  24.   
  25. /// <summary>  
  26. /// 设置单元格格式  
  27. /// </summary>  
  28. /// <param name="doc">doc对象</param>  
  29. /// <param name="table">表格对象</param>  
  30. /// <param name="setText">要填充的文字</param>  
  31. /// <param name="align">文字对齐方式</param>  
  32. /// <param name="textPos">rows行的高度</param>  
  33. /// <returns></returns>  
  34. public XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText, ParagraphAlignment align, int textPos)  
  35. {  
  36.     CT_P para = new CT_P();  
  37.     XWPFParagraph pCell = new XWPFParagraph(para, table.Body);  
  38.     //pCell.Alignment = ParagraphAlignment.LEFT;//字体  
  39.     pCell.Alignment = align;  
  40.   
  41.     XWPFRun r1c1 = pCell.CreateRun();  
  42.     r1c1.SetText(setText);  
  43.     r1c1.FontSize = 12;  
  44.     r1c1.SetFontFamily("华文楷体", FontCharRange.None);//设置雅黑字体  
  45.     r1c1.SetTextPosition(textPos);//设置高度  
  46.   
  47.     return pCell;  
  48. }  


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

[csharp] view plain copy
 
  1. XWPFTable tableTop = 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 plain copy
 
 print?
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     MemoryStream ms = new MemoryStream();  
  4.     XWPFDocument m_Docx = new XWPFDocument();  
  5.     m_Docx = CreatDocxTable();  
  6.     m_Docx.Write(ms);  
  7.     ms.Flush();  
  8.     SaveToFile(ms,"d:\test.docx");  
  9. }  
  10. protected XWPFDocument CreatDocxTable()  
  11. {  
  12.     XWPFDocument m_Docx = new XWPFDocument();  
  13.     XWPFParagraph p0 = m_Docx.CreateParagraph();  
  14.     XWPFRun r0 = p0.CreateRun();  
  15.     r0.SetText("DOCX表");  
  16.   
  17.     XWPFTable table = m_Docx.CreateTable(1, 3);//创建一行3列表  
  18.     table.GetRow(0).GetCell(0).SetText("111");  
  19.     table.GetRow(0).GetCell(1).SetText("222");  
  20.     table.GetRow(0).GetCell(2).SetText("333");  
  21.   
  22.     XWPFTableRow m_Row = table.CreateRow();//创建一行  
  23.     m_Row = table.CreateRow();//创建一行  
  24.     m_Row.GetCell(0).SetText("211");  
  25.   
  26.     //合并单元格  
  27.     m_Row = table.InsertNewTableRow(0);//表头插入一行  
  28.     XWPFTableCell cell = m_Row.CreateCell();//创建一个单元格,创建单元格时就创建了一个CT_P  
  29.     CT_Tc cttc = cell.GetCTTc();  
  30.     CT_TcPr ctPr = cttc.AddNewTcPr();  
  31.     ctPr.gridSpan.val = "3";//合并3列  
  32.     cttc.GetPList()[0].AddNewPPr().AddNewJc().val= ST_Jc.center;  
  33.     cttc.GetPList()[0].AddNewR().AddNewT().Value = "abc";       
  34.   
  35.     XWPFTableRow td3 = table.InsertNewTableRow(table.Rows.Count - 1);//插入行  
  36.     cell = td3.CreateCell();  
  37.     cttc = cell.GetCTTc();  
  38.     ctPr = cttc.AddNewTcPr();  
  39.     ctPr.gridSpan.val = "3";  
  40.     cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;  
  41.     cttc.GetPList()[0].AddNewR().AddNewT().Value = "qqq";  
  42.   
  43.     //表增加行,合并列  
  44.     CT_Row m_NewRow = new CT_Row();  
  45.     m_Row = new XWPFTableRow(m_NewRow, table);  
  46.     table.AddRow(m_Row); //必须要!!!  
  47.     cell = m_Row.CreateCell();  
  48.     cttc = cell.GetCTTc();  
  49.     ctPr = cttc.AddNewTcPr();  
  50.     ctPr.gridSpan.val = "3";  
  51.     cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;  
  52.     cttc.GetPList()[0].AddNewR().AddNewT().Value = "sss";  
  53.   
  54.     //表未增加行,合并2列,合并2行  
  55.     //1行  
  56.     m_NewRow = new CT_Row();  
  57.     m_Row = new XWPFTableRow(m_NewRow, table);  
  58.     table.AddRow(m_Row);  
  59.     cell = m_Row.CreateCell();  
  60.     cttc = cell.GetCTTc();  
  61.     ctPr = cttc.AddNewTcPr();  
  62.     ctPr.gridSpan.val = "2";  
  63.     ctPr.AddNewVMerge().val = ST_Merge.restart;//合并行  
  64.     ctPr.AddNewVAlign().val = ST_VerticalJc.center;//垂直居中  
  65.     cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;  
  66.     cttc.GetPList()[0].AddNewR().AddNewT().Value = "xxx";  
  67.     cell = m_Row.CreateCell();  
  68.     cell.SetText("ddd");  
  69.     //2行,多行合并类似  
  70.     m_NewRow = new CT_Row();  
  71.     m_Row = new XWPFTableRow(m_NewRow, table);  
  72.     table.AddRow(m_Row);  
  73.     cell = m_Row.CreateCell();  
  74.     cttc = cell.GetCTTc();  
  75.     ctPr = cttc.AddNewTcPr();  
  76.     ctPr.gridSpan.val = "2";  
  77.     ctPr.AddNewVMerge().val = ST_Merge.@continue;//合并行  
  78.     cell = m_Row.CreateCell();  
  79.     cell.SetText("kkk");  
  80.     ////3行  
  81.     //m_NewRow = new CT_Row();  
  82.     //m_Row = new XWPFTableRow(m_NewRow, table);  
  83.     //table.AddRow(m_Row);  
  84.     //cell = m_Row.CreateCell();  
  85.     //cttc = cell.GetCTTc();  
  86.     //ctPr = cttc.AddNewTcPr();  
  87.     //ctPr.gridSpan.val = "2";  
  88.     //ctPr.AddNewVMerge().val = ST_Merge.@continue;  
  89.     //cell = m_Row.CreateCell();  
  90.     //cell.SetText("hhh");  
  91.   
  92.     return m_Docx;  
  93. }  
  94. static void SaveToFile(MemoryStream ms, string fileName)  
  95. {  
  96.     using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))  
  97.     {  
  98.         byte[] data = ms.ToArray();  
  99.   
  100.         fs.Write(data, 0, data.Length);  
  101.         fs.Flush();  
  102.         data = null;  
  103.     }  
  104. }  


原文地址:https://www.cnblogs.com/a2502971/p/7464738.html