在.NET中将HTML转换为MS Word .doc的最佳方法是什么?
第三方组件?
答案 0 :(得分:1)
尝试使用Office Interop
private void _convetHTML2Doc( string FileNameUpload)
{
string filePath = Server.MapPath("~/htmlfile");
object missing = Type.Missing;
object FileName = @"D:\" + "\\" + FileNameUpload;
object readOnly = true;
m_word = new Application();
m_word.Documents.Open(ref FileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
string newfilename = filePath + "\\" + FileNameUpload.Replace(".html", ".doc");
object o_newfilename = newfilename;
object o_encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;
object o_format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
object o_endings = Microsoft.Office.Interop.Word.WdLineEndingType.wdCRLF;
m_word.ActiveDocument.SaveAs(ref o_newfilename, ref o_format, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref o_encoding, ref missing, ref missing, ref o_endings, ref missing);
m_word.Quit(ref missing, ref missing, ref missing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_word);
}
答案 1 :(得分:1)
MS警告不要在最终用户观看/监督/控制Word时自动化Word,例如:因为它可能会弹出一个消息框;因此,如果您想在服务器上执行此操作,那么第三方组件可能比自动化Word更好。
您还可以学习自己编写Word文档格式(这是一种文档化的XML格式),但是(学习和编写该格式)可能比您想要的更麻烦。
另外,请注意Word可以打开HTML:因此对于某些(可能是轻微的)范围,HTML已经是Word文档。