这个是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 library

  • microsoft 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:.xlsx", @"r:.html");
        var html = converter.convert();
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。