項目有遇到須要導出Word,在別人寫好的基礎上去修改樣式,導出後發現樣式不正確不整齊,因而採用模板的方式從新導出html
1.模板word文件的製做,本人用office2013,在設計好須要的整個表格以後,在你須要替換的位置"插入"--書籤 並命名,此命名須要在程序中進行替換瀏覽器
將作好的模板word文件放在程序目錄下app
2.引用Aspose.Words.dllide
3.新建類WordOpAp.csui
1 public class WordOpAp 2 { 3 4 static public object syncseed = new object(); 5 6 /// <summary> 7 /// 導出Word 8 /// </summary> 9 private Document WordDoc; 10 private bool isOpened = false;//判斷word模版是否被佔用 11 public void SaveAs(string strFileName, bool isReplace) 12 { 13 if (isReplace && File.Exists(strFileName)) 14 { 15 File.Delete(strFileName); 16 } 17 WordDoc.Save(strFileName); 18 } 19 20 //基於模版新建Word文件 21 public void OpenTempelte(string strTemppath) 22 { 23 WordDoc = new Document(strTemppath); 24 25 } 26 public void FillLable(string LabelId, string Content) 27 { 28 29 //打開Word模版 30 object bkmC = LabelId; 31 if (WordDoc.Range.Bookmarks[LabelId] != null) 32 { 33 WordDoc.Range.Bookmarks[LabelId].Text = Content; 34 } 35 } 36 public void ResponseOut(string filename) 37 { 38 WordDoc.Save(System.Web.HttpContext.Current.Response, filename, ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.Doc)); 39 } 40 41 public void OpenWebInline(string filename) 42 { 43 44 WordDoc.Save(System.Web.HttpContext.Current.Response, filename, ContentDisposition.Inline, SaveOptions.CreateSaveOptions(SaveFormat.Doc)); 45 } 46 47 /// <summary> 48 /// 設置打開密碼 49 /// </summary> 50 /// <param name="pwd"></param> 51 public void SetPassword(string pwd) 52 { 53 WordDoc.Protect(ProtectionType.ReadOnly, pwd); 54 } 55 56 /// <summary> 57 /// 不可編輯受保護,需輸入密碼 58 /// </summary> 59 /// <param name="Password"></param> 60 public void NoEdit(string Password) 61 { 62 WordDoc.Protect(ProtectionType.ReadOnly, Password); 63 } 64 65 public void ExportWord(string fileName, string wordname) 66 { 67 //輸出word 68 System.IO.FileInfo file = new System.IO.FileInfo(fileName); 69 System.Web.HttpContext.Current.Response.Clear(); 70 System.Web.HttpContext.Current.Response.Charset = "GB2312"; 71 System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; 72 // 添加頭信息,爲"文件下載/另存爲"對話框指定默認文件名 73 System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(wordname, System.Text.Encoding.UTF8)); 74 // 添加頭信息,指定文件大小,讓瀏覽器可以顯示下載進度 75 System.Web.HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString()); 76 // 指定返回的是一個不能被客戶端讀取的流,必須被下載 77 System.Web.HttpContext.Current.Response.ContentType = "application/ms-word"; 78 // 把文件流發送到客戶端 79 System.Web.HttpContext.Current.Response.WriteFile(file.FullName); 80 // 中止頁面的執行 81 System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest(); 82 83 } 84 85 public void ChangWordStyle(string markName, string content) 86 { 87 DocumentBuilder builder = new DocumentBuilder(this.WordDoc); 88 builder.MoveToBookmark(markName); 89 char[] chs = content.ToCharArray(); 90 int index = 0; 91 while (true) 92 { 93 if (index >= content.Length) 94 { 95 break; 96 } 97 if (chs[index] == '<') 98 { 99 if (content.Length > index + 3 && content.Substring(index + 1, 2).ToUpper() == "/P") 100 { 101 builder.Writeln(); 102 } 103 else if (content.Length > index + 7 && content.Substring(index + 1, 6).ToUpper() == "STRONG") 104 { 105 builder.Font.Bold = true; 106 } 107 else if (content.Length > index + 8 && content.Substring(index + 1, 7).ToUpper() == "/STRONG") 108 { 109 builder.Font.Bold = false; 110 } 111 else if (content.Length > index + 5 && content.Substring(index + 1, 4).ToUpper() == "BR /") 112 { 113 builder.Writeln(); 114 } 115 else if (content.Length > index + 3 && content.Substring(index + 1, 2).ToUpper() == "BR") 116 { 117 builder.Writeln(); 118 } 119 index = content.IndexOf(">", index) + 1; 120 121 } 122 else 123 { 124 if (content.IndexOf("<", index) == -1) 125 { 126 string text = content.Substring(index); 127 builder.Write(HttpUtility.HtmlDecode(text)); 128 index += text.Length; 129 } 130 else 131 { 132 string text = content.Substring(index, content.IndexOf("<", index) - index); 133 builder.Write(HttpUtility.HtmlDecode(text)); 134 index += text.Length; 135 } 136 } 137 } 138 139 } 140 141 }
4.導出word方法this
1 try 2 { 3 4 string path = Server.MapPath("../Generate/ExcelModel/"); //目錄地址 5 string templatePath = path + "zxprint.doc"; //本身作好的word 6 wop.OpenTempelte(templatePath); //打開模板文件 7 //如下爲添加內容到Word 8 ProjectSubmmit obj = (ProjectSubmmit)Session["ProjectSubmmit"];//此行是本身的實體層,用於獲取數據 9 10 string FileName = ""; 11 if (obj != null) 12 { 13 FileName = string.Format("{0}.doc", obj.ProjectNo); 14 wop.FillLable("ProjectNo", obj.ProjectNo); //替換word中指定書籤的位置 15 wop.FillLable("GroupNo", obj.Party_Index); 16 wop.FillLable("CreateTime", obj.MeetingTime.ToString("yyyy年M月d日")); 17 wop.ChangWordStyle("Description", obj.Description); //改變內容的格式, 18 } 19 wop.ResponseOut(FileName); //傳入的是導出文檔的文件名,導出文件 20 } 21 catch 22 { 23 24 }
5.附上一段清除html格式的方法spa
1 public static string NoHTML(string Htmlstring) 2 { 3 if (Htmlstring.Length > 0) 4 { 5 //刪除腳本 6 Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase); 7 //刪除HTML 8 Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase); 9 Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase); 10 Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase); 11 Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase); 12 Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase); 13 Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase); 14 Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase); 15 Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase); 16 Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase); 17 Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase); 18 Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase); 19 Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase); 20 Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase); 21 Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase); 22 Htmlstring = Regex.Replace(Htmlstring, @"“", "\"", RegexOptions.IgnoreCase);//保留【 「 】的標點符合 23 Htmlstring = Regex.Replace(Htmlstring, @"”", "\"", RegexOptions.IgnoreCase);//保留【 」 】的標點符合 24 Htmlstring.Replace("<", ""); 25 Htmlstring.Replace(">", ""); 26 Htmlstring.Replace("\r\n", ""); 27 Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim(); 28 } 29 return Htmlstring; 30 }