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)
};
发表评论