/// <summary> /// 根据word模板(书签)创建word-导出 /// </summary> /// <param name="context"></param> public void export_Word(HttpContext context) { string templatePath = context.Server.MapPath("../../../模板.docx");//word模板位置 Document doc = new Document(templatePath); //循环书签 foreach (Bookmark mark in doc.Range.Bookmarks) { if (mark != null) { switch (mark.Name) { case "UserName": mark.Text = "饶哈哈"; break; case "Sex": mark.Text = "男"; break; case "Photo": DocumentBuilder builder = new DocumentBuilder(doc); string imgPath = context.Server.MapPath("/Admin/666.jpg");//图片地址 if (File.Exists(imgPath)) { builder.MoveToBookmark("Photo"); builder.InsertImage(imgPath, Aspose.Words.Drawing.RelativeHorizontalPosition.Margin, 1, Aspose.Words.Drawing.RelativeVerticalPosition.Margin, 20, 100, 125, Aspose.Words.Drawing.WrapType.Square); //1:left、20:top、100:width、125:height } break; default: break; } } } string wordpath = context.Server.MapPath("/file/") + Guid.NewGuid() + ".docx";//word保存位置 doc.Save(wordpath, SaveFormat.Doc); //以字符流的形式下载文件 FileStream fs = new FileStream(wordpath, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); context.Response.ContentType = "application/vnd.ms-word;"; context.Response.Charset = "GB2312"; context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //通知浏览器下载文件而不是打开;对中文名称进行编码 context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("文件名", System.Text.Encoding.UTF8) + ".doc"); context.Response.BinaryWrite(bytes); context.Response.Flush(); context.Response.End(); }
根据word模板(书签)创建导出word
猜你喜欢
转载自www.cnblogs.com/raominghao/p/10529347.html
今日推荐
周排行