NPOI 导出 Word (没有Office操作Word)
2014-02-17 10:40
1241 查看
NPOI已出现一段时间了,目前版本2.0 Beta 2 [v2.0.5],网上关于NPOI操作xlsx文章较多,而关于docx的几乎没有,尽管NPOI对于Word还不稳定,经过一阵捣鼓后终于实现了表的简单操作:创建表、创建行、创建单元,单元行和列的合并。
环境:vs2010,netframework4
NPOI:由于目前网上提供的NPOI对于Word还不稳定,上述代码不能保证能用。
经过调试的例子下载:http://download.csdn.net/detail/gltide/6753163,例中附有最新NPOI。
最新NPOI(2.0RC)及上述例子(2014-1-9):http://download.csdn.net/detail/gltide/6829979,例中增加对页面设置和表居中代码。
转自:/article/2260411.html
环境:vs2010,netframework4
private void button1_Click(object sender, EventArgs e) { MemoryStream ms = new MemoryStream(); XWPFDocument m_Docx = new XWPFDocument(); m_Docx = CreatDocxTable(); m_Docx.Write(ms); ms.Flush(); SaveToFile(ms,"d:\\test.docx"); } protected XWPFDocument CreatDocxTable() { XWPFDocument m_Docx = new XWPFDocument(); XWPFParagraph p0 = m_Docx.CreateParagraph(); XWPFRun r0 = p0.CreateRun(); r0.SetText("DOCX表"); XWPFTable table = 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"); XWPFTableRow m_Row = table.CreateRow();//创建一行 m_Row = table.CreateRow();//创建一行 m_Row.GetCell(0).SetText("211"); //合并单元格 m_Row = table.InsertNewTableRow(0);//表头插入一行 XWPFTableCell cell = m_Row.CreateCell();//创建一个单元格,创建单元格时就创建了一个CT_P CT_Tc cttc = cell.GetCTTc(); CT_TcPr ctPr = cttc.AddNewTcPr(); ctPr.gridSpan.val = "3";//合并3列 cttc.GetPList()[0].AddNewPPr().AddNewJc().val= ST_Jc.center; cttc.GetPList()[0].AddNewR().AddNewT().Value = "abc"; XWPFTableRow td3 = 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_Row m_NewRow = new CT_Row(); m_Row = new XWPFTableRow(m_NewRow, table); table.AddRow(m_Row); //必须要!!! cell = m_Row.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 = "sss"; //表未增加行,合并2列,合并2行 //1行 m_NewRow = new CT_Row(); m_Row = new XWPFTableRow(m_NewRow, table); table.AddRow(m_Row); cell = m_Row.CreateCell(); cttc = cell.GetCTTc(); ctPr = cttc.AddNewTcPr(); ctPr.gridSpan.val = "2"; ctPr.AddNewVMerge().val = ST_Merge.restart;//合并行 ctPr.AddNewVAlign().val = ST_VerticalJc.center;//垂直居中 cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center; cttc.GetPList()[0].AddNewR().AddNewT().Value = "xxx"; cell = m_Row.CreateCell(); cell.SetText("ddd"); //2行,多行合并类似 m_NewRow = new CT_Row(); m_Row = new XWPFTableRow(m_NewRow, table); table.AddRow(m_Row); cell = m_Row.CreateCell(); cttc = cell.GetCTTc(); ctPr = cttc.AddNewTcPr(); ctPr.gridSpan.val = "2"; ctPr.AddNewVMerge().val = ST_Merge.@continue;//合并行 cell = m_Row.CreateCell(); cell.SetText("kkk"); ////3行 //m_NewRow = new CT_Row(); //m_Row = new XWPFTableRow(m_NewRow, table); //table.AddRow(m_Row); //cell = m_Row.CreateCell(); //cttc = cell.GetCTTc(); //ctPr = cttc.AddNewTcPr(); //ctPr.gridSpan.val = "2"; //ctPr.AddNewVMerge().val = ST_Merge.@continue; //cell = m_Row.CreateCell(); //cell.SetText("hhh"); return m_Docx; } static void SaveToFile(MemoryStream ms, string fileName) { using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { byte[] data = ms.ToArray(); fs.Write(data, 0, data.Length); fs.Flush(); data = null; } }
NPOI:由于目前网上提供的NPOI对于Word还不稳定,上述代码不能保证能用。
经过调试的例子下载:http://download.csdn.net/detail/gltide/6753163,例中附有最新NPOI。
最新NPOI(2.0RC)及上述例子(2014-1-9):http://download.csdn.net/detail/gltide/6829979,例中增加对页面设置和表居中代码。
转自:/article/2260411.html
相关文章推荐
- 在应用程序上实现OFFICE相关的WORD EXCEL导入导出操作
- NPOI 操作office、word、excel
- c#中做在线预览时没有在利用using时发现没有Microsoft.Office.Interop.Word;这个引用解决方案
- asp.net 生成导出word表单 ,导出excel; dataTable生成xls文件,返回前台下载;asp.net启动excel错误 80070005;excel-xls columnName 不能改变; 读写excel的开源利器NPOI; 设置excel Cell的数据类型;
- 使用ABAP编程实现对微软Office Word文档的操作
- POI导出word文档的基本操作
- 最近一直在做C#操作office方面的工作!总结一下!Word(二)
- Java程序员从笨鸟到菜鸟之(一百零三)java操作office和pdf文件(一)java读取word,excel和pdf文档内容
- 使用COM来进行WORD或EXECEL导出操作,出现错误: 80070005 解决方法
- C#操作Word文档(Office 2007)
- Jacob操作office文档(Word,PPT,Excel)
- .Net将数据导出Word(Office 2003)
- Jacob操作office文档(Word,PPT,Excel)
- C#操作office方面的总结 Excel和Word
- 使用NPOI2.1.3.1版本导出word附带表格和图片
- 无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”。请改用适用的接口。
- 放弃office使用xml操作word、xml
- C# 导出Word报”无法打开Office open xml文件。因为文件内容有错误“ 解决方法
- 办公文档操作word: Microsoft Office Word已停止工作 word打不开
- TX Text Control文字处理教程(12)MS Word中字段的导入导出操作