C# Aspose.Words 数据写入到word,模板样式复杂(转换指定内容并返回多张图片)
public ResultResponse<string[]> PrintStudyRecords([FromBody]StudyInfo info)
{
ResultResponse<string[]> ret = new ResultResponse<string[]>();
if (info.ProjectId <= )
{
ret.Flag = false;
return ret;
}
var project = ProjectContract.GetFilterProject(p => p.Id == info.ProjectId).FirstOrDefault();
if (project == null)
{
ret.Flag = false;
return ret;
}
var trainTarget = TrainTargetContract.GetFilterTrainTargets(t => t.Id == project.TrainTargetId).FirstOrDefault();
var studyRecords = StudyRecordsContract.GetList(s => s.ProjectId == info.ProjectId && s.IsFinish && !s.IsDelete).ToList();
Document doc;
using (var stream = System.IO.File.OpenRead(HttpContext.Current.Server.MapPath("/Content/学时记录.docx")))
{
doc = new Document(stream);
}
doc.Range.Replace(string.Format("${0}$", "Name"), LoginUser.Name, false, false);
doc.Range.Replace(string.Format("${0}$", "PersonalId"), LoginUser.PersonalId, false, false);
doc.Range.Replace(string.Format("${0}$", "Sex"), string.IsNullOrWhiteSpace(LoginUser.Sex) ? "" : LoginUser.Sex, false, false);
doc.Range.Replace(string.Format("${0}$", "PhoneNum"), string.IsNullOrWhiteSpace(LoginUser.Tel) ? "" : LoginUser.Tel, false, false);
doc.Range.Replace(string.Format("${0}$", "Company"), string.IsNullOrWhiteSpace(project.Company) ? "" : project.Company, false, false);
doc.Range.Replace(string.Format("${0}$", "TrainTargetName"), trainTarget != null ? trainTarget.TrainName : "", false, false);
doc.Range.Replace(string.Format("${0}$", "AllHours"), studyRecords.Count().ToString(), false, false);
//获取指定表格 2指的是表格下标,一共有三个,我需要改变内容的是第三个表格
Table tableRecord = (Table)doc.GetChild(NodeType.Table, , true);
if (tableRecord == null)
{
var op = new ImageSaveOptions(SaveFormat.Jpeg);
op.PrettyFormat = true;
ret.Result = new string[doc.PageCount];
for (var page = ; page < doc.PageCount; page++)
{
op.PageIndex = page;
using (var ms = new MemoryStream())
{
doc.Save(ms, op);
ret.Result[page] = "data:image/jpeg;base64," + Convert.ToBase64String(ms.GetBuffer());
}
}
ret.Flag = false;
return ret;
}
if (studyRecords.Count() > )
{
for (int i = ; i < studyRecords.Count - ; i++)
{
//克隆指定行
Row clonedRow = (Row)tableRecord.LastRow.Clone(true);
//在指定位置插入克隆行
tableRecord.AppendChild(clonedRow);
}
}
var rowIndex = ;
ZhuJian.Entity.TrainManage.StudyRecord studyRecord = null;
for (int i = ; i < studyRecords.Count; i++)
{
studyRecord = studyRecords[i];
rowIndex = i + ;
var row = tableRecord.Rows[rowIndex]; var cell0 = row.Cells[];
Paragraph pg0 = new Paragraph(doc);
pg0.AppendChild(new Run(doc, rowIndex + ""));
cell0.RemoveAllChildren();
cell0.AppendChild(pg0); var cell1 = row.Cells[];
Paragraph pg1 = new Paragraph(doc);
pg1.AppendChild(new Run(doc, studyRecord.StartTime.ToString("yyyy/MM/dd") + "-" + (studyRecord.EndTime.HasValue ? studyRecord.EndTime.Value.ToString("yyyy/MM/dd") : "")));
cell1.RemoveAllChildren();
cell1.AppendChild(pg1); var cell2 = row.Cells[];
Paragraph pg2 = new Paragraph(doc);
pg2.AppendChild(new Run(doc, studyRecord.ResourceName));
cell2.RemoveAllChildren();
cell2.AppendChild(pg2); var cell3 = row.Cells[];
Paragraph pg3 = new Paragraph(doc);
pg3.AppendChild(new Run(doc, studyRecord.IsFinish ? "" : ""));
cell3.RemoveAllChildren();
cell3.AppendChild(pg3);
}
var option = new ImageSaveOptions(SaveFormat.Jpeg);
option.PrettyFormat = true;
ret.Result = new string[doc.PageCount];
for (var page = ; page < doc.PageCount; page++)
{
option.PageIndex = page;
using (var ms = new MemoryStream())
{
doc.Save(ms, option);
ret.Result[page] = "data:image/jpeg;base64," + Convert.ToBase64String(ms.GetBuffer());
}
}
ret.Flag = true;
return ret;
}

