public ActionResult ExpInfo(string json) { try { Operator curUser = OperatorProvider.Provider.Current(); string title = "安全网格"; //设置文件名 string fileName = curUser.OrganizeName + title + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc"; title = curUser.OrganizeName + "<br />" + title; json = HttpUtility.UrlDecode(json); //加载导出模板 Aspose.Words.Document doc = new Aspose.Words.Document(Server.MapPath("~/Resource/DocumentFile/html.docx")); Aspose.Words.DocumentBuilder db = new Aspose.Words.DocumentBuilder(doc); //导入到书签 db.MoveToBookmark("title"); db.InsertHtml(title); db.MoveToBookmark("HTML"); db.InsertHtml(json.Replace(@"\", "").Replace(@" ", "").TrimStart('"').TrimEnd('"')); doc.Save(Server.MapPath("~/Resource/Temp/" + fileName)); string path = Server.MapPath("~/Resource/Temp/" + fileName); return(Success("操作成功", fileName)); } catch (Exception ex) { return(Error(ex.Message)); } }
private void DocumentBuilder_4() { //create filename of pdf_file_tosave var word_file_tosave = @"d:\1.docx"; //new pdf doc //var word_doc = new Aspose.Words.Document(); Aspose.Words.DocumentBuilder builder1 = new Aspose.Words.DocumentBuilder(); builder1.InsertHtml("<H1>this is a testing!</H1>"); var rhp = new Aspose.Words.Drawing.RelativeHorizontalPosition(); var rvp = new Aspose.Words.Drawing.RelativeVerticalPosition(); builder1.InsertShape(Aspose.Words.Drawing.ShapeType.StraightConnector1, rhp, 10, rvp, 10, 300, 300, Aspose.Words.Drawing.WrapType.None); builder1.InsertShape(Aspose.Words.Drawing.ShapeType.CurvedConnector3, rhp, 10, rvp, 10, 200, 300, Aspose.Words.Drawing.WrapType.None); builder1.InsertShape(Aspose.Words.Drawing.ShapeType.CurvedConnector4, rhp, 10, rvp, 10, 100, 300, Aspose.Words.Drawing.WrapType.None); builder1.InsertShape(Aspose.Words.Drawing.ShapeType.CurvedConnector5, rhp, 10, rvp, 10, 50, 300, Aspose.Words.Drawing.WrapType.None); //save try { //word_doc.Save(word_file_tosave, Aspose.Words.SaveFormat.Docx); builder1.Document.Save(word_file_tosave, Aspose.Words.SaveFormat.Docx); MessageBox.Show("Save Successfully!"); //pdfDoc.Dispose(); Process.Start(word_file_tosave); } catch { MessageBox.Show("Faild to Save!"); } }
public ActionResult Export(string keyValue, string title) { try { string sql = "select itemname,measures,deptname,dutyuser,to_char(plandate,'yyyy-mm-dd') plandate,to_char(realitydate,'yyyy-mm-dd') realitydate,checkuser,result,remark from jt_checkitems where checkid='" + keyValue + "' order by sortcode asc"; DataTable data = new DepartmentBLL().GetDataTable(sql); data.TableName = "T"; Aspose.Words.Document doc = new Aspose.Words.Document(Server.MapPath("~/Resource/DocumentFile/安全检查导出模板.doc")); Aspose.Words.DocumentBuilder db = new Aspose.Words.DocumentBuilder(doc); db.MoveToBookmark("title"); db.InsertHtml("<font style='font-size:21px;font-weight:bold;text-decoration:underline;'>" + title + "</font>"); doc.MailMerge.ExecuteWithRegions(data); HttpResponse resp = System.Web.HttpContext.Current.Response; doc.Save(resp, title + "安全检查问题汇总表.doc", Aspose.Words.ContentDisposition.Attachment, Aspose.Words.Saving.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Doc)); return(Success("操作成功")); } catch (Exception ex) { return(Error(ex.Message)); } }
/// <summary> /// 使用Response,将HTML字符串导出word /// 作者:佳烽 /// 日期:2013-11-8 /// 修改:DeaKen 20151230 /// </summary> /// <param name="html">html字符串或文本</param> /// <param name="tempFileName">生成临时文件路径</param> /// <param name="responseFileName">用户下载默认文件名</param> /// <param name="isHTML">是否包含了HTML字符串</param> /// <param name="isDel">是否删除临时文件</param> public static void HTMLToWord(string html, string tempFileName, string responseFileName, bool isHTML, bool isDel) { try { //锁 if (System.Threading.Monitor.TryEnter(ExportWordLock.HTMLToWordLock)) { Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(); //如果是插入html if (isHTML) { builder.InsertHtml(html); } else //其它文本 { builder.Writeln(html); } //保存到临时文件 builder.Document.Save(tempFileName); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.Buffer = false; HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; //设置响应流报文(文件名称,格式) HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(responseFileName + ".doc", System.Text.Encoding.UTF8)); HttpContext.Current.Response.ContentType = "application/ms-word"; //将文件写入输出流 HttpContext.Current.Response.WriteFile(tempFileName); //是否需要删除临时文件 if (isDel) { if (System.IO.File.Exists(tempFileName)) { System.IO.File.Delete(tempFileName); } } HttpContext.Current.Response.End(); HttpContext.Current.Response.Flush(); //解锁 System.Threading.Monitor.Exit(ExportWordLock.HTMLToWordLock); } else { HttpContext.Current.Response.Write("<script type='text/javascript'>alert('有操作正在进行,请稍后重试!');</script>"); } } catch (Exception Ex) { //遇到异常后,需要及时解锁 System.Threading.Monitor.Exit(ExportWordLock.HTMLToWordLock); throw Ex; } }