Aspose.Words導出html到word

Aspose.Words導出html到word,這個是以word模板導出的,要建word書籤,然後爲每個書籤賦值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ayy.Export
{

    /// <summary>
    /// 導出爲word
    /// </summary>
    public class ExportWordHelpter
    {
        /// <summary>
        /// 導出html 到word
        /// </summary>
        /// <param name="info">待導出數據</param>
        /// <param name="wordFilePath">導出磁盤地址</param>
        /// <param name="fileTempPath">doc模板文件(含書籤)磁盤地址</param>
        public static void ExportHtmlToWord(Models.Veiw.ArticleWhere info, string wordFilePath, string fileTempPath)
        {
            //string fileTempPath = Server.MapPath("~/Templates/NCR.docx");
            //載入模板
            Aspose.Words.Document doc = new Aspose.Words.Document(fileTempPath);
            Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
 
            //標題
            builder.MoveToBookmark("title");
            builder.Write(info?.Title);        

            //書籤賦值,部門      
            builder.MoveToBookmark("department");
            builder.Write(info?.DeparetmentName);

            //書籤賦值,報送日期
            string date = string.Empty;
            if (info.SendTime.HasValue)
            {
                date = info.SendTime.Value.ToString("yyyy-MM-dd");
            }
            builder.MoveToBookmark("sendDate");
            builder.Write(date);

            //書籤賦值,上報人
            builder.MoveToBookmark("sendName");
            builder.Write(info?.ReviewerName);

            //書籤賦值,內容        
            string content = info?.Content ?? string.Empty;
            builder.MoveToBookmark("content");
 
            //string html = ReplaceHtmlTag(content);
            //FontSettings fontSettings = new FontSettings();
            //fontSettings.SetFontsFolder("方正小標宋簡體",true);
            //builder.Document.FontSettings = fontSettings;
            builder.InsertHtml(content);//含html的文章內容

            // doc.Save("E:/DownLoadWord/DocumentBuilder.InsertTableFromHtml Out.doc");
            doc.Save(wordFilePath);
        }
 

    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章