NPOI創建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;
             }
         }
 上面代碼所創建的表見圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM