当前位置: 移动技术网 > IT编程>开发语言>.net > C#——操作Word并导出PDF

C#——操作Word并导出PDF

2019年06月05日  | 移动技术网IT编程  | 我要评论

上海破碎机,潮湿的心伴奏,华丽的外出电影

一、操作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";

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

 

   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 = "标准编号";
  有一篇文章写的很详细可以参考下:https://www.cnblogs.com/xh6300/p/5915717.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;

        }

二、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;
        }

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网