C#使用Spire.Doc Word for .Net讀寫Word

之前對Excel或Word文檔操做都使用微軟的COM組件Microsoft Word 15.0 object library。c++

可是這種方式必需要求服務器上安裝Office,並且會出現讀寫操做完成後未成功釋放資源的狀況。服務器

還有使用NPOI的方式,能夠在沒有安裝Office的狀況下對Word或Excel文檔進行讀寫操做,可是面對複雜的需求,仍是顯得無能爲力。ui

因而出現了一些其餘的API庫,如Spire.Doc和Aspose,不但能夠很好地讀寫操做Office文檔,並且能夠輕鬆實現PDF等文檔格式的轉換。spa

Spire.Doc支持JAVA和C#開發語言。咱們能夠從官網下載到。code

Document document = new Document();
Section section = document.AddSection();
ShowProgressBar(); //顯示進度條
for (int i = 0; i < dsSpInfo.Tables[0].Rows.Count; i++)
{
       GenerateWord(document,section, dsSpInfo.Tables[0], dsQiandi.Tables[0], dsBaocun.Tables[0],i); //生成Word文檔
}
section.AddColumn(550f, 50f);  //添加分欄,參數爲分欄寬度
section.AddColumn(550f, 50f);
section.MakeColumnsSameWidth();
string s = Guid.NewGuid().ToString("N") + ".docx";
document.SaveToFile(@"temp\" + s, FileFormat.Docx);
document.Close();
System.Diagnostics.Process.Start(@"temp\" + s);
private void GenerateWord(Document document,Section section,DataTable dtSpInfo, DataTable dtQiandi, DataTable dtBaocun,int index)
{

      Paragraph pFamily = section.AddParagraph();   //添加段落
      AddTextRange(section, pFamily, cfamily, 14, true, "黑體", Spire.Doc.Documents.HorizontalAlignment.Center);
       AddTextRange(section, pFamily, family, 14, true, "宋體", Spire.Doc.Documents.HorizontalAlignment.Center);

       Paragraph paragraph = section.AddParagraph();
       AddTextRange(section, paragraph, cGenus, 12, true, "黑體", Spire.Doc.Documents.HorizontalAlignment.Left);
      AddTextRange(section, paragraph, genus, 12, true, "Times New Roman", Spire.Doc.Documents.HorizontalAlignment.Left);

      DataRow[] drQiandi = dtQiandi.Select("SPID='" + spid + "'"); //獲取表格數據
      if (drQiandi.Length > 0) 
      {
          String[] headerQiandi = { "保存地點", "種質份數", "個體數量", "引種方式", "來源地", "生長情況" };   //表頭字段
          string[][] arrQiandiData = new string[drQiandi.Length][];
          for (int i = 0; i < drQiandi.Length; i++)
          {
               arrQiandiData[i] = new string[] { drQiandi[i]["保存地點"].ToString(), drQiandi[i]["種質份數"].ToString(), drQiandi[i]["個體數量"].ToString(), drQiandi[i]["引種方式"].ToString(), drQiandi[i]["來源地"].ToString(), drQiandi[i]["生長情況"].ToString() };
          }
         Table tableQiandi = section.AddTable();  //新建表格
         tableQiandi.ResetCells(arrQiandiData.Length + 1, headerQiandi.Length);
         tableQiandi.TableFormat.Borders.BorderType = Spire.Doc.Documents.BorderStyle.Single;

        TableRow rowQiandi = tableQiandi.Rows[0];  //添加行
        rowQiandi.IsHeader = true;  //設爲表頭
        rowQiandi.Height = 16;
        rowQiandi.HeightType = TableRowHeightType.Auto;
         for (int i = 0; i < headerQiandi.Length; i++) //生成表頭
         {
              rowQiandi.Cells[i].Width = 50;
              rowQiandi.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
              rowQiandi.Height = 20;
              rowQiandi.HeightType = TableRowHeightType.Auto;
              Paragraph p = rowQiandi.Cells[i].AddParagraph();
              AddTextRange(section, p, headerQiandi[i], 9, true, "黑體", Spire.Doc.Documents.HorizontalAlignment.Center);
          }

         for (int r = 0; r < arrQiandiData.Length; r++) //生成表體
         {
               TableRow dataRow = tableQiandi.Rows[r + 1];
               dataRow.RowFormat.BackColor = Color.Empty;
               for (int c = 0; c < arrQiandiData[r].Length; c++)
               {
                  dataRow.Cells[c].Width = 50;
                  dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                  TextRange tr = dataRow.Cells[c].AddParagraph().AppendText(arrQiandiData[r][c]);
                   tr.CharacterFormat.FontSize = 8;
                 }
            }
       }
}
private void AddTextRange(Section section, Paragraph pragraph, string word, float fontSize, bool isBold, string fontName, Spire.Doc.Documents.HorizontalAlignment alignType)
{
            
      TextRange textRange = pragraph.AppendText(word);
      textRange.CharacterFormat.FontSize = fontSize;
      textRange.CharacterFormat.Bold = isBold;
      textRange.CharacterFormat.FontName = fontName;
      pragraph.Format.HorizontalAlignment =  alignType;
}

下面是生成的效果圖orm

須要注意的是,Spire.Doc是收費軟件,若是超過了免費版生成表格的數量(好像是25個),就要收取費用。不然會在生成的文檔的第一頁上方中添加廣告。blog

相關文章
相關標籤/搜索