C#转换Word或Excel文档为Html文件

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C#转换Word或Excel文档为Html文件

天方   2022-06-20 我要评论

这个是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:\1.xlsx", @"r:\1.html");
        var html = converter.Convert();
    }

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们