工作中遇到这样一个需求,要求把选中的订单导出到一张Word中(要求不能使用Com组件)
要求实现图如下
下面是代码实现 先引用 DocX
string tempName = Guid.NewGuid().ToString() + ".doc"; //word临时文件名
string serverPath = Path.Combine(Request.MapPath("/WordTemplate/"), tempName); //服务器保存路径
string LogoPath = Server.MapPath("~/js/images/logo_zhtx.png"); //logo
//--------------------------------------------------------------------
using (DocX docx = DocX.Create(serverPath))
{
Novacode.Image img = docx.AddImage(LogoPath); //logo
List<OrdersModel> orderList = business.GetAllOrdersGood(Ids); //获取所有订单信息
foreach (OrdersModel order in orderList)
{
Paragraph p = docx.InsertParagraph(); //插入段落
Picture pic = img.CreatePicture();
p.InsertPicture(pic, ); //在段落处加图片 //头部
docx.InsertParagraph(string.Format("订单号: {0} 订单时间:{1} 超 市:{2}/{3}", order.OrderNumber, order.CreateTime.ToString(), order.UserName, order.SupermarketName));
docx.InsertParagraph(string.Format("收货人:{0} 超市电话:{1} 超市地址:{2}", order.Linkman, order.Phone, order.ReceiptAddress));
docx.InsertParagraph("备注:" + order.Remark);
docx.InsertParagraph("");
int row = order.GoodsList.Count; //商品个数
int cloumn = ;
Table dt = docx.InsertTable(row + , cloumn); //创建表格
Border bor = new Border();
bor.Tcbs = Novacode.BorderStyle.Tcbs_single;
//表头
string[] str_Title = new string[] { "序号", "商品ID", "商品", "类型", "品牌", "包装规格", "价格(元)", "数量", "合计(元)" };
for (int i = ; i < cloumn; i++)
{
dt.Rows[].Height = 20d;
Cell cell = dt.Rows[].Cells[i];
//设置列宽度
switch (i)
{
case :
cell.Width = ;
break;
case :
cell.Width = ;
break;
case :
cell.Width = ;
break;
case :
cell.Width = ;
break;
case :
cell.Width = ;
break;
case :
cell.Width = ;
break;
case :
cell.Width = ;
break;
case :
cell.Width = ;
break;
case :
cell.Width = ;
break;
}
//填充表格颜色及绘制边框
cell.FillColor = System.Drawing.Color.LightGreen;
cell.Paragraphs[].Append(str_Title[i]).Alignment = Alignment.center;
cell.SetBorder(TableCellBorderType.Left, bor);
cell.SetBorder(TableCellBorderType.Right, bor);
cell.SetBorder(TableCellBorderType.Top, bor);
cell.SetBorder(TableCellBorderType.Bottom, bor);
} //表格内容
int SerialNumber = ; //表格序号
for (int r = ; r <= row; r++)
{
// dt.Rows[r].Height = 20d;
OrdersGoodModel model = order.GoodsList[r - ]; //商品对象
string specifications = model.Specifications + "*" + model.Scount + GetGoodInfo.GetGoodUnit(model.Unit); //规格
string[] str_content = new string[] { SerialNumber.ToString(), model.GoodsID.ToString(), model.Title, model.PropertyName, model.BrandName, specifications, model.GoodPrice.ToString(), model.Count.ToString(), model.SumPrice.ToString() };
for (int j = ; j < cloumn; j++)
{
Cell cell = dt.Rows[r].Cells[j];
string ss = str_content[j];
cell.Paragraphs[].Append(str_content[j]).Alignment = Alignment.center;
cell.SetBorder(TableCellBorderType.Left, bor);
cell.SetBorder(TableCellBorderType.Right, bor);
cell.SetBorder(TableCellBorderType.Top, bor);
cell.SetBorder(TableCellBorderType.Bottom, bor);
}
SerialNumber++;
}
SerialNumber = ;
//表尾
string TotalMsg = "小计: 商品总数: " + order.GoodsSum + " 合计金额: ¥ " + order.SumPrice + " 促销折扣(元): ¥0.00 应收款(元): ¥ " + order.SumPrice;
docx.InsertParagraph("");
docx.InsertParagraph(TotalMsg).Color(System.Drawing.Color.Blue);
docx.InsertParagraph("");
docx.InsertParagraph("业务员: 超市签字: ");
docx.InsertParagraph(string.Format("供货商: {0} 电话: {1} 地址: {2} ", order.Shop.ShopName, order.Shop.Phone, order.Shop.Address));
docx.InsertParagraph( companyInfo);
docx.InsertParagraph("打印时间: " + DateTime.Now.ToString());
docx.InsertParagraph("");
docx.InsertParagraph(new string('_', ));
docx.InsertParagraph(""); }
//保存
docx.SaveAs(serverPath);
下载DocX
/// <summary>
/// 下载Word
/// </summary>
/// <param name="Wordpath">docx路径</param>
/// <param name="WordName">文件名</param>
/// <returns></returns>
public ActionResult DownLoadWord(string Wordpath, string WordName)
{
string filePath = Wordpath;
if (System.IO.File.Exists(filePath))
{
byte[] fileContents = System.IO.File.ReadAllBytes(filePath);
System.IO.File.Delete(filePath); //删除服务器端文件
var fileStream = new MemoryStream(fileContents);
return File(fileStream, "application/ms-word", WordName);
}
else
{
return Content("");
} }