当前位置: 代码网 > it编程>编程语言>Asp.net > net core实现htlm转word

net core实现htlm转word

2024年09月17日 Asp.net 我要评论
1、最开始用的spire.doc,但是spire.doc只能在windows上使用,部署到docker上无法使用,原因是spire.doc使用了库system.drawing.common2、 之后使

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)
                };

 

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com