资源简介
NPOI已出现一段时间了,目前版本2.0 Beta 2 [v2.0.5],网上关于NPOI操作xlsx文章较多,而关于docx的几乎没有,尽管NPOI对于Word还不稳定,经过一阵捣鼓后终于实现了表的简单操作:创建表、创建行、创建单元,单元行和列的合并。供学习参考
![](http://www.nz998.com/pic/35012.jpg)
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NPOI.XWPF.UserModel;
using NPOI.OpenxmlFormats.Wordprocessing;
using System.IO;
/* 终于实现了表的简单操作:创建表、创建行、创建单元,单元行和列的合并。
* NPOI最好使用本例子提供的。
* vs2010
* netframework4
* 2013-12-18
*/
namespace NOPIDocxTable
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
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
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5091 2013-12-22 14:47 NOPIDocxTable\NOPIDocxTable\Form1.cs
文件 2027 2013-12-19 11:39 NOPIDocxTable\NOPIDocxTable\Form1.Designer.cs
文件 5817 2013-12-19 11:39 NOPIDocxTable\NOPIDocxTable\Form1.resx
文件 4119 2013-12-19 15:09 NOPIDocxTable\NOPIDocxTable\NOPIDocxTable.csproj
文件 200704 2013-12-18 06:03 NOPIDocxTable\NOPIDocxTable\NPOI\ICSharpCode.SharpZipLib.dll
文件 1771520 2013-12-18 17:48 NOPIDocxTable\NOPIDocxTable\NPOI\NPOI.dll
文件 395264 2013-12-18 17:48 NOPIDocxTable\NOPIDocxTable\NPOI\NPOI.OOxm
文件 426939 2013-12-18 17:48 NOPIDocxTable\NOPIDocxTable\NPOI\NPOI.OOxm
文件 92672 2013-12-18 17:48 NOPIDocxTable\NOPIDocxTable\NPOI\NPOI.Openxm
文件 151291 2013-12-18 17:48 NOPIDocxTable\NOPIDocxTable\NPOI\NPOI.Openxm
文件 2102272 2013-12-18 17:48 NOPIDocxTable\NOPIDocxTable\NPOI\NPOI.Openxm
文件 2121439 2013-12-18 17:48 NOPIDocxTable\NOPIDocxTable\NPOI\NPOI.xm
文件 4440 2013-12-19 15:00 NOPIDocxTable\NOPIDocxTable\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
文件 6514 2013-12-22 14:47 NOPIDocxTable\NOPIDocxTable\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 260 2013-12-20 12:57 NOPIDocxTable\NOPIDocxTable\obj\x86\Debug\GenerateResource.read.1.tlog
文件 642 2013-12-20 12:57 NOPIDocxTable\NOPIDocxTable\obj\x86\Debug\GenerateResource.write.1.tlog
文件 1342 2013-12-22 14:47 NOPIDocxTable\NOPIDocxTable\obj\x86\Debug\NOPIDocxTable.csproj.FileListAbsolute.txt
文件 10752 2013-12-22 14:47 NOPIDocxTable\NOPIDocxTable\obj\x86\Debug\NOPIDocxTable.exe
文件 180 2013-12-20 12:57 NOPIDocxTable\NOPIDocxTable\obj\x86\Debug\NOPIDocxTable.Form1.resources
文件 26112 2013-12-22 14:47 NOPIDocxTable\NOPIDocxTable\obj\x86\Debug\NOPIDocxTable.pdb
文件 180 2013-12-20 12:57 NOPIDocxTable\NOPIDocxTable\obj\x86\Debug\NOPIDocxTable.Properties.Resources.resources
文件 30043 2013-12-20 17:16 NOPIDocxTable\NOPIDocxTable\obj\x86\Debug\ResolveAssemblyReference.cache
文件 494 2013-12-19 11:27 NOPIDocxTable\NOPIDocxTable\Program.cs
文件 1358 2013-12-19 11:27 NOPIDocxTable\NOPIDocxTable\Properties\AssemblyInfo.cs
文件 2877 2013-12-19 11:27 NOPIDocxTable\NOPIDocxTable\Properties\Resources.Designer.cs
文件 5612 2013-12-19 11:27 NOPIDocxTable\NOPIDocxTable\Properties\Resources.resx
文件 1099 2013-12-19 11:27 NOPIDocxTable\NOPIDocxTable\Properties\Settings.Designer.cs
文件 249 2013-12-19 11:27 NOPIDocxTable\NOPIDocxTable\Properties\Settings.settings
文件 881 2013-12-19 11:27 NOPIDocxTable\NOPIDocxTable.sln
..A..H. 19968 2013-12-22 14:47 NOPIDocxTable\NOPIDocxTable.suo
............此处省略25个文件信息
- 上一篇:BCM94352HMB 黑苹果驱动
- 下一篇:winsail模型
相关资源
- ado数据库MFC图书管理系统vs2010
-
解决安装vs2012后vs2010 li
nk : fatal er - AnkhSvn-2.6.12735 支持VS2010-VS2015 官方原版
- VS2010、VS2012、VS2013代码自动注释插件
- 插件式GIS应用框架的设计与实现&mda
- 通过x264录制RGB屏幕视频vs2013工程,
- G726、G711、G711A转换为AAC
- VS2010 运行时库
- vs2010 ffmpeg实时解码h264码流
- 个人银行账户管理系统
- CMake-3.9.0
- 基于FFMPEG_SDL2_音视频播放_参考音频时
- G-code-Interpreter,G代码解释器
- 三款主题VS2010
- VS2010黑色主题Dark主题
- Visual Assist X VA_X插件及破解工具,适用
- TabControl用法图解VS2010
- .net导入导出Excel文件
- Midas to FLAC3D-vs2010
- 浴池管理系统
- ecc使用源代码——真正好用的vs2010编
- 基于SIFT算法的图像拼接.rar
- NPOI2.2版本
- VisualStudio 2008-2017系统注释中文包,解
- word表格排版工具 v6.1.zip
- vs2010 win7下编译的openssl-1.0.2n静态库
- iar 工程转成vs2010工程
- NPOI最新版2.4.1
- NPOI使用手册
- NPOI2.0官方版本
评论
共有 条评论