后端接口
/// <summary>
/// 根据id导出 生成word文档
/// </summary>
/// <param name="ID"></param>
/// <returns></returns>
[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);
}
//XWPFInsertPicture.WordIndertPicTest(_webhostEnvironment.WebRootPath + "/" + model.PhotoUrl);
using (var stream = new FileStream(Path.Combine(filePath, fileName), FileMode.Create, FileAccess.Write))
{
//创建document文档对象对象实例
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);//显示的行列数rows:4行,cols: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;
//Table 表格第一行展示...后面的都是一样,只改变GetRow中的行数
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));
//img图片
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;//默认插入少了这个ID号,它就是图片的序号,从 1开始编号的。
#endregion
#region 方法二
//var gr = firstXwpfTable.GetRow(0).GetCell(6).Paragraphs.ToList().FirstOrDefault().CreateRun().GetCTR().AddNewDrawing().AddNewInline();
//var picID = document.AddPictureData(gfs, (int)PictureType.JPEG);
//XWPFInsertPicture.TableInsertPicture(gr, picID, 100, 140);
//firstXwpfTable.GetRow(0).GetCell(6).AddParagraph();
#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);
//Table 表格第二行
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;
//Table 表格第三行
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(6).SetParagraph(NpoiWordParagraphTextStyleHelper._.SetTableParagraphInstanceSetting(document, firstXwpfTable, "出生年月", ParagraphAlignment.CENTER, 24, 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;
// Table 表格第四行
firstXwpfTable.GetRow(row).Height = 120;
firstXwpfTable.GetRow(row).MergeCells(1, 6);//合并3列 职级或 职称
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;
}
//rankAndTitle = GetSystemDataByIdstring(model.RankAndTitle_GWY) + "/" + GetSystemDataByIdstring(model.RankAndTitle_SY);
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);//显示的行列数rows:4行,cols:7列
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;
//Table 表格第六行
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));
//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.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);//显示的行列数rows:5行,cols:7列
thirdXwpfTable2.Width = 5250;//总宽度
thirdXwpfTable2.SetColumnWidth(0, 1450); /* 设置列宽 */
thirdXwpfTable2.SetColumnWidth(1, 3800); /* 设置列宽 */
row = 0;
thirdXwpfTable2.GetRow(row).Height = 30 * 20;
//Table 表格第行
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);//显示的行列数rows:5行,cols: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;
//Table 表格行
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>();
//Rewardspunishments = model.PersonalRewardsAndPunishments.OrderBy(p=>p.YearOrDate).FirstOrDefault()?.Infos;
foreach (var item in model.PersonalRewardsAndPunishments)
{
Rewardspunishments.Add(GetDataTimeString(item.YearOrDate) + " " + item.Infos);
}
row++;
thirdXwpfTable.GetRow(row).Height = 160 * 20;
//Table 表格行
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");
//PersonalAnnualAssessmentResults = model.PersonalAnnualAssessmentResults.OrderBy(p => p.YearOrDate).FirstOrDefault()?.Infos;
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;
//Table 表格行
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);//显示的行列数rows:5行,cols:7列
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;
//Table 表格行
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日");//检查时间
// TODO: 这里一行需要显示文本
document.SetParagraph(NpoiWordParagraphTextStyleHelper._.ParagraphInstanceSetting(document, $"时间: {checkTime}", false, 14, "宋体", ParagraphAlignment.RIGHT, true, ""), 1);
//向文档流中写入内容,生成word
document.Write(stream);
savePath = uploadPath + fileName;
}
//SaveWordFile(out savePath);
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;
}
/// <summary>
/// 创建word文档中的段落对象和设置段落文本的基本样式(字体大小,字体,字体颜色,字体对齐位置)
/// </summary>
/// <param name="document">document文档对象</param>
/// <param name="fillContent">段落第一个文本对象填充的内容</param>
/// <param name="isBold">是否加粗</param>
/// <param name="fontSize">字体大小</param>
/// <param name="fontFamily">字体</param>
/// <param name="paragraphAlign">段落排列(左对齐,居中,右对齐)</param>
/// <param name="isStatement">是否在同一段落创建第二个文本对象(解决同一段落里面需要填充两个或者多个文本值的情况,多个文本需要自己拓展,现在最多支持两个)</param>
/// <param name="secondFillContent">第二次声明的文本对象填充的内容,样式与第一次的一致</param>
/// <param name="fontColor">字体颜色--十六进制</param>
/// <param name="isItalic">是否设置斜体(字体倾斜)</param>
/// <returns></returns>
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;
}
/// <summary>
/// 创建Word文档中表格段落实例和设置表格段落文本的基本样式(字体大小,字体,字体颜色,字体对齐位置)
/// </summary>
/// <param name="document">document文档对象</param>
/// <param name="table">表格对象</param>
/// <param name="fillContent">要填充的文字</param>
/// <param name="paragraphAlign">段落排列(左对齐,居中,右对齐)</param>
/// <param name="textPosition">设置文本位置(设置两行之间的行间,从而实现表格文字垂直居中的效果),从而实现table的高度设置效果 </param>
/// <param name="isBold">是否加粗(true加粗,false不加粗)</param>
/// <param name="fontSize">字体大小</param>
/// <param name="fontColor">字体颜色--十六进制</param>
/// <param name="isItalic">是否设置斜体(字体倾斜)</param>
/// <returns></returns>
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;//文字显示位置,段落排列(左对齐,居中,右对齐)
//paragraph.VerticalAlignment = TextAlignment.CENTER;
//paragraph.FontAlignment =Convert.ToInt32(ParagraphAlignment.CENTER);//字体在单元格内显示位置与 paragraph.Alignment效果相似
XWPFRun xwpfRun = paragraph.CreateRun();//创建段落文本对象
xwpfRun.SetText(fillContent);
xwpfRun.FontSize = fontSize;//字体大小
xwpfRun.SetColor(fontColor);//设置字体颜色--十六进制
xwpfRun.IsItalic = isItalic;//是否设置斜体(字体倾斜)
xwpfRun.IsBold = isBold;//是否加粗
xwpfRun.SetFontFamily("宋体", FontCharRange.None);//设置字体(如:微软雅黑,华文楷体,宋体)
//xwpfRun.SetTextPosition(textPosition);//设置文本位置(设置两行之间的行间),从而实现table的高度设置效果
return paragraph;
}
/// <summary>
/// 创建Word文档中表格段落实例和设置表格段落文本的基本样式(字体大小,字体,字体颜色,字体对齐位置)
/// document文档对象
/// 表格对象
/// 要填充的文字
/// 段落排列(左对齐,居中,右对齐)
/// 设置文本位置(设置两行之间的行间),从而实现table的高度设置效果
/// 是否加粗(true加粗,false不加粗)
/// 字体大小
/// </summary>
/// <param name="table"></param>
/// <param name="fillContent"></param>
/// <param name="paragraphAlign"></param>
/// <param name="rowsHeight"></param>
/// <param name="isBold"></param>
/// <param name="fontSize"></param>
/// <returns></returns>
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);//换行TEXTWRAPPING
//xwpfRun.AddTab();
}
xwpfRun.SetText(item,index);
index++;
}
xwpfRun.FontSize = fontSize;//字体大小
xwpfRun.SetColor(fontColor);//设置字体颜色--十六进制
xwpfRun.IsItalic = isItalic;//是否设置斜体(字体倾斜)
xwpfRun.IsBold = isBold;//是否加粗
xwpfRun.SetFontFamily("宋体", FontCharRange.None);//设置字体(如:微软雅黑,华文楷体,宋体)
//xwpfRun.SetTextPosition(rowsHeight);//设置文本位置(设置两行之间的行间),从而实现table的高度设置效果
return paragraph;
}
/// <summary>
/// 合并列
/// </summary>
/// <param name="table"></param>
/// <param name="col"></param>
/// <param name="fromRow"></param>
/// <param name="toRow"></param>
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);
}
/// <summary>
/// 合并行
/// </summary>
/// <param name="table"></param>
/// <param name="row"></param>
/// <param name="fromCell"></param>
/// <param name="toCell"></param>
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);
}
}
}