工作中遇到这样一个需求,要求把选中的订单导出到一张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("");
} }

  

使用开源DocX 生成Word的更多相关文章

  1. Docx 生成word文档

    1.生成word代码 /// <summary> /// 生成word文档 /// </summary> /// <param name="tempPath&q ...

  2. Docx 生成word文档二

    /// <summary> /// 生产word 文档 /// </summary> public class GenerateWord { /// <summary&g ...

  3. DocX操作word生成报表

    1.DocX简介 1.1 简介 DocX是一个在不需要安装word的情况下对word进行操作的开源轻量级.net组件,是由爱尔兰的一个叫Cathal Coffey的博士生开发出来的.DocX使得操作w ...

  4. C#开源组件DocX处理Word文档基本操作(二)

    上一篇 C#开源组件DocX处理Word文档基本操作(一) 介绍了DocX的段落.表格及图片的处理,本篇介绍页眉页脚的处理. 示例代码所用DocX版本为:1.3.0.0.关于版本的区别,请参见上篇,而 ...

  5. EasyOffice-.NetCore一行代码导入导出Excel,生成Word

    简介 Excel和Word操作在开发过程中经常需要使用,这类工作不涉及到核心业务,但又往往不可缺少.以往的开发方式在业务代码中直接引入NPOI.Aspose或者其他第三方库,工作繁琐,耗时多,扩展性差 ...

  6. Aspose.Words简单生成word文档

    Aspose.Words简单生成word文档 Aspose.Words.Document doc = new Aspose.Words.Document(); Aspose.Words.Documen ...

  7. POI生成WORD文档

    h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...

  8. poi生成word文件

    一.简介 对于poi来说,poi可以完成对word.excel.ppt的处理.word目前有两种文件格式,一种是doc后缀.另一种是docx后缀的.2007之前的版本都是doc后缀的,这种格式poi使 ...

  9. OpenXml操作Word的一些操作总结.无word组件生成word.

    OpenXml相对于用MS提供的COM组件来生成WORD,有如下优势: 1.相对于MS 的COM组件,因为版本带来的不兼容问题,及各种会生成WORD半途会崩溃的问题. 2.对比填满一张30多页的WOR ...

随机推荐

  1. logback 配置详解(一)——logger、root

    1.根节点<configuration>包含的属性 scan: 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true. scanPeriod: 设置监测配置文件 ...

  2. [Android] Google IAP unmaneged items服务器校验

    android IAP unmaneged items 服务器校验 当成功IAP以后, 会在google服务器记录此次购买的状态. 可以通过Google Play Android Developer ...

  3. Mac 下 PostgreSQL 的安装与使用

    在 mac 下,可以利用 homebrew 直接安装 PostgreSQL: 1 brew install postgresql -v 稍等片刻,PostgreSQL 就安装完成.接下来就是初始数据库 ...

  4. Strus2学习Exception处理集锦(一)

    2015-01-05 同学用dom4j写的一个更改节点文本的方法爆出这样一个错误: 代码: for(Iterator itr=root.elementIterator();itr.hasNext(); ...

  5. 【java基础】面向对象的三大特征---多态

    java的引用变量有两种类型,编译时类型和运行时类型,如果编译时类型和运行时类型不一致,就会出现多态. 多态分为: 运行时多态 java运行时系统根据调用该方法的实例的类型来决定选择调用哪个方法则被称 ...

  6. Tomcat APR & Linux Optimization

    一.简介 APR(Apache portable Run-time libraries)模式:简单理解,就是从操作系统级别解决异步IO问题,大幅度的提高服务器的处理和响应性能, 也是Tomcat运行高 ...

  7. Repeart控件使用

    对于刚刚接触到repearter控件,了解不深,现做个记录方便以后的查看 <asp:Repeater ID="rpt_OrderList" runat="serve ...

  8. 关于Js OOP编程 创建对象的一些理解。

    面向对象是一种对现实世界理解和抽象的方法,是计算机编程技术发展到一定阶段后的产物. 对象的含义          对象可以是汽车,人,动物,文字,表单或者任何存在的事物等等. 对象有: 属性----- ...

  9. iPhone 6 Screen Size and Web Design Tips

    Apple updated its iPhone a bit ago making the form factor much bigger. The iPhone 6 screen size is b ...

  10. JavaScript的面向对象编程(OOP)(一)——类

    在学习JavaScript面向对象的编程之前,需要知道,并了解面向对象的一些基本的常识.初学者中大多数都以为面向对象中,面向对象的编程是很重要和占据很大一部分精力.笔者在之前也是认为OOP是面向对象的 ...