Aspose.Words关于域的应用

 

 


 

#region 通过word域去插入相应的数据
/// <summary>
/// 通过word域去进行指定位置替换数据
/// </summary>
public void WordHelpers() {
       string filepath = FileHelper.GetDateDir();
       FileHelper.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("/Upload/" + filepath + ""));
       string pathsrc =System.Web.HttpContext.Current.Server.MapPath("~\\Content\\word.docx");//word文件路径
       string[] filedName = new string[] { "Test", "Text1" };//域名
       object[] values = new object[] {"2","2"};//相应数据
       Document dm = new Document(pathsrc);
       dm.MailMerge.Execute(filedName, values);
       dm.Save(System.Web.HttpContext.Current.Server.MapPath("~\\Upload\\" + filepath + "\\" + Guid.NewGuid() +".doc" + ""));

}
#endregion


 

  对于上面这种既有要循环展示的数据又有固定展示的数据  循环列添加《TableStart:List》   《TableEnd:List》 
 


/// <summary>
/// 订单打印
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void BtnPrint_Click(object sender, EventArgs e)
{
     string FilePath = FileHelper.GetDateDir();//根据时间得到目录名
     FileHelper.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("/upload-file/order/Orders/"+ FilePath));//创建一个目录
     string OrderTemp = System.Web.HttpContext.Current.Server.MapPath("~\\upload-file\\detail\\order\\ScHt.docx");//获取模板
     Document dm = new Document(OrderTemp);
     string[] filedName = new string[] { "Buyer", "orderId", "ForderId",
     "TotalPirce", "BackPirce", "YfPirce", "Payment", "Delivery", "Receive", "CName" , "DateTime","ChPrice" };//对应的域名
     HttpCookie userName = Request.Cookies[Cookie.UserNameCookieName];
     Member member = new MemberBusiness().GetEntityByUserName(userName.Value);
     OrderBusiness orderBusiness = new OrderBusiness();
     Order ordermodel=orderBusiness.GetEntity(lOrderNo.Text);
     object[] values = new object[] { member.CusName,ordermodel.GroupOrderId,lOrderNo.Text,ordermodel.TotalMoney,ordermodel.DiscountMoney, (ordermodel.TotalMoney - ordermodel.Disco      untMoney).ToString(),
     lPaymentMethod.Text, lShoppingMethod.Text, lShoppingDate.Text, lReceiver.Text,ordermodel.CreateDate, MoneyConvertChinese.MoneyToChinese((ordermodel.TotalMoney - ordermodel.Dis      countMoney).ToString())};//相对应的值内容
     OrderItemBusiness orderItemBusiness = new OrderItemBusiness();
     string where = "order_id='" + lOrderNo.Text + "'";
     string fieldList = "afd_stkNo,product_id,price,quantity,total_money";
     DataTable table = orderItemBusiness.GetList(fieldList, "Id", false, 1, 20, where).Table;
     table.TableName = "List";//这边要注意的是datatable的名称一定要和域循环的名称相对应
     table.Columns.Add("ProName");
     com.eshop.www.BLL.ProductDetailBusiness bllProd = new ProductDetailBusiness();
     foreach (DataRow dr in table.Rows) {
     dr["ProName"]= bllProd.GetEntity(int.Parse(dr["product_id"].ToString())).ProductName;
     }

     dm.MailMerge.ExecuteWithRegions(table);//datatable列名要和循环内的域名称保持一致

     dm.MailMerge.Execute(filedName, values);
     string url = System.Web.HttpContext.Current.Server.MapPath("~\\upload-file\\order\\Orders\\" + FilePath + "\\" + Guid.NewGuid() + ".doc");
     dm.Save(url);
     Response.Clear();
     Response.Buffer = true;
     Response.Charset = "utf-8";
     Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(url, System.Text.Encoding.UTF8));
     Response.ContentType = "application/octet-stream";
     Response.WriteFile(url);
     Response.Flush();
     Response.Close();
     Response.End();
     dm.Clone();

     JavascriptHelper.Alert("打印成功!");

}

目前我只应用过这两种导出方式下面在添加一种方式只是简单测试一下


public void WordHelpers(DataTable dt)
{
    string filepath = FileHelper.GetDateDir();
    FileHelper.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("/Upload/" + filepath + ""));
    //创建字符输出流
    StreamWriter sw = new StreamWriter("~\\Upload\\" + filepath + "\\" + Guid.NewGuid() + ".doc" + "", true, System.Text.UnicodeEncoding.UTF8);
    //需要导出的内容
    string str = "";
    str += "<html><head><title>无标题文档</title></head><body>";
    str += "<div>部门表格</div>";
    str += "<table> <thead>";
    str += "<tr> <th>部门名称</th> <th> 添加时间 </th> <th> 介绍 </th> </tr>";
    str += " <tbody>";
    foreach (DataRow dr in dt.Rows)
    {
    str += "<tr>";
    str += "<td>" + dr[""] + "</td>";
    str += "</tr>";
    }
    str += " </tbody>";
    str += "</table></body></html>";
    //写入
    sw.Write(str);
    sw.Close();
    //Response.Clear();
    //Response.Buffer = true;
    //// this.EnableViewState = false;
    //Response.Charset = "utf-8";
    //Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
    //Response.ContentType = "application/octet-stream";
    //Response.WriteFile(dirpath + fileName);
    //Response.Flush();
    //Response.Close();
    //Response.End();
}

posted @ 2017-06-26 14:45  HueiFeng  阅读(1964)  评论(0编辑  收藏  举报