`
jinyanhui2008
  • 浏览: 312442 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

C#将html导出到word(基于wps)

    博客分类:
  • C#
阅读更多

由于客户需要,我们需要实现将网页导出到word中的功能,在此过程中,尝试使用过openoffice、itext、wordapi等各种方法,都不尽如人意。openoffice导出的问题图片信息在word2007下看不到,itext导出嵌套表格格式会乱套、wordapi导出倒是正常,但是无法将图片信息一并导入到文件中。最后没有办法突发奇想用wps试试,没想到成功了。

在尝试之前因为不知道wps是否有相关的api或者com组件,事先致电了金山客服询问了相关事宜,经确认wps是提供com组件调用的,现在即附上导出代码供大家测试分享。

 

 WPS.Application wps = null;
        try
        {
            wps = new WPS.Application();
        }
        catch (Exception ex) {
            WriteLine(ex.Message);
            return "";
        }
        WPS.Document doc = wps.Documents.Open(httpUrl, false, true);
        string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
        System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();


        string serverPath = Server.MapPath("~/doc/");
        string savePath = serverPath + filename + ".doc";
        object saveFileName = savePath;
        doc.SaveAs(savePath, WPS.WdSaveFormat.wdFormatDocument);

        doc.Close(WPS.WdSaveOptions.wdSaveChanges, WPS.WdOriginalFormat.wdWordDocument, WPS.WdRoutingSlipStatus.wdNotYetRouted);
        wps.Quit(WPS.WdSaveOptions.wdSaveChanges, WPS.WdOriginalFormat.wdWordDocument, WPS.WdRoutingSlipStatus.wdNotYetRouted);

在此需要先引用wps的com组件,并且using WPS;使用还是非常方便的推荐大家使用.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics