在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
项目有遇到需要导出Word,在别人写好的基础上去修改样式,导出后发现样式不正确不整齐,于是采用模板的方式重新导出
1.模板word文件的制作,本人用office2013,在设计好需要的整个表格之后,在你需要替换的位置"插入"--书签 并命名,此命名需要在程序中进行替换 将做好的模板word文件放在程序目录下 2.引用Aspose.Words.dll 3.新建类WordOpAp.cs 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 }
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 }
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 }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论