首页 > 其他分享 >net core实现htlm转word

net core实现htlm转word

时间:2023-11-10 09:34:07浏览次数:31  
标签:core word MemoryStream msInp outData new net Document outMs

1、最开始用的Spire.Doc,但是Spire.Doc只能在windows上使用,部署到docker上无法使用,原因是Spire.Doc使用了库 System.Drawing.Common

2、 之后使用Aspose.Words,示例代码如下:

      public static void Run()
        {
            Document doc = new Document(@"D:\testhtml.html");
            doc.Save(@"D:\aspose_word.docx");
        }

可以在windows使用,也可以在linux上使用。不过免费版有水印

3、最后找到了一个库叫sautinsoft.document。可以实现功能,可以linux上使用,并且没有水印。示例代码如下

public static void ConvertFromStream()
        {
            // We need files only for demonstration purposes.
            // The conversion process will be done completely in memory.
            string inpFile = @"D:\testhtml2.html";
            string outFile = @"D:\sautinsoft_word2.docx";
            byte[] inpData = File.ReadAllBytes(inpFile);
            byte[] outData = null;

            using (MemoryStream msInp = new MemoryStream(inpData))
            {

                // Load a document.
                DocumentCore dc = DocumentCore.Load(msInp, new SautinSoft.Document.HtmlLoadOptions());

                // Save the document to DOCX format.
                using (MemoryStream outMs = new MemoryStream())
                {
                    dc.Save(outMs, new DocxSaveOptions());
                    outData = outMs.ToArray();
                }
                // Show the result for demonstration purposes.
                if (outData != null)
                {
                    File.WriteAllBytes(outFile, outData);
                    System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
                }
            }

 

html转doc给前端返回文件的base64示例代码如下:

          byte[] inpData = File.ReadAllBytes(htmlPath);
                using var msInp = new MemoryStream(inpData);
                DocumentCore dc = DocumentCore.Load(msInp, new SautinSoft.Document.HtmlLoadOptions());
                using MemoryStream outMs = new MemoryStream();
                dc.Save(outMs, new DocxSaveOptions());
                var outData = outMs.ToArray();

                var fileResult = new DownFileResult
                {
                    FileName = $"word_name.doc",
                    Base64Str = Convert.ToBase64String(outData)
                };

 

标签:core,word,MemoryStream,msInp,outData,new,net,Document,outMs
From: https://www.cnblogs.com/dayang12525/p/17823341.html

相关文章

  • .Net6 and VsCode CodeFirst开发和迁移使用
    ------------VsCode开发.net6----------------------------------干货如下:C#BaselanguagesupportforC#包含vscode的调试C#DevKitC#ExtensionsIntelliCodeIntelliCodeAPIUsageExamplesIntelliCodeCompletionsIntelliCodeforC#DevKitIntelliCodeInsi......
  • centos7.9安装kubernetes1.27.4版本
    ./etc/os-releasecolor(){RES_COL=60MOVE_TO_COL="echo-en\\033[${RES_COL}G"SETCOLOR_SUCCESS="echo-en\\033[1;32m"SETCOLOR_FAILURE="echo-en\\033[1;31m"SETCOLOR_WARNING="echo-en\\0......
  • netstat -ano | findstr :80 ,80端口被占用解决
    C:\Users\17482>netstat-ano|findstr:80TCP0.0.0.0:800.0.0.0:0LISTENING5312TCP0.0.0.0:8080.0.0.0:0LISTENING4688TCP192.168.43.14:56688106.39.203.229:80ESTABLISHED8052TCP192.168.43.14:56760124.236.26.167:80ESTABLISHED20068TCP[::]......
  • Introducing the core concepts of Kafka
    IntroductionI havelearntthekafkasince5years,IbelieveIlearndsomthing,Itisontimeforimprovingenglish.SoIdecidedtopickupmyblogs,towritingsomeconceptsofkafkaforconsolidatingmemory.Bytheway, makingmyenglishbetter.How......
  • Taurus .Net Core 微服务开源框架:Admin 插件【4-4】 - 配置管理-Mvc【Plugin-CORS 跨
    前言:继上篇:Taurus.NetCore微服务开源框架:Admin插件【4-3】-配置管理-Mvc【Plugin-MicroService微服务】本篇继续介绍下一个内容:系统配置节点:Mvc- Plugin- CORS 跨域界面:界面如下:跨域功能相关配置说明如下:1、CORS.IsEnable:是否启用跨域功能。仅需要开启该功......
  • 在ASP.NET中上传下载文件实例代码
    usingSystem.IO;//检查上传文件不为空if(File1.PostedFile!=null)  {       string nam=File1.PostedFile.FileName;   //取得文件名(抱括路径)里最后一个"."的索引   int i=nam.LastIndexOf(".");   //取得文件扩展名   string newext=nam......
  • Kubernetes日志采集Sidecar模式介绍
    Kubernetes(K8S)作为CNCF(cloudnativecomputingfoundation)的一个核心项目,背靠Google和Redhat的强大社区,近两年发展十分迅速,在成为容器编排领域中领导者的同时,也正在朝着PAAS底座标配的方向发展。日志采集方式日志作为任一系统不可或缺的部分,在K8S的官方文档中也介绍了多种的日......
  • Kubernetes(v1.21)简介
    Kubernetes(v1.21)简介第一章:背景1.1部署方式的演变传统部署时代:①在物理机服务器上运行应用程序。②无法为应用程序定义资源边界。③导致资源分配问题。如果在物理服务器上运行多个应用程序,则可能会出现一个应用程序占用大部分资源的情况下,会导致其它应用程序的......
  • m基于Yolov2和GoogleNet深度学习网络的疲劳驾驶检测系统matlab仿真,带GUI界面
    1.算法仿真效果matlab2022a仿真结果如下:  疲劳状态:    2.算法涉及理论知识概要      疲劳驾驶检测系统是一种基于深度学习网络的系统,它结合了Yolov2和GoogleNet模型,用于检测驾驶员的疲劳状态和人脸。疲劳驾驶检测系统主要包括两个部分:人脸检测和疲劳......
  • log4net 使用
    1:创建log4net配置文件log4net.config<?xmlversion="1.0"encoding="utf-8"?><configuration><configSections><sectionname="log4net"type="log4net.Config.Log4NetConfigurationSectionHandler,l......