你的位置:首页 > ASP.net教程

[ASP.net教程]OpenXml Sdk 根据Word模板导出到word


一:Open

  Open

  Open

二:Open

  下载并安装Open

  

  官方SDK文档:

  

三:Open

  打开vs新建项目Open

      

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

  

四:Open

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

  

  

  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个图片书签位置,具体代码如下:

  

  

 /// <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

  首先给已有表格插入书签

  

  

/// <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