使用开源DocX 生成Word
工作中遇到这样一个需求,要求把选中的订单导出到一张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的更多相关文章
- Docx 生成word文档
1.生成word代码 /// <summary> /// 生成word文档 /// </summary> /// <param name="tempPath&q ...
- Docx 生成word文档二
/// <summary> /// 生产word 文档 /// </summary> public class GenerateWord { /// <summary&g ...
- DocX操作word生成报表
1.DocX简介 1.1 简介 DocX是一个在不需要安装word的情况下对word进行操作的开源轻量级.net组件,是由爱尔兰的一个叫Cathal Coffey的博士生开发出来的.DocX使得操作w ...
- C#开源组件DocX处理Word文档基本操作(二)
上一篇 C#开源组件DocX处理Word文档基本操作(一) 介绍了DocX的段落.表格及图片的处理,本篇介绍页眉页脚的处理. 示例代码所用DocX版本为:1.3.0.0.关于版本的区别,请参见上篇,而 ...
- EasyOffice-.NetCore一行代码导入导出Excel,生成Word
简介 Excel和Word操作在开发过程中经常需要使用,这类工作不涉及到核心业务,但又往往不可缺少.以往的开发方式在业务代码中直接引入NPOI.Aspose或者其他第三方库,工作繁琐,耗时多,扩展性差 ...
- Aspose.Words简单生成word文档
Aspose.Words简单生成word文档 Aspose.Words.Document doc = new Aspose.Words.Document(); Aspose.Words.Documen ...
- POI生成WORD文档
h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...
- poi生成word文件
一.简介 对于poi来说,poi可以完成对word.excel.ppt的处理.word目前有两种文件格式,一种是doc后缀.另一种是docx后缀的.2007之前的版本都是doc后缀的,这种格式poi使 ...
- OpenXml操作Word的一些操作总结.无word组件生成word.
OpenXml相对于用MS提供的COM组件来生成WORD,有如下优势: 1.相对于MS 的COM组件,因为版本带来的不兼容问题,及各种会生成WORD半途会崩溃的问题. 2.对比填满一张30多页的WOR ...
随机推荐
- Linux系统下安装Mysql
原文档地址:http://www.itpub.net/thread-1766546-1-1.html 1.在Linux服务器上通过wget命令取得rpm包: wget –c http://dev.my ...
- 转载分享----一线交付眼中的为何"项目总是迟迟无法交付”
当初博主在一线交付BOSS系统中承担过TC角色 交付的路途很艰辛,加班到10点多或1点多第二天8点上班,还有通宵的日子 还有无数个问题从开始到关闭的周期,各种催人,各种掐架拉会,各种被甲方嫌弃 看到这 ...
- instanceof, isinstance,isAssignableFrom的区别
instanceof运算符 只被用于对象引用变量,检查左边的被测试对象 是不是 右边类或接口的 实例化.如果被测对象是null值,则测试结果总是false. 形象地:自身实例或子类实例 instanc ...
- red hat安装mysql二进制包
数据包命名格式解释 mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz 黑色粗体表示为包名称 蓝色表示linux系统二进制包 红色表示构架 1.上传mysql- ...
- webpack + react + es6, 并附上自己碰到的一些问题
最近一直在学react,react的基础部分已经学得差不多了,然而自己并没有做详细的记录,有兴趣的同志可以参考阮一峰老师的教程,个人觉得挺不错的,链接如下:https://github.com/rua ...
- <mvc:annotation-driven/>与<context:annotation-config/>的区别
在使用注解的方式配置SSM的时候一般会配置<mvc:annotation-driven/>与<context:annotation-config/>,有时候会对两者的概念有些区 ...
- ssh 登录
一.ssh登录过程 在实际开发中,经常使用ssh进行远程登录.ssh 登录到远程主机的过程包括: 版本号协商 密钥和算法协商 认证 交互 1.1 版本号协商阶段 (1) 服务端打开22端口(也可以为了 ...
- ROS学习笔记(七)——节点
NEW 1 #打开新的终端,以后不再注释$ sudo apt-get install ros-<distro>-ros-tutorials #下载一个教学用的仿真器$ roscore #运 ...
- mybatis-缓存1
以下转自:http://www.cnblogs.com/weidiao/p/5469046.html mybatis有两级缓存机制,一级缓存默认开启,可以在手动关闭:二级缓存默认关闭,可以手动开启.一 ...
- FTP多任务下载实现类
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...