星空网 > 软件开发 > ASP.net

OpenXml Sdk 根据Word模板导出到word

一:Open

  Open

  Open

二:Open

  下载并安装Open

  OpenXml Sdk 根据Word模板导出到word

  官方SDK文档:

  OpenXml Sdk 根据Word模板导出到word

三:Open

  打开vs新建项目Open

      OpenXml Sdk 根据Word模板导出到word

  新建word模板(word模板.docx)

  OpenXml Sdk 根据Word模板导出到word

四:Open

  这里主要根据插入书签(BookMark)的方式来定位位置,打开word模板.docx分别在‘公司名称’和‘公司简介’中插入两个书签。

  OpenXml Sdk 根据Word模板导出到word

  OpenXml Sdk 根据Word模板导出到word

  InsertSimpleText方法:

  

/// <summary>    ///    /// </summary>    /// <param name="filepPath"></param>    /// <param name="Dictionary"></param>    public static void InsertSimpleText(string filepPath, Dictionary<string, string> dictionary)    {      using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepPath, true))      {        List<BookmarkStart> allBookmarkStart = wordprocessingDocument.MainDocumentPart.RootElement.Descendants<BookmarkStart>().ToList();        foreach (KeyValuePair<string, string> keyValuePair in dictionary)        {          foreach (BookmarkStart bookmarkStart in allBookmarkStart)          {            if (bookmarkStart.Name.Value == keyValuePair.Key)            {              InsertIntoBookmark(bookmarkStart,keyValuePair.Value);              break;            }          }        }      }    }

 /// <summary>    /// 更换书签单一文本内容    /// </summary>    /// <param name="bookmarkStart">书签</param>    /// <param name="text">书签内容文本</param>    private static void InsertIntoBookmark(BookmarkStart bookmarkStart, string text)    {      Open

  

五:Open

  根据步骤四中新建2个图片书签位置,具体代码如下:

  OpenXml Sdk 根据Word模板导出到word

  

 /// <summary>    ///    /// </summary>    /// <param name="filepPath"></param>    /// <param name="dictionary"></param>    public static void InsertImage(string filepPath, Dictionary<string, string> dictionary,byte [] byteArrary=null)    {      using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepPath, true))      {        List<BookmarkStart> allBookmarkStart = wordprocessingDocument.MainDocumentPart.RootElement.Descendants<BookmarkStart>().ToList();        {          foreach (KeyValuePair<string, string> keyValuePair in dictionary)          {            foreach (BookmarkStart bookmarkStart in allBookmarkStart)            {              if (bookmarkStart.Name.Value == keyValuePair.Key)              {                byte[] imageByte = Convert.FromBase64String(keyValuePair.Value);                MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;                ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);                Stream stream = new MemoryStream(imageByte);                if (byteArrary != null)                {                  stream = new MemoryStream(byteArrary);                }                imagePart.FeedData(stream);                AddImageToBody(wordprocessingDocument, wordprocessingDocument.MainDocumentPart.GetIdOfPart(imagePart), bookmarkStart);                break;              }            }          }        }      }    }

 /// <summary>    ///    /// </summary>    /// <param name="wordDoc"></param>    /// <param name="relationshipId"></param>    /// <param name="bookmarkStart"></param>    private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId, BookmarkStart bookmarkStart)    {      // Define the reference of the image.      var element =       new Drawing(        new Inline(         new Extent() { Cx = 4900000L, Cy = 3920000L }, // 调节图片大小         new EffectExtent()         {           LeftEdge = 0L,           TopEdge = 0L,           RightEdge = 0L,           BottomEdge = 0L         },         new DocProperties()         {           Id = (UInt32Value)1U,           Name = "Picture 1"         },         new DocumentFormat.Open

  

六:Open

  首先给已有表格插入书签

  OpenXml Sdk 根据Word模板导出到word

  

/// <summary>    ///    /// </summary>    /// <param name="filepPath"></param>    /// <param name="configModel"></param>    /// <param name="dataModelList"></param>    public static void InsertTable(string filepPath, ConfigModel configModel, List<TableDataModel> dataModelList)    {      using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepPath, true))      {        Body body = wordprocessingDocument.MainDocumentPart.Document.Body;        List<BookmarkStart> allBookmarkStart = wordprocessingDocument.MainDocumentPart.RootElement.Descendants<BookmarkStart>().ToList();        //通过索引获得table        //var table = body.Elements<DocumentFormat.Open

  最后源码下载地址:http://files.cnblogs.com/files/sunyj/Open


原标题:OpenXml Sdk 根据Word模板导出到word

关键词:xml

xml
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

又有新进展?卖家可以进入东南亚市场了:https://www.goluckyvip.com/news/71.html
退欧,英国将面临物流梦魇?:https://www.goluckyvip.com/news/710.html
Tiktok如何打造爆款视频?学会这8个技巧,让你的账号成为爆款:https://www.goluckyvip.com/news/7100.html
海外仓仓储代发专题之:童装仓储代发详解:https://www.goluckyvip.com/news/7101.html
海外仓仓储代发专题之:个护产品仓储代发详解:https://www.goluckyvip.com/news/7102.html
美国海外仓电商知识分享:wish哪些产品适合做海外仓吗? :https://www.goluckyvip.com/news/7103.html
川藏线自驾游要怎么走才比较划算呢?:https://www.vstour.cn/a/411240.html
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
我的浏览记录
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流