C# Aspose.Words 数据写入到word,模板样式复杂(转换指定内容并返回多张图片)的更多相关文章
- 将excel中的数据填入word模板中-VBA
首先将word模板中需要填写excel中数据的空白处用自己独特的字符串标记,比如 数据001 什么的.如下图: 这样,就可以用vba搜寻这些自己独特的标记来根据excel内容填充word了. 第 ...
- Python中 将数据插入到Word模板并生成一份Word
搬运出处: https://blog.csdn.net/DaShu0612/article/details/82912064
- c#读取Word模板,利用书签替换内容包括表格
//生成WORD程序对象和WORD文档对象 Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Inter ...
- Java将数据写入word文档(.doc)
Java可用org.apache.poi包来操作word文档.org.apache.poi包可于官网上下载,解压后各jar作用如下图所示: 可根据需求导入对应的jar. 一.HWPFDocument类 ...
- 读取word模板,填充数据后导出
一.需求说明 定期生成word报告,报告中含有文本.表格.图表等元素,依次获取进行替换,保留原有样式,生成新的word文档 二.引入依赖 <dependency> <groupId& ...
- java通过word模板生成word文档
介绍 上次公司项目需要一个生成word文档的功能,有固定的模板根据业务填充数据即可,由于从来没做过,项目也比较着急于是去网上找有没有合适的工具类,找了好几种,看到其中有freeMark模板生成比较靠谱 ...
- 把数据输出到Word (非插件形式)
项目开发过程中,我们要把数据以各种各样的形式展现给客户.把数据以文档的形式展现给客户相信是一种比较头疼的问题,如果没有好的方法会 使得我的开发繁琐,而且满足不了客户的需求.接下来我会通过两种开发方式介 ...
- 前端数据渲染及mustache模板引擎的简单实现
早期数据渲染的几种方式 在模板引擎没有诞生之前,为了用JS把数据渲染到页面上,诞生了一系列数据渲染的方式. 最最基础的,莫过于直接使用DOM接口创建所有节点. <div id="roo ...
- POI往word模板中写入数据
转: POI往word模板中写入数据 2018年03月24日 16:00:22 乄阿斗同學 阅读数:2977 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn ...
随机推荐
- Python错误提示:[Errno 24] Too many open files的分析与解决
背景 最近在工作中发现了一个错误,在执行多线程扫描脚本的时候频繁出现下面这个错误 HTTPConnectionPool(host=‘t.tips', port=80): Max retries exc ...
- 前端学习(三十九)移动端app(笔记)
移动端App 开发App的三种方式 Native App 原生 底层语言 java Android oc ...
- Sass-数据类型
Sass和JavaScript语言类似,也具有自己的数据类型,在Sass中包含一下几种数据类型 数字:如,1,2,13,10px; 字符串: 有引号字符串或无引号字符串,如,“foo”,"b ...
- H2database创建表
语法和sql server大同小异 create table users(id int primary key not null int identity, name varchar(20))
- BZOJ2143 飞飞侠 & [校内NOIP2018模拟20181026] 最强大脑
Time Limit: 50 Sec Memory Limit: 259 MB Description 飞飞国是一个传说中的国度,国家的居民叫做飞飞侠.飞飞国是一个N×M的矩形方阵,每个格子代表一个街 ...
- python tkinter的Label
from tkinter import * window=Tk() window.title("my first window") window.geometry("50 ...
- [原创] Delphi InputBox、InputQuery函数
Delphi InputBox.InputQuery函数 两个函数都是弹框提示输入信息 function InputQuery(const ACaption, APrompt: string; var ...
- POJ 3481 Double Queue (treap模板)
Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest ...
- AcWing 312. 乌龟棋 (简单DP)打卡
题目:https://www.acwing.com/problem/content/description/314/ 题意:有一段路,每个格子都有个价值,然后有m张卡牌,四种类型,走1,2,3,4步, ...
- CoffeeScript编写简单新闻页(仅UI)
CoffeeScript编写简单新闻页(仅UI) 1. 配置(在公司搭建好的环境下配置) omnisocials-backend/src/backend/modules/member/config/m ...