zoukankan      html  css  js  c++  java
  • C# 后台模块 Word 模板操作

     public static string CreateWord()
            {
                //**********************************************
                //来自博客http://blog.csdn.net/fujie724
                //**********************************************
                object oMissing = System.Reflection.Missing.Value;
                //创建一个Word应用程序实例
                Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();
                //设置为不可见
                oWord.Visible = false;
                //模板文件地址,这里假设在X盘根目录   模板word 位置
                object oTemplate = "d://采购单生成表.docx";
                //以模板为基础生成文档
                Microsoft.Office.Interop.Word._Document oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
                //声明书签数组
                object[] oBookMark = new object[11];
                //赋值书签名  //获取模板 指定书签
                oBookMark[0] = "MerchantName";
                oBookMark[1] = "CustomerName";
                oBookMark[2] = "SHR";
                oBookMark[3] = "PZS";
                oBookMark[4] = "Address";
                oBookMark[5] = "CGDNO";
                oBookMark[6] = "Phone";
                oBookMark[7] = "NO";
                oBookMark[8] = "CreateTime";
                oBookMark[9] = "Table";
    
    
    
                //赋值任意数据到书签的位置
                oDoc.Bookmarks.get_Item(ref oBookMark[0]).Range.Text = "新华瀚品";
                oDoc.Bookmarks.get_Item(ref oBookMark[1]).Range.Text = "李四";
                oDoc.Bookmarks.get_Item(ref oBookMark[2]).Range.Text = "李四";
                oDoc.Bookmarks.get_Item(ref oBookMark[3]).Range.Text = "5";
                oDoc.Bookmarks.get_Item(ref oBookMark[4]).Range.Text = "贺州平山开路区33号";
                oDoc.Bookmarks.get_Item(ref oBookMark[5]).Range.Text = "CGD123456789";
                oDoc.Bookmarks.get_Item(ref oBookMark[6]).Range.Text = "15832665267";
                oDoc.Bookmarks.get_Item(ref oBookMark[7]).Range.Text = "A00001";
                oDoc.Bookmarks.get_Item(ref oBookMark[8]).Range.Text = "2018/01/01";
    
    
                Word.Range range = oDoc.Bookmarks.get_Item(ref oBookMark[9]).Range;
                Word.Table textTable = oDoc.Tables.Add(range, 3, 8, ref oMissing, ref oMissing);
    
                /**表格 水平居中**/
                oWord.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                /**表格 外边线**/
                textTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
    
                /**设置 表格外边线 行**/
                for (int k = 1; k <= 3; k++)
                {
                    textTable.Rows[k].Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                }
                /**设置 表格外边线 列**/
                for (int k = 1; k <= 8; k++)
                {
                    textTable.Columns[k].Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                    if (k == 1)
                    {
                        textTable.Columns[k].Width = 40f;
                    }
                }
    
                /**垂直居中**/
                object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
                object countjz = 1;
                oWord.Selection.MoveEnd(ref unit, ref countjz);
                textTable.Select();
                oWord.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中
    
                /**设置单元格表头**/
                textTable.Cell(1, 1).Range.Text = "序号";
                textTable.Cell(1, 1).Merge(textTable.Cell(2, 1));//合并得话 是在这里操作
                textTable.Cell(1, 2).Range.Text = "条形码";
                textTable.Cell(1, 2).Merge(textTable.Cell(2, 2));
                textTable.Cell(1, 3).Range.Text = "品名";
                textTable.Cell(1, 3).Merge(textTable.Cell(2, 3));
                textTable.Cell(1, 4).Range.Text = "规格";
                textTable.Cell(1, 4).Merge(textTable.Cell(2, 4));
                textTable.Cell(1, 5).Range.Text = "计量单位";
                textTable.Cell(1, 5).Merge(textTable.Cell(2, 5));
                textTable.Cell(1, 6).Range.Text = "数量";
                textTable.Cell(1, 6).Merge(textTable.Cell(2, 6));
                textTable.Cell(1, 7).Range.Text = "发货价";
                textTable.Cell(1, 7).Merge(textTable.Cell(2, 7));
                textTable.Cell(1, 8).Range.Text = "备注";
                textTable.Cell(1, 8).Merge(textTable.Cell(2, 8));
    
                textTable.Rows.Select();
                /**设置单元格样式 行**/
                Word.Range rngCell = null;
                for (int i = 1; i <= 8; i++)
                {
                    textTable.Cell(1, i).Height = 20f;//设置 行高
                    rngCell = textTable.Cell(1, i).Range;
                    rngCell.Font.Bold = 1;
                    rngCell.Font.Name = "微软雅黑";
                    rngCell.Font.Size = 10.5f;
                }
    
                /**添加 数据行**/
                for (int i = 2; i < 3; i++)
                {
                    textTable.Cell(i, 1).Range.Text = (i-1).ToString();
                    textTable.Cell(i, 2).Range.Text = "1";
                    textTable.Cell(i, 3).Range.Text = "1";
                    textTable.Cell(i, 4).Range.Text = "1";
                    textTable.Cell(i, 5).Range.Text = "1";
                    textTable.Cell(i, 6).Range.Text = "1";
                    textTable.Cell(i, 7).Range.Text = "1";
                    textTable.Cell(i, 8).Range.Text = "1";
                }
    
                //保存生成的Word
                object filename = "d://测试.docx";// 要 绝对路径 保存的位置
                oDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);
                oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                //关闭word
                oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
                return "";
    
            }
    
    using Word = Microsoft.Office.Interop.Word;  声明方便使用
    

      

      

    这个 之前需要 增加一个 word  模板 放到指定得位置 并在相应节点添加 书签   结合到  增加表单操作, 需要添加 下面得dll

    作者注释: 这种方法  必须在服务器上安装 office 套装 才能完成 word 操作(安装需要掏钱得), 可以用 NPOI  相关插件,

    直接项目中  管理 NuGet程序包    中搜索 NPOI 直接安装,在做相应操作

  • 相关阅读:
    gdb --configuration
    firecracker 编译
    gvisor 编译
    gvisor
    rust Deref
    rust explicit
    rust move
    rust drop
    出租人对经营租赁的会计处理
    关于以公允价值计量的投资性房地产的处置
  • 原文地址:https://www.cnblogs.com/nnqwbc/p/9476750.html
Copyright © 2011-2022 走看看