使用开源DocX 生成Word
工作中遇到这样一个需求,要求把选中的订单导出到一张Word中(要求不能使用Com组件)
要求实现图如下
下面是代码实现 先引用 DocX
1 string tempName = Guid.NewGuid().ToString() + ".doc"; //word临时文件名 2 string serverPath = Path.Combine(Request.MapPath("/WordTemplate/"), tempName); //服务器保存路径 3 string LogoPath = Server.MapPath("~/js/images/logo_zhtx.png"); //logo 4 //-------------------------------------------------------------------- 5 using (DocX docx = DocX.Create(serverPath)) 6 { 7 Novacode.Image img = docx.AddImage(LogoPath); //logo 8 List<OrdersModel> orderList = business.GetAllOrdersGood(Ids); //获取所有订单信息 9 foreach (OrdersModel order in orderList) 10 { 11 Paragraph p = docx.InsertParagraph(); //插入段落 12 Picture pic = img.CreatePicture(); 13 p.InsertPicture(pic, 0); //在段落处加图片 14 15 //头部 16 docx.InsertParagraph(string.Format("订单号: {0} 订单时间:{1} 超 市:{2}/{3}", order.OrderNumber, order.CreateTime.ToString(), order.UserName, order.SupermarketName)); 17 docx.InsertParagraph(string.Format("收货人:{0} 超市电话:{1} 超市地址:{2}", order.Linkman, order.Phone, order.ReceiptAddress)); 18 docx.InsertParagraph("备注:" + order.Remark); 19 docx.InsertParagraph(""); 20 int row = order.GoodsList.Count; //商品个数 21 int cloumn = 9; 22 Table dt = docx.InsertTable(row + 1, cloumn); //创建表格 23 Border bor = new Border(); 24 bor.Tcbs = Novacode.BorderStyle.Tcbs_single; 25 //表头 26 string[] str_Title = new string[] { "序号", "商品ID", "商品", "类型", "品牌", "包装规格", "价格(元)", "数量", "合计(元)" }; 27 for (int i = 0; i < cloumn; i++) 28 { 29 dt.Rows[0].Height = 20d; 30 Cell cell = dt.Rows[0].Cells[i]; 31 //设置列宽度 32 switch (i) 33 { 34 case 0: 35 cell.Width = 200; 36 break; 37 case 1: 38 cell.Width = 350; 39 break; 40 case 2: 41 cell.Width = 700; 42 break; 43 case 3: 44 cell.Width = 450; 45 break; 46 case 4: 47 cell.Width = 500; 48 break; 49 case 5: 50 cell.Width = 600; 51 break; 52 case 6: 53 cell.Width = 200; 54 break; 55 case 7: 56 cell.Width = 200; 57 break; 58 case 8: 59 cell.Width = 200; 60 break; 61 } 62 //填充表格颜色及绘制边框 63 cell.FillColor = System.Drawing.Color.LightGreen; 64 cell.Paragraphs[0].Append(str_Title[i]).Alignment = Alignment.center; 65 cell.SetBorder(TableCellBorderType.Left, bor); 66 cell.SetBorder(TableCellBorderType.Right, bor); 67 cell.SetBorder(TableCellBorderType.Top, bor); 68 cell.SetBorder(TableCellBorderType.Bottom, bor); 69 } 70 71 //表格内容 72 int SerialNumber = 1; //表格序号 73 for (int r = 1; r <= row; r++) 74 { 75 // dt.Rows[r].Height = 20d; 76 OrdersGoodModel model = order.GoodsList[r - 1]; //商品对象 77 string specifications = model.Specifications + "*" + model.Scount + GetGoodInfo.GetGoodUnit(model.Unit); //规格 78 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() }; 79 for (int j = 0; j < cloumn; j++) 80 { 81 Cell cell = dt.Rows[r].Cells[j]; 82 string ss = str_content[j]; 83 cell.Paragraphs[0].Append(str_content[j]).Alignment = Alignment.center; 84 cell.SetBorder(TableCellBorderType.Left, bor); 85 cell.SetBorder(TableCellBorderType.Right, bor); 86 cell.SetBorder(TableCellBorderType.Top, bor); 87 cell.SetBorder(TableCellBorderType.Bottom, bor); 88 } 89 SerialNumber++; 90 } 91 SerialNumber = 1; 92 //表尾 93 string TotalMsg = "小计: 商品总数: " + order.GoodsSum + " 合计金额: ¥ " + order.SumPrice + " 促销折扣(元): ¥0.00 应收款(元): ¥ " + order.SumPrice; 94 docx.InsertParagraph(""); 95 docx.InsertParagraph(TotalMsg).Color(System.Drawing.Color.Blue); 96 docx.InsertParagraph(""); 97 docx.InsertParagraph("业务员: 超市签字: "); 98 docx.InsertParagraph(string.Format("供货商: {0} 电话: {1} 地址: {2} ", order.Shop.ShopName, order.Shop.Phone, order.Shop.Address)); 99 docx.InsertParagraph( companyInfo); 100 docx.InsertParagraph("打印时间: " + DateTime.Now.ToString()); 101 docx.InsertParagraph(""); 102 docx.InsertParagraph(new string('_', 80)); 103 docx.InsertParagraph(""); 104 105 } 106 //保存 107 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(""); } }