NPOI 导出 Word (没有Office操作Word)

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

        环境: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,例中增加对页面设置和表居中代码。


转自:http://blog.csdn.net/gltide/article/details/17416547

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值