html格式转换word_html嵌入word

html格式转换word_html嵌入word1基于wps直接将页面信息下载成word文档1publicvoidtest()2{34WPS.Applicationwps=null;5try6{7…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

1 基于wps直接将页面信息下载成word文档

html格式转换word_html嵌入word
html格式转换word_html嵌入word

 1  public void test()
 2         {
 3 
 4             WPS.Application wps = null;
 5             try
 6             {
 7                 wps = new WPS.Application();
 8             }
 9             catch (Exception ex)
10             {
11                 return;
12             }
13             var httpurl = "http://www.baidu.com";
14             WPS.Document doc = wps.Documents.Open(httpurl, false, true);
15             string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
16             System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
17 
18             string saveFileName = "D:\\1.doc";
19             doc.SaveAs(saveFileName, WPS.WdSaveFormat.wdFormatDocument);
20 
21             doc.Close(WPS.WdSaveOptions.wdSaveChanges, WPS.WdOriginalFormat.wdWordDocument, WPS.WdRoutingSlipStatus.wdNotYetRouted);
22             wps.Quit(WPS.WdSaveOptions.wdSaveChanges, WPS.WdOriginalFormat.wdWordDocument, WPS.WdRoutingSlipStatus.wdNotYetRouted);
23         }

View Code

这种情况下载的word文档中,样式全乱了,当时参考资料为:http://lanhy2000.blog.163.com/blog/static/4367860820119198575552/

 

2 用数据流的形式将页面下载成word文档

     1>首先获取webUrl页面输出内容

html格式转换word_html嵌入word
html格式转换word_html嵌入word

 1  /// <summary>
 2         /// 获取weburl输出内容
 3         /// </summary>
 4         /// <param name="url">weburl</param>
 5         /// <returns>输出内容</returns>
 6         public static string GetPage(string url)
 7         {
 8             WebResponse result = null;
 9             try
10             {
11                 WebRequest req = WebRequest.Create(new Uri(url));
12                 result = req.GetResponse();
13 
14                 var receivedStream = result.GetResponseStream();
15                 var sr = new System.IO.StreamReader(receivedStream, GetEncoding(GetContentType(result.ContentType).FirstOrDefault().Key));
16                 return sr.ReadToEnd();
17             }
18             catch (Exception ex)
19             {
20                 return ex.Message;
21             }
22             finally
23             {
24                 //ensure that network resources are not wasted
25                 if (result != null)
26                     result.Close();
27             }
28         }

View Code

     2>然后下载生成成word文档

html格式转换word_html嵌入word
html格式转换word_html嵌入word

 1  //数据流的方式
 2         public void test2()
 3         {
 4             var pageString = Linkin.Toolkit.Utility.Network.GetPage("http://www.baidu.com");
 5             System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=result.doc");
 6             System.Web.HttpContext.Current.Response.ContentType = "application/ms-word";
 7             System.Web.HttpContext.Current.Response.Charset = "utf-8";
 8             System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
 9             Response.Write(pageString);
10             Response.End();
11         }

View Code

     3>生成后的word文档因为没有样式,所有稍微丑了一些,有待优化,如图

     html格式转换word_html嵌入word

 

3 基于office com控件,在模板中添加书签的形式,将数据写入word模板中并保存

     1>首先需要向工程中的“引用”加入Word类库的引用(如图)。我是Office 2007。其他版本可能略有不同。在COM里面。

          html格式转换word_html嵌入word

     2>用Word设计一个模板文档(后缀名*.doc)。(如图)

          html格式转换word_html嵌入word

      3>向模板中的需要显示动态内容的地方添加书签。具体方法是。光标落到欲插入内容的地方,选择菜单栏上的“插入”——〉“书签”。

           在我的模板中添加完书签的样子如图

           html格式转换word_html嵌入word

    4>保存这个已完成的模板到任意路径,例如 D://template.doc

    5>具体读取模板,添加数据,保存文件代码如下: 

html格式转换word_html嵌入word
html格式转换word_html嵌入word

 1 public static void ExportToWord()
 2         {
 3             object oMissing = System.Reflection.Missing.Value;
 4             //创建一个Word应用程序实例  
 5             Word._Application oWord = new Word.Application();
 6             //设置为不可见  
 7             oWord.Visible = false;
 8             //模板文件地址,这里假设在X盘根目录  
 9             object oTemplate = "D://template.doc";
10             //以模板为基础生成文档  
11             Word._Document oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
12             //声明书签数组  
13             object[] oBookMark = new object[5];
14             //赋值书签名  
15             oBookMark[0] = "beizhu";
16             oBookMark[1] = "xingming";
17             oBookMark[2] = "xingbie";
18             oBookMark[3] = "chushengriqi";
19             oBookMark[4] = "jiguan";
20           
21             //赋值任意数据到书签的位置  
22             oDoc.Bookmarks.get_Item(ref oBookMark[0]).Range.Text = "使用模板实现Word生成";
23             oDoc.Bookmarks.get_Item(ref oBookMark[1]).Range.Text = "李四";
24             oDoc.Bookmarks.get_Item(ref oBookMark[2]).Range.Text = "";
25             oDoc.Bookmarks.get_Item(ref oBookMark[3]).Range.Text = "1987.06.07";
26             oDoc.Bookmarks.get_Item(ref oBookMark[4]).Range.Text = "夕阳无限好/r/n只是近黄昏";
27 
28             string savePath = "D:\\1.doc";
29 
30 
31             object filename = savePath;
32 
33             oDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing,
34             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
35             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
36             ref oMissing, ref oMissing);
37             oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
38             //关闭word  
39             oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
40         }

View Code

     6>到此,保存word文件成功,如图

         html格式转换word_html嵌入word

 

4 基于WPS com控件,模板表格标志导出word文档,此方式和方式3比较相似

     1>首先需要向工程中的“引用”加入wps类库的引用(如图)。

          html格式转换word_html嵌入word

    2>在本地任意路径下面创建模板,并在模板中需要添加数据的地方表明标签,例如:D:\\Resume.doc   如图

        html格式转换word_html嵌入word

    3>C#代码控制模板,并填充数据

       

html格式转换word_html嵌入word
html格式转换word_html嵌入word

 1 //基于 wps com控件 表格标志 导出word
 2         public void test4()
 3         {
 4 
 5             WPS.Application wps = null;
 6             try
 7             {
 8                 wps = new WPS.Application();
 9             }
10             catch
11             {
12 
13             }
14             ////获取当前项目的路径
15             //string path = AppDomain.CurrentDomain.BaseDirectory;
16             //WPS.Document doc = wps.Documents.Open(path + "Templetes\\Resume.doc", false, true);
17             WPS.Document doc = wps.Documents.Open("D:\\Resume.doc", false, true);
18 
19             #region 基本信息
20             var titleTable = doc.Tables.Item(1);
21             titleTable.Cell(1, 2).Range.Text = titleTable.Cell(1, 2).Range.Text.Replace("{RealName}", "简历名称");
22             titleTable.Cell(2, 2).Range.Text = titleTable.Cell(2, 2).Range.Text.Replace("{ExpectJobCategory}", "期望职位");
23             titleTable.Cell(3, 2).Range.Text = titleTable.Cell(3, 2).Range.Text.Replace("{UpdateTime}", "更新时间");
24             #endregion
25 
26 
27             string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
28        System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
29             string serverPath = @"D:\" + filename + ".doc";
30             doc.SaveAs(serverPath, WPS.WdSaveFormat.wdFormatDocument);
31 
32             doc.Close(WPS.WdSaveOptions.wdSaveChanges, WPS.WdOriginalFormat.wdWordDocument, WPS.WdRoutingSlipStatus.wdNotYetRouted);
33             wps.Quit(WPS.WdSaveOptions.wdSaveChanges, WPS.WdOriginalFormat.wdWordDocument, WPS.WdRoutingSlipStatus.wdNotYetRouted);
34         }

View Code

    4>生成后的word文件 如图

       html格式转换word_html嵌入word

 

5 这种方法相对以上都比较复杂,先保存地址,以待研究

    http://www.cnblogs.com/kingteach/archive/2011/11/22/2258801.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/166519.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • struts2 FilterDispatcher 和 StrutsPrepareAndExecuteFilter 的区别

    struts2 FilterDispatcher 和 StrutsPrepareAndExecuteFilter 的区别转自:http://blog.csdn.net/s_ongfei/article/details/5811070FilterDispatcher是struts2.0.x到2.1.2版本的核心过滤器.! StrutsPrepareAndExecuteFilter是自2.1.3开始就替代了FilterDispatcher的.! 这样的改革当然是有好处的.! 为什么这么说.?应该知道如果我们自…

    2022年8月16日
    4
  • SIMD and Avx2

    SIMD and Avx2SIMD一条指令可以执行多个数据group的计算和输出。对于SIMD相对应的SISD.intel SSE2 ,AVX2,AVX-512假设有一个任务是统计字符串中每一个字符出现的次数,我们可以用128bit的SISD指令进行统计。每8个bit代表一个字符,所以只需要两个SIMD指令(movemask、popcount)。详细测试:#include<stdio.h>#include<thread>#defineINC_TO1000000//o

    2022年5月7日
    29
  • document cookie用法[通俗易懂]

    document cookie用法[通俗易懂]cookie概述曾经利用一个不变的框架来存储购物栏数据,而商品显示页面是不断变化的,尽管这样能达到一个模拟全局变量的功能,但并不严谨。例如在导航框架页面内右击,单击快捷菜单中的【刷新】命令,则所有的JavaScript变量都会丢失。因此,要实现严格的跨页面全局变量,这种方式是不行的,JavaScript中的另一个机制:cookie,则可以达到真正全局变量的要求。 cookie是浏

    2022年7月11日
    15
  • Lasso回归总结

    Lasso回归总结Ridge回归由于直接套用线性回归可能产生过拟合,我们需要加入正则化项,如果加入的是L2正则化项,就是Ridge回归,有时也翻译为岭回归。它和一般线性回归的区别是在损失函数上增加了一个L2正则化的项,和一个调节线性回归项和正则化项权重的系数α。损失函数表达式如下:J(θ)=1/2(Xθ−Y)T(Xθ−Y)+1/2α||θ||22其中α为常数系数,需要进行调优。||θ||2为L…

    2022年5月30日
    35
  • 解决mac连接不上星巴克wifi的问题

    解决mac连接不上星巴克wifi的问题在星巴克写我的第一篇博客,结果mbp连wifi的时候遇到无法跳转到星巴克wifi页的问题,搞了一会儿发现一个好办法。。。正好试写一下mbp(macbookpro)如何连上星巴克的wifi来练个手~问题描述wifi显示连接,chrome浏览器随便打开一个网页却无法跳转,并显示未连接到互联网safari浏览器显示有跳转,但是网页并不能加载出来解决方案1连接上星巴克wifi2打开sa…

    2022年6月17日
    1.1K
  • fprintf函数的的用法matlab_fwrite函数的用法

    fprintf函数的的用法matlab_fwrite函数的用法fprintf 简介   c/c++语言函数:fprintf 功能   传送格式化输出到一个文件中 用法   #include   intfprintf(FILE*stream,constchar*format,…);   fprintf()函数根据指定的format(格式)(格式)发送信息(参数)到由stream(流)指

    2022年10月19日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号