用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

李浩    -  云代码空间

——

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

2022-08-13|293阅||

摘要:这个是codeproject上的一篇文章:microsoft interop api to convert the .doc, .docx, .dot, .dotx and .xls,.xlsx, .

这个是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();
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持云代码。
顶 0踩 0收藏
文章评论
    发表评论

    个人资料

    • 昵称: 李浩
    • 等级: 中级程序员
    • 积分: 445
    • 代码: 5 个
    • 文章: 14 篇
    • 随想: 0 条
    • 访问: 0 次
    • 关注

    最新提问

      站长推荐