aspose 换行写_C# Aspose.Words 数据写入到word,模板样式复杂(转换指定内容并返回多张图片)...

该博客介绍了如何在C#中使用Aspose.Words库动态生成Word文档,替换模板中的占位符,并根据数据源填充内容。通过示例代码展示了如何读取模板文件,替换文本,以及在表格中插入和修改数据,最后将文档转换为多张图片。
摘要由CSDN通过智能技术生成

public ResultResponsePrintStudyRecords([FromBody]StudyInfo info)

{

ResultResponse ret = new ResultResponse();if (info.ProjectId <= 0)

{

ret.Flag= false;returnret;

}var project = ProjectContract.GetFilterProject(p => p.Id ==info.ProjectId).FirstOrDefault();if (project == null)

{

ret.Flag= false;returnret;

}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= newDocument(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, 2, true);if (tableRecord == null)

{var op = newImageSaveOptions(SaveFormat.Jpeg);

op.PrettyFormat= true;

ret.Result= new string[doc.PageCount];for (var page = 0; page < doc.PageCount; page++)

{

op.PageIndex=page;using (var ms = newMemoryStream())

{

doc.Save(ms, op);

ret.Result[page]= "data:image/jpeg;base64," +Convert.ToBase64String(ms.GetBuffer());

}

}

ret.Flag= false;returnret;

}if (studyRecords.Count() > 1)

{for (int i = 0; i < studyRecords.Count - 1; i++)

{//克隆指定行

Row clonedRow = (Row)tableRecord.LastRow.Clone(true);//在指定位置插入克隆行

tableRecord.AppendChild(clonedRow);

}

}var rowIndex = 0;

ZhuJian.Entity.TrainManage.StudyRecord studyRecord= null;for (int i = 0; i < studyRecords.Count; i++)

{

studyRecord=studyRecords[i];

rowIndex= i + 1;var row =tableRecord.Rows[rowIndex];var cell0 = row.Cells[0];

Paragraph pg0= newParagraph(doc);

pg0.AppendChild(new Run(doc, rowIndex + ""));

cell0.RemoveAllChildren();

cell0.AppendChild(pg0);var cell1 = row.Cells[1];

Paragraph pg1= newParagraph(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[2];

Paragraph pg2= newParagraph(doc);

pg2.AppendChild(newRun(doc, studyRecord.ResourceName));

cell2.RemoveAllChildren();

cell2.AppendChild(pg2);var cell3 = row.Cells[3];

Paragraph pg3= newParagraph(doc);

pg3.AppendChild(new Run(doc, studyRecord.IsFinish ? "1" : "0"));

cell3.RemoveAllChildren();

cell3.AppendChild(pg3);

}var option = newImageSaveOptions(SaveFormat.Jpeg);

option.PrettyFormat= true;

ret.Result= new string[doc.PageCount];for (var page = 0; page < doc.PageCount; page++)

{

option.PageIndex=page;using (var ms = newMemoryStream())

{

doc.Save(ms, option);

ret.Result[page]= "data:image/jpeg;base64," +Convert.ToBase64String(ms.GetBuffer());

}

}

ret.Flag= true;returnret;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值