这个是CodeProject上的一篇文章:Microsoft Interop API to convert the .doc, .docx, .dot, .dotx and .xls,.xlsx, .rtf to HTML。该文介绍了一种通过Microsoft office Interop library转换word或excel文档为html的方法,这里转录一下,以供更多需要的人参考。
要使用Microsoft office Interop library库,首先得在电脑上安装Office,然后添加如下三个com组件的引用:
Microsoft Office Excel library.Microsoft Office Word libraryMicrosoft Office object library作者编写了两个类DocToHtml和XlsToHtml用以转换Word和Excel文档。
public static IConverter Converter(string fullFilePath, string fileToSave)
{
switch (Path.GetExtension(fullFilePath).ToLower())
{
case ".doc":
case ".docx":
case ".dot":
case ".dotx":
case ".rtf":
return new DocToHtml { FileToSave = fileToSave, FullFilePath = fullFilePath };
case ".xls":
case ".xlsx":
return new XlsToHtml { FileToSave = fileToSave, FullFilePath = fullFilePath };
default:
throw new NotSupportedException();
}
}
使用方法如下:
static void Main(string[] args)
{
var converter = ConverterLocator.Converter(@"r:\1.xlsx", @"r:\1.html");
var html = converter.Convert();
}