aspose word导出表格
[HttpGet]
[Route("GetPurchaseItemWord")]
public IHttpActionResult Get_PurchaseItemWord(string id)
{
#region 文件流
var model = _purchaseRepository.Get(id);
var purchaseDetail = _purchasedetailRepository.GetDetails(model.id);
ItemPurchaseViewModel purchaseVM = new ItemPurchaseViewModel();
purchaseVM = Mapper.Map<im_purchase, ItemPurchaseViewModel>(model);
purchaseVM.itemList = purchaseDetail;
var predicateTrail = Predicates.Field<wf_flow_trail>(f => f.business_id, Operator.Eq, model.id);
var flow_trail = _trailRepository.GetList(predicateTrail).OrderBy(a => a.create_date).ToList();
IList<ISort> sort = new List<ISort>()
{
Predicates.Sort<wf_flow_node>(x => x.create_date)
};
var flow_node = _nodeRepository.GetList(Predicates.Field<wf_flow_node>(x => x.flow_id, Operator.Eq, "14e84dba-8546-46b0-989e-0ee1312345ad"), sort).ToList();
flow_node = flow_node.Where(a => a.id != "20").ToList();
purchaseVM.flow_node = Mapper.Map<List<wf_flow_node>, List<flow_node>>(flow_node); try
{
string Path = AppDomain.CurrentDomain.RelativeSearchPath + "\\" + "Templet";
Path = Path.Replace("\\bin\\", "\\");
string templatePath = Path + "\\impurchaseTml.doc"; //自己做好的word
Document WordDoc = new Document(templatePath); DocumentBuilder builder = new DocumentBuilder(WordDoc);
builder.MoveToBookmark("impurchase");
if (WordDoc.Range.Bookmarks["impurchase"] != null)
{
var table = builder.StartTable();
//设置文本垂直居中
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
//设置字体大小
Aspose.Words.Font f = builder.Font;
f.Size = 12;
builder.InsertCell();
builder.CellFormat.Width = 35; builder.Write("序号");
builder.InsertCell();
builder.CellFormat.Width = 100; builder.Write("名称");
builder.InsertCell();
builder.CellFormat.Width = 70; builder.Write("规格型号");
builder.InsertCell();
builder.CellFormat.Width = 40; builder.Write("数量");
builder.InsertCell();
builder.CellFormat.Width = 45; builder.Write("单价");
builder.InsertCell();
builder.CellFormat.Width = 70; builder.Write("总计");
builder.InsertCell();
builder.CellFormat.Width = 112; builder.Write("备注");
builder.EndRow();
decimal? money = 0;
foreach (var item in purchaseDetail)
{
builder.InsertCell();
builder.CellFormat.Width = 35; builder.Write((purchaseDetail.IndexOf(item)+1).ToString());
builder.InsertCell();
builder.CellFormat.Width = 100; builder.Write(item.name);
builder.InsertCell();
builder.CellFormat.Width = 70; builder.Write(item.specification != null ? item.specification : "");
builder.InsertCell();
builder.CellFormat.Width = 40; builder.Write(item.quantity.ToString());
builder.InsertCell();
builder.CellFormat.Width = 45; builder.Write(item.price.ToString());
builder.InsertCell();
builder.CellFormat.Width = 70; builder.Write((item.quantity*item.price).ToString());
builder.InsertCell();
builder.CellFormat.Width = 112; builder.Write(item.remark != null ? item.remark : "");
money += item.price * item.quantity;
builder.EndRow();
}
f.Size = 16;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.First;//合并单元格开始
builder.CellFormat.Width = 35; builder.Write("预计总额");
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 100;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.None; //合并单元格结束
builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.CellFormat.Width = 70; builder.Write(money.ToString());
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 40;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 45;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 70;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 112;
builder.EndRow();
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.CellFormat.Width = 35; builder.Write("使用说明");
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 100;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.CellFormat.Width = 70; builder.Write(model.remark);
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 40;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 45;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 70;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 112;
builder.EndRow(); builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.CellFormat.Width = 35; builder.Write("申请人");
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 100;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.CellFormat.Width = 70; builder.Write(model.create_user);
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 40;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.CellFormat.Width = 45; builder.Write("申请时间");
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 70;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.None;
builder.CellFormat.Width = 112; builder.Write(Convert.ToDateTime(model.create_date).ToString("yyyy年MM月dd日"));
builder.EndRow(); builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.CellFormat.Width = 35; builder.Write("申请部门");
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 100;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.CellFormat.Width = 70; builder.Write(model.dept_name);
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 40;
foreach (var nodeitem in purchaseVM.flow_node)
{
var trail= flow_trail.Where(a => a.next_node_id == nodeitem.next_node).OrderByDescending(a => a.create_date).FirstOrDefault();
var filepath = (AppDomain.CurrentDomain.RelativeSearchPath).Replace("\\bin", "\\");
if (nodeitem.name== "后勤部门意见")
{
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.CellFormat.Width = 45; builder.Write("部门意见");
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 70;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.None;
builder.CellFormat.Width = 112;
if (trail != null)
{
builder.Write(trail.approval != null ? trail.approval : "");
if (File.Exists(filepath + trail.sign_path))
{
builder.InsertImage(filepath + trail.sign_path, RelativeHorizontalPosition.Margin, 1, RelativeVerticalPosition.Margin, 1, 100, 40, WrapType.Square);//插入图片
}
}
builder.EndRow();
}
else
{
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.CellFormat.Width = 35; builder.Write(nodeitem.name);
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 100;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.CellFormat.Width = 70;
if (trail != null)
{
builder.Write(trail.approval != null ? trail.approval : "");
if (File.Exists(filepath + trail.sign_path))
{
builder.InsertImage(filepath + trail.sign_path, RelativeHorizontalPosition.Margin, 1, RelativeVerticalPosition.Margin, 1, 100, 40, WrapType.Square);
}
} builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 40;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 45;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 70;
builder.InsertCell(); builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.CellFormat.Width = 112;
builder.EndRow();
}
}
table.AutoFit(AutoFitBehavior.FixedColumnWidths);
builder.EndTable(); table.Alignment = TableAlignment.Center;
//表格居中、固定宽高
} string sPath = AppDomain.CurrentDomain.RelativeSearchPath + "\\" + "UploadFile";
sPath = sPath.Replace("\\bin\\", "\\");
var fileName = "物品采购单" + DateTime.Now.ToString("yyyy-MM-dd") + ".doc";
fileName = HttpUtility.UrlEncode(fileName);
string filePath = sPath + "\\" + fileName;
WordDoc.Save(filePath);
var stream = FileToStream(filePath);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = fileName;
return ResponseMessage(result); }
catch (Exception e)
{
//return new HttpResponseMessage(HttpStatusCode.NoContent);
return Json(new { success = false, message = "导出有误,请联系管理员!" });
}
#endregion
}
aspose word导出表格的更多相关文章
- Aspose.Word 输出表格后空格字符丢失的解决方法
将datatable输出到word,执行如下代码. doc.MailMerge.ExecuteWithRegions(outDt); 执行完后发现第一列含前缀空格的字段值,空格字符被自动清除了. 解决 ...
- Aspose.Words导出图片 表格 Interop.Word
先定义一个WORD 模板, 然后替换文本.域 ,定位开始表格 文本和段落 // Specify font formatting Aspose.Words.Font font = builder.Fon ...
- 利用Aspose.Word控件和Aspose.Cell控件,实现Word文档和Excel文档的模板化导出
我们知道,一般都导出的Word文档或者Excel文档,基本上分为两类,一类是动态生成全部文档的内容方式,一种是基于固定模板化的内容输出,后者在很多场合用的比较多,这也是企业报表规范化的一个体现. 我的 ...
- C#通过模板导出Word(文字,表格,图片)
C#通过模板导出Word(文字,表格,图片) C#导出Word,Excel的方法有很多,这次因为公司的业务需求,需要导出内容丰富(文字,表格,图片)的报告,以前的方法不好使,所以寻找新的导出方法, ...
- 通过Aspose.Word和ZXING生成复杂的WORD表格
1.前言 这是我之前做的一个项目中要求的功能模块,它的需求是生成一个WORD文档,需要每页一个表格并且表格中需要插入文字.条形码和二维码等信息,页数可控制.具体的效果如下图所示: 可以看到有以下几点是 ...
- 利用Aspose.Word控件实现Word文档的操作
Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及输出,由于一般输出的内容比较正规化或者多数是表格居多,所以一般 ...
- 黄聪:利用Aspose.Word控件实现Word文档的操作(转)
撰写人:伍华聪 http://www.iqidi.com Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及 ...
- 关于ASPOSE.WORD使用上的一个小问题
最近实习期间负责了公司某个项目的一个功能模块里面的word导出功能,使用的是ASPOSE.WORD类库,但是经常导出时候会遇到图中的问题,大概意思就是两个表格不能跨在一起,调试了好几次还是没发现具体的 ...
- aspose.word 使用简单方法
aspose.word使用简单方法 概念介绍 使用aspose生成word报表步骤: 加载word模板 提供数据源 填充 加载模板 提供了4种重载方法 1 2 3 4 5 public Documen ...
- Aspose.Word 的常见使用(2018-12-26 更新版)
Aspose.Word 的常见使用 起因 因项目需要,而且使用html转Word的时候,样式不兼容问题,于是只能使用Aspose.Word通过代码生成.下面是通过DocumentBuilder来设计W ...
随机推荐
- 【SSO单点系列】(5):CAS4.0 之JDBC
deployerConfigContext.xml 修改对应添加以下代码 <bean id="SearchModeSearchDatabaseAuthenticationHandler ...
- jenkins-构建触发器之定时构建和轮询 SCM
前言 最近搭建自动化框架,跑自动化用例每次都得用手工点击构建任务,我们希望能每天固定时间跑,这样就不用管了,坐等收测试报告结果就行 定时构建语法 五颗星,中间用空格隔开 * * * * * 第一颗*表 ...
- css悬浮动画
1.Grow-Shadow /* Grow-Shadow */ .hvr-grow-shadow { display: inline-block; vertical-align: middle; -w ...
- popen函数和pyinstaller打包之 -w冲突
启发文章:https://www.jb51.net/article/184731.htm 之前我也是用到了os.popen()这个函数 1.os.popen(self.excel_path) 括号里 ...
- Lombok Requires Annotation Processing
当你打开一个项目启动的时候报这样的错误 这种错误 打开这个设置
- (0821) git 下拉code不稳
(1)红帽粘贴 vncconfig & (2)csh不稳定 解决办法: bash eval "$(ssh-agent -s)" ssh-add git pull 或者 g ...
- Python 安装使用cx_Oracle操作Oracle数据库
cx_Oracle 是一个能够访问 Oracle 数据库的 Python 扩展模块.它符合 Python 数据库 API 2.0 规范,并增加了相当多的内容和几个排除项.Python 连接使用Orac ...
- 使用ts二次封装storage(sessionStorage/localStorage)
export class LocalCache { setCache<T = any>(key: string, value: T): boolean; setCache<T = a ...
- 阻塞I/O,非阻塞I/O,同步I/O,异步I/O
根据应用程序是否阻塞自身运行分为: 阻塞I/O:是指应用程序在执行I/O操作后,如果没有获得响应, 就会阻塞当前线程,不能执行其他任务. 非阻塞I/O:是指应用程序在执行I/O操作后 ...
- java写 变量到文件
import java.io.BufferedReader; import java.io.File;import java.io.FileReader; import java.io.IOExcep ...