Npoi导出word(Peanuts)
版权声明:本文为博主原创文章,未经博主允许不得转载。
一个项目,要做一个从数据库读取数据,然后导出到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;
最终效果图如下:
关键代码如下:
- ///<summary>
- ///新增
- ///</summary>
- ///<paramname="sender"></param>
- ///<paramname="e"></param>
- protectedvoidbtnPrint_Click(objectsender,EventArgse)
- {
- //创建document对象
- XWPFDocumentdoc=newXWPFDocument();
- //创建段落对象
- XWPFParagraphp1=doc.CreateParagraph();
- p1.Alignment=ParagraphAlignment.CENTER;//字体居中
- //创建run对象
- //本节提到的所有样式都是基于XWPFRun的,
- //你可以把XWPFRun理解成一小段文字的描述对象,
- //这也是Word文档的特征,即文本描述性文档。
- //来自TonyQuhttp://tonyqus.sinaapp.com/archives/609
- XWPFRunrunTitle=p1.CreateRun();
- runTitle.IsBold=true;
- runTitle.SetText("军检验收单");
- runTitle.FontSize=16;
- runTitle.SetFontFamily("宋体",FontCharRange.None);//设置雅黑字体
- XWPFParagraphp2=doc.CreateParagraph();
- XWPFRunrun1=p2.CreateRun();
- run1.SetText("军检项目号:");
- run1.FontSize=12;
- run1.SetFontFamily("华文楷体",FontCharRange.None);//设置雅黑字体
- #region头部(6rows)
- //基本row12,列5;头部6行,4列
- XWPFTabletableTop=doc.CreateTable(6,5);
- tableTop.Width=1000*5;
- tableTop.SetColumnWidth(0,1300);/*设置列宽*/
- tableTop.SetColumnWidth(1,500);/*设置列宽*/
- tableTop.SetColumnWidth(2,1000);/*设置列宽*/
- tableTop.SetColumnWidth(3,500);/*设置列宽*/
- tableTop.SetColumnWidth(4,1700);/*设置列宽*/
- tableTop.GetRow(0).MergeCells(1,4);/*合并行*/
- tableTop.GetRow(0).GetCell(0).SetParagraph(SetCellText(doc,tableTop,"产品名称"));
- tableTop.GetRow(0).GetCell(1).SetParagraph(SetCellText(doc,tableTop,""));
- tableTop.GetRow(1).MergeCells(1,4);
- tableTop.GetRow(1).GetCell(0).SetParagraph(SetCellText(doc,tableTop,"项目名称"));
- tableTop.GetRow(1).GetCell(1).SetParagraph(SetCellText(doc,tableTop,""));
- tableTop.GetRow(2).MergeCells(1,4);
- tableTop.GetRow(2).GetCell(0).SetParagraph(SetCellText(doc,tableTop,"施工依据",ParagraphAlignment.CENTER,45));
- tableTop.GetRow(2).GetCell(1).SetParagraph(SetCellText(doc,tableTop,"",ParagraphAlignment.CENTER,45));
- tableTop.GetRow(3).GetCell(0).SetParagraph(SetCellText(doc,tableTop,"检验方式"));
- tableTop.GetRow(3).GetCell(1).SetParagraph(SetCellText(doc,tableTop,"独立检验"));
- tableTop.GetRow(3).GetCell(2).SetParagraph(SetCellText(doc,tableTop,""));
- tableTop.GetRow(3).GetCell(3).SetParagraph(SetCellText(doc,tableTop,"联合检验"));
- tableTop.GetRow(3).GetCell(4).SetParagraph(SetCellText(doc,tableTop,""));
- tableTop.GetRow(4).MergeCells(3,4);
- tableTop.GetRow(4).GetCell(0).SetParagraph(SetCellText(doc,tableTop,"设备名称及编号"));
- tableTop.GetRow(4).GetCell(1).SetParagraph(SetCellText(doc,tableTop,""));
- tableTop.GetRow(4).GetCell(2).SetParagraph(SetCellText(doc,tableTop,"设备制造厂"));
- tableTop.GetRow(4).GetCell(3).SetParagraph(SetCellText(doc,tableTop,""));
- //tableTop.GetRow(4).GetCell(3).SetBorderBottom(XWPFtableTop.XWPFBorderType.NONE,0,0,"");
- tableTop.GetRow(5).MergeCells(0,4);
- CT_Ppara=newCT_P();
- XWPFParagraphpCell=newXWPFParagraph(para,tableTop.Body);
- pCell.Alignment=ParagraphAlignment.LEFT;//字体居中
- XWPFRunr1c1=pCell.CreateRun();
- r1c1.SetText("检验要素共9项");
- r1c1.FontSize=12;
- r1c1.SetFontFamily("华文楷体",FontCharRange.None);//设置雅黑字体
- tableTop.GetRow(5).GetCell(0).SetParagraph(pCell);
- //table.GetRow(6).GetCell(0).SetParagraph(SetCellText(doc,table,"序号"));
- //table.GetRow(6).GetCell(1).SetParagraph(SetCellText(doc,table,"检验要素"));
- //table.GetRow(6).GetCell(2).SetParagraph(SetCellText(doc,table,"指标要求"));
- //table.GetRow(6).GetCell(3).SetParagraph(SetCellText(doc,table,"实测值"));
- //table.GetRow(6).GetCell(4).SetParagraph(SetCellText(doc,table,"测量工具编号及有效期"));
- #endregion
- #region检验要素列表部分(数据库读取循环显示)
- /*打印1页:小于8行数据,创建9行;
- *打印2页:大于8小于26行数据,创建27行。增加18
- *打印3页:大于26小于44行数据,创建45行。增加18
- */
- XWPFTabletableContent=doc.CreateTable(45,5);
- tableContent.Width=1000*5;
- tableContent.SetColumnWidth(0,300);/*设置列宽*/
- tableContent.SetColumnWidth(1,1000);/*设置列宽*/
- tableContent.SetColumnWidth(2,1000);/*设置列宽*/
- tableContent.SetColumnWidth(3,1000);/*设置列宽*/
- tableContent.SetColumnWidth(4,1700);/*设置列宽*/
- tableContent.GetRow(0).GetCell(0).SetParagraph(SetCellText(doc,tableContent,"序号"));
- tableContent.GetRow(0).GetCell(1).SetParagraph(SetCellText(doc,tableContent,"检验要素"));
- tableContent.GetRow(0).GetCell(2).SetParagraph(SetCellText(doc,tableContent,"指标要求"));
- tableContent.GetRow(0).GetCell(3).SetParagraph(SetCellText(doc,tableContent,"实测值"));
- tableContent.GetRow(0).GetCell(4).SetParagraph(SetCellText(doc,tableContent,"测量工具编号及有效期"));
- for(inti=1;i<45;i++)
- {
- tableContent.GetRow(i).GetCell(0).SetParagraph(SetCellText(doc,tableContent,i.ToString(),ParagraphAlignment.CENTER,50));
- tableContent.GetRow(i).GetCell(1).SetParagraph(SetCellText(doc,tableContent,"检验要素",ParagraphAlignment.CENTER,50));
- tableContent.GetRow(i).GetCell(2).SetParagraph(SetCellText(doc,tableContent,"指标要求",ParagraphAlignment.CENTER,50));
- tableContent.GetRow(i).GetCell(3).SetParagraph(SetCellText(doc,tableContent,"实测值",ParagraphAlignment.CENTER,50));
- tableContent.GetRow(i).GetCell(4).SetParagraph(SetCellText(doc,tableContent,"测量工具编号及有效期",ParagraphAlignment.CENTER,50));
- }
- #endregion
- #region底部内容
- XWPFTabletableBottom=doc.CreateTable(5,4);
- tableBottom.Width=1000*5;
- tableBottom.SetColumnWidth(0,1000);/*设置列宽*/
- tableBottom.SetColumnWidth(1,1500);/*设置列宽*/
- tableBottom.SetColumnWidth(2,1000);/*设置列宽*/
- tableBottom.SetColumnWidth(3,1500);/*设置列宽*/
- tableBottom.GetRow(0).MergeCells(0,3);/*合并行*/
- tableBottom.GetRow(0).GetCell(0).SetParagraph(SetCellText(doc,tableBottom,"附件:",ParagraphAlignment.LEFT,80));
- tableBottom.GetRow(0).Height=30;
- tableBottom.GetRow(1).MergeCells(0,3);/*合并行*/
- tableBottom.GetRow(1).GetCell(0).SetParagraph(SetCellText(doc,tableBottom,"检验结论:",ParagraphAlignment.LEFT,80));
- tableBottom.GetRow(1).Height=30;
- tableBottom.GetRow(2).GetCell(0).SetParagraph(SetCellText(doc,tableBottom,"施工部门"));
- tableBottom.GetRow(2).GetCell(1).SetParagraph(SetCellText(doc,tableBottom,""));
- tableBottom.GetRow(2).GetCell(2).SetParagraph(SetCellText(doc,tableBottom,"报验日期"));
- tableBottom.GetRow(2).GetCell(3).SetParagraph(SetCellText(doc,tableBottom,""));
- tableBottom.GetRow(3).GetCell(0).SetParagraph(SetCellText(doc,tableBottom,"军检次数"));
- tableBottom.GetRow(3).GetCell(1).SetParagraph(SetCellText(doc,tableBottom,""));
- tableBottom.GetRow(3).GetCell(2).SetParagraph(SetCellText(doc,tableBottom,"军检日期"));
- tableBottom.GetRow(3).GetCell(3).SetParagraph(SetCellText(doc,tableBottom,""));
- tableBottom.GetRow(4).GetCell(0).SetParagraph(SetCellText(doc,tableBottom,"检验员"));
- tableBottom.GetRow(4).GetCell(1).SetParagraph(SetCellText(doc,tableBottom,""));
- tableBottom.GetRow(4).GetCell(2).SetParagraph(SetCellText(doc,tableBottom,"军代表"));
- tableBottom.GetRow(4).GetCell(3).SetParagraph(SetCellText(doc,tableBottom,""));
- #endregion
- //保存文件到磁盘WinForm
- //stringdocPath=Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,"DocxWord");
- //if(!Directory.Exists(docPath)){Directory.CreateDirectory(docPath);}
- //stringfileName=string.Format("{0}.doc",HttpUtility.UrlEncode("jjysd"+"_"+DateTime.Now.ToString("yyyyMMddHHmmssfff"),System.Text.Encoding.UTF8));
- //FileStreamout1=newFileStream(Path.Combine(docPath,fileName),FileMode.Create);
- //doc.Write(out1);
- //out1.Close();
- #region保存导出WebForm
- //Response.Redirect(ResolveUrl(string.Format(@"~\DocxWord\{0}",fileName)));
- System.IO.MemoryStreamms=newSystem.IO.MemoryStream();
- doc.Write(ms);
- Response.AddHeader("Content-Disposition",string.Format("attachment;filename={0}.doc",HttpUtility.UrlEncode("文件名"+"_"+DateTime.Now.ToString("yyyyMMddHHmmssfff"),System.Text.Encoding.UTF8)));
- Response.BinaryWrite(ms.ToArray());
- Response.End();
- ms.Close();
- ms.Dispose();
- //using(MemoryStreamms=newMemoryStream())
- //{
- //doc.Write(ms);
- //Response.ClearContent();
- //Response.Buffer=true;
- //Response.ContentType="application/octet-stream";
- //Response.AddHeader("Content-Disposition",string.Format("attachment;filename={0}.doc",HttpUtility.UrlEncode("军检验收单"+"_"+DateTime.Now.ToString("yyyyMMddHHmmssfff"),System.Text.Encoding.UTF8)));
- //Response.BinaryWrite(ms.ToArray());
- ////Response.End();
- //Response.Flush();
- //doc=null;
- //ms.Close();
- //ms.Dispose();
- //}
- #endregion
- }
- ///<summary>
- ///设置字体格式
- ///</summary>
- ///<paramname="doc"></param>
- ///<paramname="table"></param>
- ///<paramname="setText"></param>
- ///<returns></returns>
- publicXWPFParagraphSetCellText(XWPFDocumentdoc,XWPFTabletable,stringsetText)
- {
- //table中的文字格式设置
- CT_Ppara=newCT_P();
- XWPFParagraphpCell=newXWPFParagraph(para,table.Body);
- pCell.Alignment=ParagraphAlignment.CENTER;//字体居中
- pCell.VerticalAlignment=TextAlignment.CENTER;//字体居中
- XWPFRunr1c1=pCell.CreateRun();
- r1c1.SetText(setText);
- r1c1.FontSize=12;
- r1c1.SetFontFamily("华文楷体",FontCharRange.None);//设置雅黑字体
- //r1c1.SetTextPosition(20);//设置高度
- returnpCell;
- }
- ///<summary>
- ///设置单元格格式
- ///</summary>
- ///<paramname="doc">doc对象</param>
- ///<paramname="table">表格对象</param>
- ///<paramname="setText">要填充的文字</param>
- ///<paramname="align">文字对齐方式</param>
- ///<paramname="textPos">rows行的高度</param>
- ///<returns></returns>
- publicXWPFParagraphSetCellText(XWPFDocumentdoc,XWPFTabletable,stringsetText,ParagraphAlignmentalign,inttextPos)
- {
- CT_Ppara=newCT_P();
- XWPFParagraphpCell=newXWPFParagraph(para,table.Body);
- //pCell.Alignment=ParagraphAlignment.LEFT;//字体
- pCell.Alignment=align;
- XWPFRunr1c1=pCell.CreateRun();
- r1c1.SetText(setText);
- r1c1.FontSize=12;
- r1c1.SetFontFamily("华文楷体",FontCharRange.None);//设置雅黑字体
- r1c1.SetTextPosition(textPos);//设置高度
- returnpCell;
- }
设置table的宽度,以及没列的宽度代码如下:
- XWPFTabletableTop=doc.CreateTable(6,5);
- tableTop.Width=1000*5;/*设置table宽度,必须设置width,SetColumnWidth才有效*/
- tableTop.SetColumnWidth(0,1300);/*设置第一列宽*/
- tableTop.SetColumnWidth(1,500);/*设置第二列宽*/
- tableTop.SetColumnWidth(2,1000);/*设置列宽*/
- tableTop.SetColumnWidth(3,500);/*设置列宽*/
- 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
- privatevoidbutton1_Click(objectsender,EventArgse)
- {
- MemoryStreamms=newMemoryStream();
- XWPFDocumentm_Docx=newXWPFDocument();
- m_Docx=CreatDocxTable();
- m_Docx.Write(ms);
- ms.Flush();
- SaveToFile(ms,"d:\\test.docx");
- }
- protectedXWPFDocumentCreatDocxTable()
- {
- XWPFDocumentm_Docx=newXWPFDocument();
- XWPFParagraphp0=m_Docx.CreateParagraph();
- XWPFRunr0=p0.CreateRun();
- r0.SetText("DOCX表");
- XWPFTabletable=m_Docx.CreateTable(1,3);//创建一行3列表
- table.GetRow(0).GetCell(0).SetText("111");
- table.GetRow(0).GetCell(1).SetText("222");
- table.GetRow(0).GetCell(2).SetText("333");
- XWPFTableRowm_Row=table.CreateRow();//创建一行
- m_Row=table.CreateRow();//创建一行
- m_Row.GetCell(0).SetText("211");
- //合并单元格
- m_Row=table.InsertNewTableRow(0);//表头插入一行
- XWPFTableCellcell=m_Row.CreateCell();//创建一个单元格,创建单元格时就创建了一个CT_P
- CT_Tccttc=cell.GetCTTc();
- CT_TcPrctPr=cttc.AddNewTcPr();
- ctPr.gridSpan.val="3";//合并3列
- cttc.GetPList()[0].AddNewPPr().AddNewJc().val=ST_Jc.center;
- cttc.GetPList()[0].AddNewR().AddNewT().Value="abc";
- XWPFTableRowtd3=table.InsertNewTableRow(table.Rows.Count-1);//插入行
- cell=td3.CreateCell();
- cttc=cell.GetCTTc();
- ctPr=cttc.AddNewTcPr();
- ctPr.gridSpan.val="3";
- cttc.GetPList()[0].AddNewPPr().AddNewJc().val=ST_Jc.center;
- cttc.GetPList()[0].AddNewR().AddNewT().Value="qqq";
- //表增加行,合并列
- CT_Rowm_NewRow=newCT_Row();
- m_Row=newXWPFTableRow(m_NewRow,table);
知识推荐
- 点聚weboffice隐藏自带工具栏
- jQuery动画处理
- jsz中的作用域与上下文
- Web应用安全审计工具WATOBO
- 网站部署
- MVC model验证 获取验证错误信息
- jssdk.php
- PHP中关于PDO数据访问抽象层的功能操作
- [js高手之路]Node.js模板引擎教程-jade速学与实战2-流程控制,转义与非转义
- JS replace()用法
- php PDO遇到的坑
- HTTP状态码
- Nginx+Keepalived搭建高可用负载平衡WEB 集群
- 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC
- CentOS6.8单独编译安装PHP gd库扩展
- phpqrcode 乱码/phpqrcode 图片不显示
- jquery鼠标悬停事件hover()
- js中的分支与循环