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

        环境:vs2010,netframework4

 

 

  1.  
    private void button1_Click(object sender, EventArgs e)
  2.  
    {
  3.  
    MemoryStream ms = new MemoryStream();
  4.  
    XWPFDocument m_Docx = new XWPFDocument();
  5.  
    m_Docx = CreatDocxTable();
  6.  
    m_Docx.Write(ms);
  7.  
    ms.Flush();
  8.  
    SaveToFile(ms,"d:\\test.docx");
  9.  
    }
  10.  
    protected XWPFDocument CreatDocxTable()
  11.  
    {
  12.  
    XWPFDocument m_Docx = new XWPFDocument();
  13.  
    XWPFParagraph p0 = m_Docx.CreateParagraph();
  14.  
    XWPFRun r0 = p0.CreateRun();
  15.  
    r0.SetText("DOCX表");
  16.  
     
  17.  
    XWPFTable table = m_Docx.CreateTable(1, 3);//创建一行3列表
  18.  
    table.GetRow(0).GetCell(0).SetText("111");
  19.  
    table.GetRow(0).GetCell(1).SetText("222");
  20.  
    table.GetRow(0).GetCell(2).SetText("333");
  21.  
     
  22.  
    XWPFTableRow m_Row = table.CreateRow();//创建一行
  23.  
    m_Row = table.CreateRow();//创建一行
  24.  
    m_Row.GetCell(0).SetText("211");
  25.  
     
  26.  
    //合并单元格
  27.  
    m_Row = table.InsertNewTableRow(0);//表头插入一行
  28.  
    XWPFTableCell cell = m_Row.CreateCell();//创建一个单元格,创建单元格时就创建了一个CT_P
  29.  
    CT_Tc cttc = cell.GetCTTc();
  30.  
    CT_TcPr ctPr = cttc.AddNewTcPr();
  31.  
    ctPr.gridSpan.val = "3";//合并3列
  32.  
    cttc.GetPList()[0].AddNewPPr().AddNewJc().val= ST_Jc.center;
  33.  
    cttc.GetPList()[0].AddNewR().AddNewT().Value = "abc";
  34.  
     
  35.  
    XWPFTableRow td3 = table.InsertNewTableRow(table.Rows.Count - 1);//插入行
  36.  
    cell = td3.CreateCell();
  37.  
    cttc = cell.GetCTTc();
  38.  
    ctPr = cttc.AddNewTcPr();
  39.  
    ctPr.gridSpan.val = "3";
  40.  
    cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
  41.  
    cttc.GetPList()[0].AddNewR().AddNewT().Value = "qqq";
  42.  
     
  43.  
    //表增加行,合并列
  44.  
    CT_Row m_NewRow = new CT_Row();
  45.  
    m_Row = new XWPFTableRow(m_NewRow, table);
  46.  
    table.AddRow(m_Row); //必须要!!!
  47.  
    cell = m_Row.CreateCell();
  48.  
    cttc = cell.GetCTTc();
  49.  
    ctPr = cttc.AddNewTcPr();
  50.  
    ctPr.gridSpan.val = "3";
  51.  
    cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
  52.  
    cttc.GetPList()[0].AddNewR().AddNewT().Value = "sss";
  53.  
     
  54.  
    //表未增加行,合并2列,合并2行
  55.  
    //1行
  56.  
    m_NewRow = new CT_Row();
  57.  
    m_Row = new XWPFTableRow(m_NewRow, table);
  58.  
    table.AddRow(m_Row);
  59.  
    cell = m_Row.CreateCell();
  60.  
    cttc = cell.GetCTTc();
  61.  
    ctPr = cttc.AddNewTcPr();
  62.  
    ctPr.gridSpan.val = "2";
  63.  
    ctPr.AddNewVMerge().val = ST_Merge.restart;//合并行
  64.  
    ctPr.AddNewVAlign().val = ST_VerticalJc.center;//垂直居中
  65.  
    cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;
  66.  
    cttc.GetPList()[0].AddNewR().AddNewT().Value = "xxx";
  67.  
    cell = m_Row.CreateCell();
  68.  
    cell.SetText("ddd");
  69.  
    //2行,多行合并类似
  70.  
    m_NewRow = new CT_Row();
  71.  
    m_Row = new XWPFTableRow(m_NewRow, table);
  72.  
    table.AddRow(m_Row);
  73.  
    cell = m_Row.CreateCell();
  74.  
    cttc = cell.GetCTTc();
  75.  
    ctPr = cttc.AddNewTcPr();
  76.  
    ctPr.gridSpan.val = "2";
  77.  
    ctPr.AddNewVMerge().val = ST_Merge.@continue;//合并行
  78.  
    cell = m_Row.CreateCell();
  79.  
    cell.SetText("kkk");
  80.  
    ////3行
  81.  
    //m_NewRow = new CT_Row();
  82.  
    //m_Row = new XWPFTableRow(m_NewRow, table);
  83.  
    //table.AddRow(m_Row);
  84.  
    //cell = m_Row.CreateCell();
  85.  
    //cttc = cell.GetCTTc();
  86.  
    //ctPr = cttc.AddNewTcPr();
  87.  
    //ctPr.gridSpan.val = "2";
  88.  
    //ctPr.AddNewVMerge().val = ST_Merge.@continue;
  89.  
    //cell = m_Row.CreateCell();
  90.  
    //cell.SetText("hhh");
  91.  
     
  92.  
    return m_Docx;
  93.  
    }
  94.  
    static void SaveToFile(MemoryStream ms, string fileName)
  95.  
    {
  96.  
    using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
  97.  
    {
  98.  
    byte[] data = ms.ToArray();
  99.  
     
  100.  
    fs.Write(data, 0, data.Length);
  101.  
    fs.Flush();
  102.  
    data = null;
  103.  
    }
  104.  
    }


 

 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