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;
}