根据word模板(书签)创建导出word

本文介绍了一种使用Aspose.Words库根据预设的Word模板及书签来创建并导出Word文档的方法。通过C#代码实现了替换模板中书签位置的文本和图片,最终将修改后的文档以字符流形式下载到客户端。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/// <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();
}

 

转载于:https://www.cnblogs.com/raominghao/p/10529347.html

首先当然是定义word模板,在需要替换的地方加上标签。 菜单-插入-书签,输入属性名,如year,date,pic1,voList等等。 打印页面: 需要把打印的数据从后台取出,以单个vo(一个对象)为一组,或以voList(对象的列表集合)为一组 组织好页面上 再得到这些数据后进行替换。 数据组织形式如下: <div id="export2word"> <form id="singleVo" name="singleVo"> <textarea name="jcxcrs" style="display:none"><c:out value="${zywstjfxbgVO.jcxcrs }"/></textarea> <textarea name="xcjhl" style="display:none"><c:out value="${zywstjfxbgVO.xcjhl }"/></textarea> <textarea name="tbjcxcrs" style="display:none"><c:out value="${tbjcxcrs }"/></textarea> <textarea name="tptest" style="display:none">../zwgl/zw008-ZwMkjbxxCTRL-showWxytp.png?xh=3041</textarea> </form> <c:forEach var="mxvo" items="${jgList}" varStatus="s"> <form name="mxvoForm"> <!-- 注:这里的宽度设置为表格单元格宽度(厘米*100)--> <textarea name="tbjcmcrs" style="width:349;display:none"><c:out value="${mxvo.tbjcmcrs }"/></textarea> <textarea name="tbjcmcrsbl" style="width:270;display:none"><c:out value="${mxvo.tbjcmcrsbl }"/></textarea> <textarea name="tbjcxcrs" style="width:477;display:none"><c:out value="${mxvo.tbjcxcrs}"/></textarea> <textarea name="tbjcxcrsbl" style="display:none"><c:out value="${mxvo.tbjcxcrsbl }"/></textarea> </form> </c:forEach> </div> 使用: <input type="button" id="select2" name="select2" class="button" value="导出数据" onclick="print2doc();"> <script type="text/javascript" src="../public/scripts/export2word.js"></script> <script type="text/javascript"> function print2doc(){ //参数为模板(与页面的相对)路径 var word = new WordApp("test.doc"); //参数为form名,vo中需要添加的属性(为空时form里所有属性) var vo = word.getSingleVo("singleVo",["jcxcrs","xcjhl","tbjcxcrs"]); //var vo = word.getSingleVo("singleVo"); //组织成的图片vo var tpvo = word.getSingleVo("singleVo",["tptest"]); //参数为 form名,需要添加的属性(顺序为生成表格列的顺序,为空时form里的所有属性和顺序) var voList = word.getVoList("mxvoForm",["tbjcmcrs","tbjcmcrsbl","tbjczsrs"]); //var voList = word.getVoList("mxvoForm"); //替换普通书签 word.replaceBookmarkUsevo(vo); //替换图片书签 word.replaceBookmarkUsepicvo(tpvo); //替换书签jgList,画出表格形成多行数据。 word.replaceBookmarkUsevolist("jgList",voList); //文档可见 word.wordObj.visible=true; //word.closeApp(); } </script> 注意: 替换图片的值需要解释一下: 1.可以设为相对本页面的路径如../zbgl/abc.png 2.如果是输出流,则需要把请求输出流的url映射成以图片格式结尾的。如/.../abc.do?id=123换成/../abc.png?id=123 可以在web.xml里配一个servlet,如以*.png的请求转成.do的。如: public class PngDispatcherServlet extends HttpServlet { private static final long serialVersionUID = 6230740581031996144L; public void init() throws ServletException { } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //StringBuffer url = request.getRequestURL(); StringBuffer url = new StringBuffer(request.getRequestURI()); if(request.getQueryString() != null) { url.append('?'); url.append(request.getQueryString()); } String newUrl = url.toString().replaceAll(".png", ".do"); ServletContext sc = getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher(newUrl); //定向的页面 rd.forward(request, response); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值