C#导出WORD

摘要:
你可以操作单词、表格、图片等。

一、操作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:UsersAdministratorDesktopBB合同模版.doc";
            object FileName = @"C:UsersAdministratorDesktopBB" + 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 = "单重
(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 = "重量
(MT))";
                  
                     

                     table.Cell(1, 9).Merge(table.Cell(2, 9));
                     table.Cell(1, 9).Range.Text = "单价
(元/吨)";
                                        
                     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;
        }
复制代码

免责声明:文章转载自《C#导出WORD》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇安卓手机清缓存方式下拉菜单(css)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

eltable数据遍历结合elform校验

需要实现的效果 最新遇到一个需求,数据在table中遍历展示,且需要校验每一项数据的格式,而且表头数据需要增加必填项*标示。 这里的校验和平时的校验不一样的是此处的数据是循环遍历展示的,因此要注意 prop的值为`bindList[${scope.$index}].nickName` :model="bindForm"中bindForm的数据类型,...

oracle 将表名和字段名变为大写

文章目录 前言 一、批量将表名变为大写 二、批量将空间内所有表的所有字段名变成大写 三、将用户空间的所有表名及所有字段变为大写 前言 当使用powerdesigner创建数据库时要注意大小写。注:以下脚本在oracle 10g,11g上正确执行 一、批量将表名变为大写 1 begin 2 for c in (select table_name t...

【使用 DOM】为DOM元素设置样式

1. 使用样式表 可以通过document.styleSheets属性访问文档中可用的CSS样式表,它会返回一组对象集合,这些对象代表了与文档管理的各个样式表。 每个样式表 都由一个CSSStyleSheet 对象代表,它提供了一组属性和方法来操作文档里的样式。 1.1 获得样式表的基本信息 第一步是获得定义在文档中的样式表的一些基本信息。 <!...

FlinkSQL源码阅读-schema管理

在Flink SQL中, 元数据的管理分为三层: catalog-> database-> table, 我们知道Flink SQL是依托calcite框架来进行SQL执行树生产,校验,优化等等, 所以本文讲介绍FlinkSQL是如何来结合Calcite来进行元数据管理的. calcite开放的接口 public interface Schem...

git merge合并解决冲突问题

效果:B 分支内容合并到 A 分支 git check A git merge origin/B 比如冲突文件为t.txt 修改掉冲突后 git add t.txt git commit git push origin A 完成。...

移植Kdump至嵌入式ARM64环境【转】

转自:https://blog.csdn.net/lsy673908720/article/details/90215501 工欲善其事,必先利其器。操作系统崩溃是高级Linux运维工程师很头疼的事情,但也不要害怕,前辈们已经给我们磨了好多把刀,其中有一把叫做Kdump。目前在主流的Linux发行版如Ubuntu、Centos等已经默认安装或者仓库中含有工...