aspose.words实现html和word的相互转化

using Aspose.Words;
using Aspose.Words.Saving;
using System;
using System.IO;

namespace AsposeWordStu
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Word2Html();
            Html2Word();
            Console.ReadKey();
        }

        static void Html2Word()
        {
            var htmlContent = File.ReadAllText("word.html");
            var memoryStream = new MemoryStream(Convert.FromBase64String(""));
            var license = new Aspose.Words.License();
            license.SetLicense(memoryStream);
            var doc = new Aspose.Words.Document();
            doc.RemoveAllChildren();
            //css的行内样式、内部样式支持,外部样式应该是不支持未测试
            Aspose.Words.DocumentBuilder builder = new DocumentBuilder(doc);
            builder.InsertHtml(htmlContent);
            var now = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
            var docPath = now + ".docx";
            doc.Save(docPath);
            Console.WriteLine("存储成功");
            //存储为stream
            //var memoryStream = new MemoryStream();
 			//doc.Save(memoryStream, SaveOptions.CreateSaveOptions(SaveFormat.Docx));
        }
        
        static void Word2Html()
        {
            var docxPath = "001.docx";
            var memoryStream = new MemoryStream(Convert.FromBase64String(""));
            var license = new Aspose.Words.License();
            license.SetLicense(memoryStream);

            var doc = new Aspose.Words.Document(docxPath);
            var options = new HtmlSaveOptions();
            //控制文本输入表单字段如何保存为HTML或MHTML。默认值是false。
            options.ExportTextInputFormFieldAsText = true;
            //指定图像是以Base64格式保存为HTML、MHTML还是EPUB。默认值false
            options.ExportImagesAsBase64 = true;
            //指定保存为HTML或MHTML时是否写入DOCTYPE声明。
            //如果为true,则在根元素之前在文档中写入DOCTYPE声明。
            //默认值为false。保存到EPUB或HTML5(Aspose.Words.saving.HtmlVersion.HTML5)时
            //DOCTYPE声明始终被写入。
            options.ExportXhtmlTransitional = true;
            //转化为html5格式
            options.HtmlVersion = HtmlVersion.Html5;
            var now = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
            var htmlPath = now + ".html";
            doc.Save(htmlPath,options);
            Console.WriteLine("存储成功");
            //var memoryStream = new MemoryStream();
			//doc.Save(memoryStream, options);
        }
    }
}

你可能感兴趣的:(dotnet,html,word,前端,aspose,words)