前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#——操作Word并导出PDF

C#——操作Word并导出PDF

作者头像
_一级菜鸟
发布2019-09-10 10:26:44
1.2K0
发布2019-09-10 10:26:44
举报
文章被收录于专栏:工厂程序员工厂程序员

一、操作Word 

  首先引用这个DLL,Microsoft.Office.Interop.Word,官方提供的。

  可以操作word文字,表格,图片等。

  文字通过替换关键字的方式实现

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";

  也可以自己创建表格,可以设计表头,单元格等。

代码语言:javascript
复制
   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);
代码语言:javascript
复制
   //默认创建的表格没有边框,这里修改其属性,使得创建的表格带有边框 
   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 = "标准编号";
代码语言:javascript
复制
  有一篇文章写的很详细可以参考下:https://www.cnblogs.com/xh6300/p/5915717.html
代码语言:javascript
复制
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;

        }

二、Word导出PDF

代码语言:javascript
复制
 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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-06-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档