使用开源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系统下配置JDK环境变量
刚申请了阿里云,平时很少接触Linux,特此记录一下Linux系统下安装JDK的步骤. 1.进入usr:cd /usr: 2.创建java文件夹:mkdir java: 3.将下载好的文件拷贝至jav ...
- html4,xhtml,html5发展历史
SGML SGML 是一种很强大但很复杂的标记语言,HTML.XML 就是从中衍生出来的.SGML 的例子如下:<QUOTE TYPE="example"> typic ...
- maven工程使用spring-boot-devtools进行热部署,更改代码避免重启web容器
spring-boot-devtools 是一个为开发者服务的一个模块,其中最重要的功能就是自动应用代码更改到最新的App上面去.相关Blog: 点击打开链接 原理是在发现代码有更改之后,重新启动应用 ...
- PHP 中 define() 和 const 定义常量时的区别
自 PHP 5.3.0 起,有两种方式定义常量,使用 const 关键字或者 define() 函数: 1 2 const FOO = 'BAR'; define('FOO', 'BAR'); 这 ...
- nginx.conf 解释
http://snapshot.sogoucdn.com/websnapshot?ie=utf8&url=http%3A%2F%2Fwww.cszhi.com%2F20120513%2Fngi ...
- C# 必看书籍
C# in Depth:讲的是C#的东西.CLR via C#:讲的是运行时的东西.Framework Design Guideline:讲的是你要如何设计一个类库才能跟.NET浑然一体.——“赵三本 ...
- ftp put本地文件至ubuntu服务器报错
起因:我想把本地下载的安装包上传至服务器. 由于Mac的ftp图形化客户端还没找着合适的,就想着用命令也是一样的. 但是又进坑了. 下载能够正常运行: ftp> get 2.jpg /Users ...
- DDOS分布式拒绝服务
DDOS(分布式拒绝服务)概念 DDOS称为分布式拒绝服务,DDOS本是利用合理的请求伪造资源过载,导致服务不可用.比如一个停车场有100个停车位,当100个停车位都停满后,再有车想要进来.就必须要等 ...
- 油猴 greasemonkey 背景音乐 火狐 chrome 背景音乐
火狐,chrome背景音乐 http://www.w3school.com.cn/tags/tag_audio.asp js插入背景音乐,猴油脚本使用 var audio = document.cre ...
- Mui框架一 快捷键+基础知识点
1.折叠面板--mAccordion 2.数字角标-mBadges <h5>有底色</h5> <span class="mui-badge">灰 ...