工作中遇到这样一个需求,要求把选中的订单导出到一张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. Mac系统下配置Maven环境变量

    1.在官网下载Maven安装包,网址:https://maven.apache.org/download.cgi: 2.进入终端Terminal,验证JDK是否配置成功,输入java -version ...

  2. [poj2349]Arctic Network(最小生成树+贪心)

    Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17758   Accepted: 5646 D ...

  3. jQuery 移动端ajax请求列表数据,实现点击翻页效果(还有手势往下滑动翻页)。

    1 首先是html部分 <div class="content"> <div class="list"></div>  // ...

  4. PHP绘图

    创建图像的一般流程1.设定标头,告诉浏览器你要生成的绘图类型.2.创建一个图像区域,以后的操作都将基于此图像区域.3.在空白图像区域绘制填充背景.4.在背景上绘制图形轮廓输入文本.5.输出最终图形.6 ...

  5. mac OS.NE开发环境搭建

    合肥程序员群:49313181.    合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入,申请备注填写姓名+技术+工作年限) Q  Q:408365330     E-Mail:eg ...

  6. SharePoint Foundation 2013 安装出错

    前段时间装foundation 13的时候遇到这个问题.怀疑是Office的问题.然后找了一些资料,问题得到了解决 解决方案: 运行 regedit,删除注册表下的office的LicenseType ...

  7. Codeforces Round #373 (Div. 2) E. Sasha and Array

    题目链接 分析:矩阵快速幂+线段树 斐波那契数列的计算是矩阵快速幂的模板题,这个也没什么很多好解释的,学了矩阵快速幂应该就知道的东西= =这道题比较巧妙的在于需要用线段树来维护矩阵,达到快速查询区间斐 ...

  8. Could not load file or assembly or one of its dependencies. 试图加载格式不正确的程序。

    当我把编译好的程序托管到IIS下后,访问时出现了以下问题,服务器环境是IIS 7,操作系统 Windows server 2008 r2 64位. 出现上述问题的原因是,所加载的程序集中有32位的,也 ...

  9. DotnetBar在VS2010工具箱中不显示问题

    请参考:http://blog.csdn.net/yanbo710148546/article/details/7862819

  10. Redis常用命令入门4:集合类型

    集合类型 之前我们已经介绍过了最基本的字符串类型.散列类型.列表类型,下面我们一起学习一下集合类型. 集合类型也是体现redis一个比较高价值的一个类型了.因为Redis的集合类型,所以我们可以很容易 ...