1 基於wps直接將頁面信息下載成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 }
這種情況下載的word文檔中,樣式全亂了,當時參考資料為:http://lanhy2000.blog.163.com/blog/static/4367860820119198575552/
2 用數據流的形式將頁面下載成word文檔
1>首先獲取webUrl頁面輸出內容

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 }
2>然后下載生成成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 }
3>生成后的word文檔因為沒有樣式,所有稍微丑了一些,有待優化,如圖:
3 基於office com控件,在模板中添加書簽的形式,將數據寫入word模板中並保存
1>首先需要向工程中的“引用”加入Word類庫的引用(如圖)。我是Office 2007。其他版本可能略有不同。在COM里面。
2>用Word設計一個模板文檔(后綴名*.doc)。(如圖)
3>向模板中的需要顯示動態內容的地方添加書簽。具體方法是。光標落到欲插入內容的地方,選擇菜單欄上的“插入”——〉“書簽”。
在我的模板中添加完書簽的樣子如圖
4>保存這個已完成的模板到任意路徑,例如 D://template.doc
5>具體讀取模板,添加數據,保存文件代碼如下:

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 }
6>到此,保存word文件成功,如圖
4 基於WPS com控件,模板表格標志導出word文檔,此方式和方式3比較相似
1>首先需要向工程中的“引用”加入wps類庫的引用(如圖)。
2>在本地任意路徑下面創建模板,並在模板中需要添加數據的地方表明標簽,例如:D:\\Resume.doc 如圖
3>C#代碼控制模板,並填充數據

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 }
4>生成后的word文件 如圖
5 這種方法相對以上都比較復雜,先保存地址,以待研究
http://www.cnblogs.com/kingteach/archive/2011/11/22/2258801.html