zoukankan      html  css  js  c++  java
  • Docx 生成word文档

    1.生成word代码
      1   /// <summary>
      2             /// 生成word文档
      3             /// </summary>
      4             /// <param name="tempPath">模板绝对路径</param>
      5             /// <param name="savePath">模板保存路径(包含文件名称 后缀必须是docx)</param>
      6             /// <param name="hsHeads">页眉数据</param>
      7             /// <param name="hsFoots">页脚数据</param>
      8             /// <param name="hsBookMark">书签数据</param>
      9             /// <param name="dtBody">文档内容</param>
     10               public static void  SaveTemplate(string tempPath,string savePath,Hashtable hsHeads,Hashtable hsFoots,Hashtable hsBookMark,DataTable dtBody){
     11                   using (DocX document = DocX.Load(tempPath))
     12                   {
     13                       #region 生成页眉
     14                       document.AddHeaders(); //添加所有页眉
     15                       Headers heads = document.Headers; //获取该文档所有的页脚
     16                       Header hfirst = heads.first;
     17                       Header head1 = heads.even;
     18                       Header head2 = heads.odd;
     19 
     20                       //添加logo
     21                       Paragraph p = head1.InsertParagraph("", false);
     22                       System.Net.WebRequest webreq = System.Net.WebRequest.Create("http://www.bc.ccoo.cn/logo/logo.gif");
     23                       System.Net.WebResponse webres = webreq.GetResponse();
     24                       Stream stream = webres.GetResponseStream();
     25                       MemoryStream stmMemory = new MemoryStream();
     26                       System.Drawing.Image myimg = System.Drawing.Image.FromStream(stream);
     27                       myimg.Save(stmMemory, myimg.RawFormat);  // 保存你的图片到memorystream
     28                       stmMemory.Seek(0, SeekOrigin.Begin);
     29                       Novacode.Image img = document.AddImage(stmMemory);
     30                       stream.Close();
     31 
     32                       //将图像插入到段落后面
     33                       Picture pic = img.CreatePicture();
     34 
     35                       //选择图像,并修改图像尺寸
     36                       pic.Rotation = 0;
     37                       pic.Width = 150;
     38                       pic.Height = 55;
     39 
     40                       //设置图片形状,并水平翻转图片
     41                       pic.SetPictureShape(BasicShapes.cube);
     42                       pic.FlipHorizontal = false;
     43                       p.InsertPicture(pic, 0);
     44                       p.InsertText("                                      真诚为您服务");
     45                       p.AppendLine();
     46                       Paragraph ph2 = head2.InsertParagraph("", false);
     47 
     48                       ph2.InsertPicture(pic, 0);
     49                       ph2.InsertText("                                      真诚为您服务");
     50                       ph2.AppendLine();
     51 
     52                       Paragraph phfirst = hfirst.InsertParagraph("", false);
     53 
     54                       phfirst.InsertPicture(pic, 0);
     55                       phfirst.UnderlineColor(System.Drawing.Color.Yellow);
     56                       phfirst.InsertText("                                      真诚为您服务");
     57                       phfirst.AppendLine();
     58                       #endregion
     59 
     60                       #region 生成文档中内容
     61                     
     62                       foreach (Paragraph pbody in document.Paragraphs)
     63                       {
     64                         var bookmarks= pbody.GetBookmarks();
     65                         foreach (Bookmark item in bookmarks)
     66                         {
     67                             switch (item.Name)
     68                             {
     69                             case "MerchantName": //商家名称
     70                                     item.Paragraph.ReplaceText("{MerchantName}", hsBookMark["MerchantName"].ToString());
     71                             break;
     72                             case "OperatingCommissioner"://运营专员
     73                             item.Paragraph.ReplaceText("{OperatingCommissioner}", hsBookMark["OperatingCommissioner"].ToString());
     74                             break;
     75                             case "OperatingTime"://运营时间
     76                             item.Paragraph.ReplaceText("{OperatingTime}",  hsBookMark["OperatingTime"].ToString());
     77                             break;
     78                             case "IPNUM"://IP流量
     79                             item.Paragraph.ReplaceText("{IPNUM}", "88"+hsBookMark["OperatingTime"].ToString());
     80                             break;
     81                             case "PVNUM"://PV
     82                             item.Paragraph.ReplaceText("{PVNUM}", "868" + hsBookMark["OperatingTime"].ToString());
     83                             break;
     84                             case "FKNUM"://feek
     85                             item.Paragraph.ReplaceText("{FKNUM}", "878" + hsBookMark["OperatingTime"].ToString());
     86                             break;116                             } 
    117                         }
    118                       }
    119                       List<Table> table = document.Tables;
    120                       Row newRow = table[1].InsertRow(1);
    121                       newRow.Cells[0].Paragraphs[0].InsertText("&&&&&&&hhhHHHH00000", false);
    122                       newRow.Cells[1].Paragraphs[0].InsertText("&&&&&&&hhhHHHH111111111111",false);
    123                      
    124                       #endregion
    125 
    126                       #region 生成页脚
    127                       document.AddFooters();//添加所有的页脚
    128                       Footers footers = document.Footers; //获取该文档所有的页脚
    129                       //获取文档第一页的页脚
    130                       Footer first = footers.first;
    131 
    132                       //获取奇数页的页脚
    133                       Footer odd = footers.odd;
    134                       ////设置不同页使用不同的页脚
    135                       document.DifferentFirstPage = true;
    136                       document.DifferentOddAndEvenPages = true;
    137                       //设置页脚的内容
    138                       Paragraph pf = first.InsertParagraph();
    139                       pf.Append("页脚内容替换成你的页脚内容");
    140 
    141                       Paragraph p2 = footers.even.InsertParagraph();
    142                       p2.Append("页脚内容替换成你的页脚内容");
    143 
    144                       Paragraph p3 = footers.odd.InsertParagraph();
    145                       p3.Append("页脚内容替换成你的页脚内容");
    146                       #endregion
    147 
    148                       document.SaveAs(savePath);
    149                   }
    150               }

     2.docx下载

  • 相关阅读:
    LeetCode 1275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
    LeetCode 307. 区域和检索
    LeetCode 1271 十六进制魔术数字 Hexspeak
    秋实大哥与花 线段树模板
    AcWing 835. Trie字符串统计
    Leetcode 216. 组合总和 III
    Mybatis 示例之 复杂(complex)属性(property)
    Mybatis 示例之 复杂(complex)属性(property)
    Mybatis 高级结果映射 ResultMap Association Collection
    Mybatis 高级结果映射 ResultMap Association Collection
  • 原文地址:https://www.cnblogs.com/linsu/p/4462245.html
Copyright © 2011-2022 走看看