C#——操做Word並導出PDF

1、操做Word html

  首先引用這個DLL,Microsoft.Office.Interop.Word,官方提供的。服務器

  能夠操做word文字,表格,圖片等。app

  文字經過替換關鍵字的方式實現ui

  document.Paragraphs[i].Range.Text = temptext.Replace("{$village}", "HELLO WORLD");spa

  表格能夠本身獲取模板中已有的表格設計

   Microsoft.Office.Interop.Word.Table table1 = document.Tables[1];code

   table1.Cell(1, 1).Range.Text = "TEST1";orm

  也能夠本身建立表格,能夠設計表頭,單元格等。htm

 

   int tableRow = 6 ; int tableColumn = 6; //定義一個Word中的表格對象 Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs[i].Range, tableRow, tableColumn, ref Nothing, ref Nothing);
   //默認建立的表格沒有邊框,這裏修改其屬性,使得建立的表格帶有邊框 table.Borders.Enable = 1; table.Cell(1, 1).Merge(table.Cell(2, 1));//縱向合併 table.Cell(1, 1).Range.Text = "牌號/代碼"; table.Cell(1, 2).Merge(table.Cell(2, 2));//縱向合併 table.Cell(1, 2).Range.Text = "標準編號";
  有一篇文章寫的很詳細能夠參考下:http://www.javashuo.com/article/p-cibiyfup-eg.html

 

public bool CreateWord(DataTable dttmp)
        {
            bool result = false;
            Object Nothing = Missing.Value;
            Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document document = null;
            string path = @"C:\Users\Administrator\Desktop\BB\合同模版.doc";
            object FileName = @"C:\Users\Administrator\Desktop\BB\" + DateTime.Now.ToString("yyyyMMddHHmmssffffff") + ".doc";
            application.Visible = false;
            document = application.Documents.Open(path);

            int rowNum = dttmp.Rows.Count;
            for (int i = 1; i <= document.Paragraphs.Count; i++)
            {
                string temptext = document.Paragraphs[i].Range.Text;
                //如下爲替換文檔模版中的關鍵字
                if (temptext.Contains("{$village}"))
                    document.Paragraphs[i].Range.Text = temptext.Replace("{$village}", "HELLO WORLD");
                Microsoft.Office.Interop.Word.Table table1 = document.Tables[1];
                table1.Cell(1, 1).Range.Text = "TEST1";
                if (temptext.Contains("{$Table1}"))
                {
                    //設置表格的行數和列數
                    int tableRow = 6 + rowNum;
                    int tableColumn = 13;
                    //定義一個Word中的表格對象
                   

                    Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs[i].Range,
                      tableRow, tableColumn, ref Nothing, ref Nothing);
                    //默認建立的表格沒有邊框,這裏修改其屬性,使得建立的表格帶有邊框 
                     table.Borders.Enable = 1;//這個值能夠設置得很大
                     table.Cell(1, 1).Merge(table.Cell(2, 1));//縱向合併
                     table.Cell(1, 1).Range.Text = "牌號/代碼";

                     table.Cell(1, 2).Merge(table.Cell(2, 2));//縱向合併
                     table.Cell(1, 2).Range.Text = "標準編號";

                 

                     table.Cell(1, 6).Merge(table.Cell(2, 6));
                     table.Cell(1, 6).Range.Text = "單重\n(MT)";

                     table.Cell(1, 7).Merge(table.Cell(2, 7));
                     table.Cell(1, 7).Range.Text = "張數";
                     
                     table.Cell(1, 8).Merge(table.Cell(2, 8));
                     table.Cell(1, 8).Range.Text = "重量\n(MT))";
                  
                     

                     table.Cell(1, 9).Merge(table.Cell(2, 9));
                     table.Cell(1, 9).Range.Text = "單價\n(元/噸)";
                                        
                     table.Cell(1, 10).Merge(table.Cell(2, 10));
                     table.Cell(1, 10).Range.Text = "金額(人民幣)";
                                      
         
                     table.Cell(1, 13).Merge(table.Cell(2, 13));
                     table.Cell(1, 13).Range.Text = "交貨期";

                     table.Cell(1, 3).Merge(table.Cell(1, 5));//橫向合併
                     table.Cell(1, 3).Range.Text = "規格(mm)";
                     table.Cell(2, 3).Range.Text = "厚度";
                     table.Cell(2, 4).Range.Text = "寬度";
                     table.Cell(2, 5).Range.Text = "寬度";

                     table.Cell(1, 9).Merge(table.Cell(1, 10));
                     table.Cell(1, 10).Range.Text = "表面加工";
                     table.Cell(2, 11).Range.Text = "邊緣";
                     table.Cell(2, 11).Range.Text = "精度";
                
                }
            }
           
            object format = document.SaveFormat;
            document.Save();

            application.Quit(ref Nothing, ref Nothing, ref Nothing);
            return result;

        }

2、Word導出PDF對象

  

 public bool WordToPDF(string sourcePath)
        {
            bool result = false;
           Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document document = null;
            try
            {
                application.Visible = false;
                document = application.Documents.Open(sourcePath);
                string PDFPath = sourcePath.Replace(".doc", ".pdf");//pdf存放位置
                if (!File.Exists(@PDFPath))//存在PDF,不須要繼續轉換
                {
                    document.ExportAsFixedFormat(PDFPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
                }
                result = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                result = false;
            }
            finally
            {
                //document.Close();
            }
            return result;
        }

 服務器部署能夠參考;https://www.cnblogs.com/5426z/articles/4865312.html

相關文章
相關標籤/搜索