后端接口
[HttpGet("PersonalInfoExport")]
public IActionResult PersonalInfoExport(int ID)
{
try
{
var model = _personalInfomationService.GetModel(ID);
string uploadPath = Utils.UploadPath + "/" + DateTime.Now.ToString("yyyyMMdd") + "/";
string filePath = _webhostEnvironment.WebRootPath + "/" + uploadPath;
var fileName = "干部信息表(" + model.Name + ")-" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".docx";
string fileFullName = filePath + fileName;
var savePath = "";
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
using (var stream = new FileStream(Path.Combine(filePath, fileName), FileMode.Create, FileAccess.Write))
{
XWPFDocument document = new XWPFDocument();
var title = "干部(职工)基本信息表";
document.SetParagraph(NpoiWordParagraphTextStyleHelper._.ParagraphInstanceSetting(document, title, true, 26, "方正小标宋_GBK", ParagraphAlignment.CENTER), 0);
#region 文档第一个表格对象实例
XWPFTable firstXwpfTable = document.CreateTable(4, 7);
firstXwpfTable.Width = 5250;
firstXwpfTable.SetColumnWidth(0, 700);
firstXwpfTable.SetColumnWidth(1, 750);
firstXwpfTable.SetColumnWidth(2, 750);
firstXwpfTable.SetColumnWidth(3, 750);
firstXwpfTable.SetColumnWidth(4, 800);
firstXwpfTable.SetColumnWidth(5, 750);
firstXwpfTable.SetColumnWidth(6, 750);
int row = 0;
firstXwpfTable.GetRow(row).Height = 30 * 20;
firstXwpfTable.GetRow(row).GetCell(0).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "姓 名", ParagraphAlignment.CENTER, 35, false));
firstXwpfTable.GetRow(row).GetCell(1).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, model.Name, ParagraphAlignment.CENTER, 35, false));
firstXwpfTable.GetRow(row).GetCell(2).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "性 别", ParagraphAlignment.CENTER, 35, false));
firstXwpfTable.GetRow(row).GetCell(3).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, GetSex(model.Sex), ParagraphAlignment.CENTER, 35, false));
firstXwpfTable.GetRow(row).GetCell(4).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "出生年月", ParagraphAlignment.CENTER, 35, false));
firstXwpfTable.GetRow(row).GetCell(5).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, GetDataTimeString(model.Birthday), ParagraphAlignment.CENTER, 35, false));
NpoiWordParagraphTextStyleHelper._.MergeVertically(firstXwpfTable, 6, 0, 2);
if (System.IO.File.Exists(_webhostEnvironment.WebRootPath + "/" + model.PhotoUrl))
{
FileStream gfs = null;
gfs = new FileStream(_webhostEnvironment.WebRootPath + "/" + model.PhotoUrl, FileMode.Open, FileAccess.Read);
#region 方法一
XWPFRun r = firstXwpfTable.GetRow(0).GetCell(6).Paragraphs.ToList().FirstOrDefault().CreateRun();
r.AddPicture(gfs, (int)PictureType.JPEG, "avatar.png", 1000000, 1400000);
CT_Inline inline = r.GetCTR().GetDrawingList()[0].inline[0];
inline.docPr.id = 1;
#endregion
#region 方法二
#endregion
gfs.Close();
}
firstXwpfTable.GetRow(row).GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
firstXwpfTable.GetRow(row).GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
firstXwpfTable.GetRow(row).GetCell(2).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
firstXwpfTable.GetRow(row).GetCell(3).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
firstXwpfTable.GetRow(row).GetCell(4).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
firstXwpfTable.GetRow(row).GetCell(5).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
row = 1;
firstXwpfTable.GetRow(row).Height = 30 * 20;
firstXwpfTable.GetRow(row).GetCell(0).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "民 族", ParagraphAlignment.CENTER, 35, false));
firstXwpfTable.GetRow(row).GetCell(1).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, GetSystemDataByIdstring(model.Nation), ParagraphAlignment.CENTER, 35, false));
firstXwpfTable.GetRow(row).GetCell(2).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "籍 贯", ParagraphAlignment.CENTER, 35, false));
firstXwpfTable.GetRow(row).GetCell(3).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, model.NativePlace, ParagraphAlignment.CENTER, 35, false));
firstXwpfTable.GetRow(row).GetCell(4).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "出 生 地", ParagraphAlignment.CENTER, 35, false));
firstXwpfTable.GetRow(row).GetCell(5).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, model.BirthPlace, ParagraphAlignment.CENTER, 35, false));
firstXwpfTable.GetRow(row).GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
firstXwpfTable.GetRow(row).GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
firstXwpfTable.GetRow(row).GetCell(2).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
firstXwpfTable.GetRow(row).GetCell(3).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
firstXwpfTable.GetRow(row).GetCell(4).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
firstXwpfTable.GetRow(row).GetCell(5).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
row = 2;
firstXwpfTable.GetRow(row).Height = 35 * 20;
var rdsj = new List<string>();
rdsj.Add("入 党");
rdsj.Add("时 间");
firstXwpfTable.GetRow(row).GetCell(0).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, rdsj, ParagraphAlignment.CENTER, 12, false));
firstXwpfTable.GetRow(row).GetCell(1).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, GetDataTimeString(model.TimeOfJoiningTheParty), ParagraphAlignment.CENTER, 12, false));
var cjgzsj = new List<string>();
cjgzsj.Add("参加工");
cjgzsj.Add("作时间");
firstXwpfTable.GetRow(row).GetCell(2).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, cjgzsj, ParagraphAlignment.CENTER, 12, false));
firstXwpfTable.GetRow(row).GetCell(3).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, GetDataTimeString(model.TimeToWork), ParagraphAlignment.CENTER, 12, false));
firstXwpfTable.GetRow(row).GetCell(4).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "健康状况", ParagraphAlignment.CENTER, 12, false));
firstXwpfTable.GetRow(row).GetCell(5).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, model.Health, ParagraphAlignment.CENTER, 12, false));
firstXwpfTable.GetRow(row).GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
firstXwpfTable.GetRow(row).GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
firstXwpfTable.GetRow(row).GetCell(2).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
firstXwpfTable.GetRow(row).GetCell(3).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
firstXwpfTable.GetRow(row).GetCell(4).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
firstXwpfTable.GetRow(row).GetCell(5).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
row = 3;
firstXwpfTable.GetRow(row).Height = 70 * 20;
firstXwpfTable.GetRow(row).Height = 120;
firstXwpfTable.GetRow(row).MergeCells(1, 6);
var asdf = new List<string>();
asdf.Add("职级或");
asdf.Add("职称");
firstXwpfTable.GetRow(row).GetCell(0).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, asdf, ParagraphAlignment.CENTER, 12, false));
var rankAndTitle = "";
var ZJ = GetSystemDataByIdstring(model.RankAndTitle_GWY);
var ZC = GetSystemDataByIdstring(model.RankAndTitle_SY);
if (!string.IsNullOrEmpty(ZJ))
{
rankAndTitle = ZJ;
if (!string.IsNullOrEmpty(ZC))
{
rankAndTitle += "/";
}
}
if (!string.IsNullOrEmpty(ZC))
{
rankAndTitle += ZC;
}
firstXwpfTable.GetRow(row).GetCell(1).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, rankAndTitle, ParagraphAlignment.LEFT, 24, false));
firstXwpfTable.GetRow(row).GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
firstXwpfTable.GetRow(row).GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
#endregion
#region 文档第二个表格对象实例
XWPFTable secoedXwpfTable = document.CreateTable(2, 5);
secoedXwpfTable.Width = 5250;
secoedXwpfTable.SetColumnWidth(0, 700);
secoedXwpfTable.SetColumnWidth(1, 750);
secoedXwpfTable.SetColumnWidth(2, 1500);
secoedXwpfTable.SetColumnWidth(3, 800);
secoedXwpfTable.SetColumnWidth(4, 1500);
row = 0;
secoedXwpfTable.GetRow(row).Height = 45 * 20;
var dfdjkdf = new List<string>();
dfdjkdf.Add("学 历");
dfdjkdf.Add("学 位");
secoedXwpfTable.GetRow(row).GetCell(0).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, dfdjkdf, ParagraphAlignment.CENTER, 24, false));
var qrzjy = new List<string>();
qrzjy.Add("全日制");
qrzjy.Add("教育");
secoedXwpfTable.GetRow(row).GetCell(1).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, qrzjy, ParagraphAlignment.CENTER, 24, false));
secoedXwpfTable.GetRow(row).GetCell(2).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, GetSystemDataByIdstring(model.FullTime_Education), ParagraphAlignment.LEFT, 24, false));
var byyxxjzy = new List<string>();
byyxxjzy.Add("毕业院校");
byyxxjzy.Add("系及专业");
secoedXwpfTable.GetRow(row).GetCell(3).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, byyxxjzy, ParagraphAlignment.CENTER, 24, false));
secoedXwpfTable.GetRow(row).GetCell(4).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, model.FullTime_University + "/" + model.FullTime_Profession, ParagraphAlignment.LEFT, 35, false));
secoedXwpfTable.GetRow(row).GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
secoedXwpfTable.GetRow(row).GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
secoedXwpfTable.GetRow(row).GetCell(3).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
row++;
secoedXwpfTable.GetRow(row).Height = 45 * 20;
secoedXwpfTable.GetRow(row).GetCell(0).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "", ParagraphAlignment.CENTER, 30, false));
secoedXwpfTable.GetRow(row).GetCell(1).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "在职教育", ParagraphAlignment.CENTER, 24, false));
secoedXwpfTable.GetRow(row).GetCell(2).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, GetSystemDataByIdstring(model.OnWork_Education), ParagraphAlignment.LEFT, 24, false));
secoedXwpfTable.GetRow(row).GetCell(3).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, byyxxjzy, ParagraphAlignment.CENTER, 24, false));
secoedXwpfTable.GetRow(row).GetCell(4).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, model.OnWork_University + "/" + model.OnWork_Profession, ParagraphAlignment.LEFT, 24, false));
secoedXwpfTable.GetRow(row).GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
secoedXwpfTable.GetRow(row).GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
secoedXwpfTable.GetRow(row).GetCell(3).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
NpoiWordParagraphTextStyleHelper._.MergeVertically(secoedXwpfTable, 0, 0, row);
#endregion
#region 文档第三个个表格对象实例
XWPFTable thirdXwpfTable2 = document.CreateTable(1, 2);
thirdXwpfTable2.Width = 5250;
thirdXwpfTable2.SetColumnWidth(0, 1450);
thirdXwpfTable2.SetColumnWidth(1, 3800);
row = 0;
thirdXwpfTable2.GetRow(row).Height = 30 * 20;
thirdXwpfTable2.GetRow(row).GetCell(0).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "现 任 职 务", ParagraphAlignment.CENTER, 35, false));
thirdXwpfTable2.GetRow(row).GetCell(1).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, GetSystemDataByIdstring(model.Duty), ParagraphAlignment.LEFT, 35, false));
thirdXwpfTable2.GetRow(row).GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
thirdXwpfTable2.GetRow(row).GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
XWPFTable thirdXwpfTable = document.CreateTable(3, 7);
thirdXwpfTable.Width = 5250;
thirdXwpfTable.SetColumnWidth(0, 700);
thirdXwpfTable.SetColumnWidth(1, 750);
thirdXwpfTable.SetColumnWidth(2, 750);
thirdXwpfTable.SetColumnWidth(3, 750);
thirdXwpfTable.SetColumnWidth(4, 800);
thirdXwpfTable.SetColumnWidth(5, 750);
thirdXwpfTable.SetColumnWidth(6, 750);
row = 0;
thirdXwpfTable.GetRow(row).Height = 210 * 20;
var jdkfd = new List<string>();
jdkfd.Add("简");
jdkfd.Add("");
jdkfd.Add("");
jdkfd.Add("");
jdkfd.Add("历");
thirdXwpfTable.GetRow(row).GetCell(0).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, jdkfd, ParagraphAlignment.CENTER, 24, false));
thirdXwpfTable.GetRow(row).GetCell(1).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, model.Resume, ParagraphAlignment.LEFT, 24, false));
thirdXwpfTable.GetRow(row).MergeCells(1, 6);
thirdXwpfTable.GetRow(row).GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
var Rewardspunishments = new List<string>();
foreach (var item in model.PersonalRewardsAndPunishments)
{
Rewardspunishments.Add(GetDataTimeString(item.YearOrDate) + " " + item.Infos);
}
row++;
thirdXwpfTable.GetRow(row).Height = 160 * 20;
var jcqk = new List<string>();
jcqk.Add("奖");
jcqk.Add("惩");
jcqk.Add("情");
jcqk.Add("况");
thirdXwpfTable.GetRow(row).GetCell(0).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, jcqk, ParagraphAlignment.CENTER, 24, false));
thirdXwpfTable.GetRow(row).GetCell(1).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, Rewardspunishments, ParagraphAlignment.LEFT, 24, false));
thirdXwpfTable.GetRow(row).MergeCells(1, 6);
thirdXwpfTable.GetRow(row).GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
var PersonalAnnualAssessmentResults = new List<string>();
var PersonalResult = _systemTypeService.GetSystemTypeList("3c2114dc-f042-4c70-b9db-b532f56d89d2");
foreach (var item in model.PersonalAnnualAssessmentResults)
{
PersonalAnnualAssessmentResults.Add(item.Years + "年: " + PersonalResult.FirstOrDefault(p => p.id == item.Results)?.name + "");
}
row++;
thirdXwpfTable.GetRow(row).Height = 80 * 20;
var ndksjg = new List<string>();
ndksjg.Add("年度");
ndksjg.Add("考试");
ndksjg.Add("结果");
thirdXwpfTable.GetRow(row).GetCell(0).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, ndksjg, ParagraphAlignment.CENTER, 24, false));
thirdXwpfTable.GetRow(row).GetCell(1).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, PersonalAnnualAssessmentResults, ParagraphAlignment.LEFT, 24, false));
thirdXwpfTable.GetRow(row).MergeCells(1, 6);
thirdXwpfTable.GetRow(row).GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
#endregion
#region 文档第四个个表格对象实例
int fourth_rows = 7;
if (model.PersonalFamilyMembers.Count() >= fourth_rows)
{
fourth_rows = model.PersonalFamilyMembers.Count() + 1;
}
XWPFTable fourthXwpfTable = document.CreateTable(fourth_rows, 6);
fourthXwpfTable.Width = 5250;
fourthXwpfTable.SetColumnWidth(0, 700);
fourthXwpfTable.SetColumnWidth(1, 750);
fourthXwpfTable.SetColumnWidth(2, 750);
fourthXwpfTable.SetColumnWidth(3, 750);
fourthXwpfTable.SetColumnWidth(4, 800);
fourthXwpfTable.SetColumnWidth(5, 1500);
row = 0;
fourthXwpfTable.GetRow(row).Height = 30 * 20;
var jtzycyjzyshgx = new List<string>();
jtzycyjzyshgx.Add("家");
jtzycyjzyshgx.Add("庭");
jtzycyjzyshgx.Add("主");
jtzycyjzyshgx.Add("要");
jtzycyjzyshgx.Add("成");
jtzycyjzyshgx.Add("员");
jtzycyjzyshgx.Add("及");
jtzycyjzyshgx.Add("重");
jtzycyjzyshgx.Add("要");
jtzycyjzyshgx.Add("社");
jtzycyjzyshgx.Add("会");
jtzycyjzyshgx.Add("关");
jtzycyjzyshgx.Add("系");
fourthXwpfTable.GetRow(row).GetCell(0).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, jtzycyjzyshgx, ParagraphAlignment.CENTER, 6, false));
fourthXwpfTable.GetRow(row).GetCell(1).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "称 谓", ParagraphAlignment.CENTER, 30, false));
fourthXwpfTable.GetRow(row).GetCell(2).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "姓 名", ParagraphAlignment.CENTER, 30, false));
fourthXwpfTable.GetRow(row).GetCell(3).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "年龄", ParagraphAlignment.CENTER, 30, false));
fourthXwpfTable.GetRow(row).GetCell(4).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "政治面貌", ParagraphAlignment.CENTER, 30, false));
fourthXwpfTable.GetRow(row).GetCell(5).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "工 作 单 位 及 职 务", ParagraphAlignment.CENTER, 30, false));
fourthXwpfTable.GetRow(row).GetCell(0).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
fourthXwpfTable.GetRow(row).GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
fourthXwpfTable.GetRow(row).GetCell(2).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
fourthXwpfTable.GetRow(row).GetCell(3).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
fourthXwpfTable.GetRow(row).GetCell(4).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
fourthXwpfTable.GetRow(row).GetCell(5).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
var list_Political = _systemTypeService.GetSystemDataByGuid("c2cfd640-03e8-4498-906e-87e3a40d5ed5");
string plll = "";
foreach (var item in model.PersonalFamilyMembers)
{
row++;
fourthXwpfTable.GetRow(row).Height = 30 * 20;
plll = list_Political.FirstOrDefault(p => item.Political == p.value)?.label;
if (plll == null)
{
plll = "";
}
fourthXwpfTable.GetRow(row).GetCell(1).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, item.Appellation, ParagraphAlignment.CENTER, 24, false));
fourthXwpfTable.GetRow(row).GetCell(2).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, item.Name, ParagraphAlignment.CENTER, 24, false));
fourthXwpfTable.GetRow(row).GetCell(3).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "" + GetAgeByBirthdate(item.Birthday), ParagraphAlignment.CENTER, 24, false));
fourthXwpfTable.GetRow(row).GetCell(4).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, plll, ParagraphAlignment.CENTER, 24, false));
fourthXwpfTable.GetRow(row).GetCell(5).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, item.Unit + "/" + item.Duty, ParagraphAlignment.CENTER, 24, false));
fourthXwpfTable.GetRow(row).GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
fourthXwpfTable.GetRow(row).GetCell(2).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
fourthXwpfTable.GetRow(row).GetCell(3).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
fourthXwpfTable.GetRow(row).GetCell(4).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
fourthXwpfTable.GetRow(row).GetCell(5).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
}
for (int i = row; i < fourth_rows; i++)
{
fourthXwpfTable.GetRow(i).Height = 30 * 20;
fourthXwpfTable.GetRow(i).GetCell(1).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
fourthXwpfTable.GetRow(i).GetCell(2).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
fourthXwpfTable.GetRow(i).GetCell(3).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
fourthXwpfTable.GetRow(i).GetCell(4).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
fourthXwpfTable.GetRow(i).GetCell(5).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
}
NpoiWordParagraphTextStyleHelper._.MergeVertically(fourthXwpfTable, 0, 0, fourth_rows - 1);
#endregion
string checkTime = DateTime.Now.ToString("yyyy年MM月dd日");
document.SetParagraph(NpoiWordParagraphTextStyleHelper._.ParagraphInstanceSetting(document, $"时间: {checkTime}", false, 14, "宋体", ParagraphAlignment.RIGHT, true, ""), 1);
document.Write(stream);
savePath = uploadPath + fileName;
}
return Success(200, "导出成功", savePath);
}
catch (Exception e)
{
return Fail(201, e.Message);
}
}
前端js
// 导出调用的方法
exportbtn() {
// var id = this.form.id
var id = this.$route.query.id
// 请求后端接口
PersonalInfoExport({ ID: id }).then(response => {
if (response.code === 200) {
// 导出成功
var pathurl = process.env.VUE_APP_BASE_API + '/' + response.data
console.log(pathurl)
var link = document.createElement('a')
link.style.display = 'none'
link.href = pathurl
link.setAttribute('download', '干部信息表.docx')
document.body.appendChild(link)
link.click()
}
})
},
导出后的效果预览
一个实用的帮助类(NPOI的简单封装)
using NPOI.OpenXmlFormats.Wordprocessing;
using NPOI.XWPF.UserModel;
using System;
using System.Collections.Generic;
using System.Text;
namespace Infrastructure.Util
{
public class NpoiWordParagraphTextStyleHelper
{
private static NpoiWordParagraphTextStyleHelper _exportHelper;
public static NpoiWordParagraphTextStyleHelper _
{
get => _exportHelper ?? (_exportHelper = new NpoiWordParagraphTextStyleHelper());
set => _exportHelper = value;
}
public XWPFParagraph ParagraphInstanceSetting(XWPFDocument document, string fillContent, bool isBold, int fontSize, string fontFamily, ParagraphAlignment paragraphAlign, bool isStatement = false, string secondFillContent = "", string fontColor = "000000", bool isItalic = false)
{
XWPFParagraph paragraph = document.CreateParagraph();
paragraph.Alignment = paragraphAlign;
paragraph.VerticalAlignment = TextAlignment.CENTER;
XWPFRun xwpfRun = paragraph.CreateRun();
xwpfRun.IsBold = isBold;
xwpfRun.SetText(fillContent);
xwpfRun.FontSize = fontSize;
xwpfRun.IsItalic = isItalic;
xwpfRun.SetColor(fontColor);
xwpfRun.SetFontFamily(fontFamily, FontCharRange.None);
if (!isStatement) return paragraph;
XWPFRun secondXwpfRun = paragraph.CreateRun();
secondXwpfRun.IsBold = isBold;
secondXwpfRun.SetText(secondFillContent);
secondXwpfRun.FontSize = fontSize;
secondXwpfRun.IsItalic = isItalic;
secondXwpfRun.SetColor(fontColor);
secondXwpfRun.SetFontFamily(fontFamily, FontCharRange.None);
return paragraph;
}
public XWPFParagraph SetTableParagraphInstanceSetting(XWPFDocument document, XWPFTable table, string fillContent, ParagraphAlignment paragraphAlign, int textPosition = 24, bool isBold = false, int fontSize = 12, string fontColor = "000000", bool isItalic = false)
{
var para = new CT_P();
para.AddNewPPr().AddNewTextAlignment();
XWPFParagraph paragraph = new XWPFParagraph(para, table.Body);
paragraph.Alignment = paragraphAlign;
XWPFRun xwpfRun = paragraph.CreateRun();
xwpfRun.SetText(fillContent);
xwpfRun.FontSize = fontSize;
xwpfRun.SetColor(fontColor);
xwpfRun.IsItalic = isItalic;
xwpfRun.IsBold = isBold;
xwpfRun.SetFontFamily("宋体", FontCharRange.None);
return paragraph;
}
public XWPFParagraph SetTableParagraphInstanceSetting(XWPFDocument document, XWPFTable table, List<string> fillContent, ParagraphAlignment paragraphAlign, int rowsHeight = 24, bool isBold = false, int fontSize = 12, string fontColor = "000000", bool isItalic = false)
{
var para = new CT_P();
XWPFParagraph paragraph = new XWPFParagraph(para, table.Body);
paragraph.Alignment = paragraphAlign;
paragraph.VerticalAlignment = TextAlignment.CENTER;
XWPFRun xwpfRun = paragraph.CreateRun();
int index = 0;
foreach (var item in fillContent)
{
if (index > 0)
{
xwpfRun.AddBreak(BreakType.TEXTWRAPPING);
}
xwpfRun.SetText(item,index);
index++;
}
xwpfRun.FontSize = fontSize;
xwpfRun.SetColor(fontColor);
xwpfRun.IsItalic = isItalic;
xwpfRun.IsBold = isBold;
xwpfRun.SetFontFamily("宋体", FontCharRange.None);
return paragraph;
}
public void MergeVertically(XWPFTable table, int col, int fromRow, int toRow)
{
for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++)
{
XWPFTableCell cell = table.GetRow(rowIndex).GetCell(col);
if (rowIndex == fromRow)
{
cell.GetCTTc().AddNewTcPr().AddNewVMerge().val = ST_Merge.restart;
}
else
{
cell.GetCTTc().AddNewTcPr().AddNewVMerge().val = ST_Merge.@continue;
}
}
table.GetRow(fromRow).GetCell(col).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
}
public void MergeHorizontal(XWPFTable table, int row, int fromCell, int toCell)
{
for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++)
{
XWPFTableCell cell = table.GetRow(row).GetCell(cellIndex);
if (cellIndex == fromCell)
{
cell.GetCTTc().AddNewTcPr().AddNewHMerge().val = ST_Merge.restart;
}
else
{
cell.GetCTTc().AddNewTcPr().AddNewHMerge().val = ST_Merge.@continue;
}
}
table.GetRow(row).GetCell(fromCell).SetVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
}
}
}