.Net Core NOPI操作word(二) 表格操作

时间:2022-09-22 06:53:26

一、创建表格操作

private void btnExport_Click(object sender, EventArgs e)
{
var dbcontext = new BlogModel();
var list = dbcontext.ArticleInfos.ToList();
//创建document对象
XWPFDocument doc = new XWPFDocument();
//创建段落对象
XWPFParagraph p1 = doc.CreateParagraph();
//创建run对象
//本节提到的所有样式都是基于XWPFRun的,
//你可以把XWPFRun理解成一小段文字的描述对象,
//这也是Word文档的特征,即文本描述性文档。
//来自Tony Qu http://tonyqus.sinaapp.com/archives/609
XWPFRun r1 = p1.CreateRun();
r1.SetBold(true);
r1.SetText("数据导出demo");
r1.SetBold(true);
r1.SetFontFamily("Arial");//设置雅黑字体
//创建表格对象列数写死了,可根据自己需要改进或者自己想想解决方案
XWPFTable table = doc.CreateTable(list.Count(), );
for (int i = ; i < list.Count(); i++)
{
table.GetRow(i).GetCell().SetText(list[i].Id.ToString());
table.GetRow(i).GetCell().SetText(list[i].Title);
table.GetRow(i).GetCell().SetText(list[i].Content);
table.GetRow(i).GetCell().SetText(list[i].AddTime);
}
//保存文件到磁盘
FileStream out1 = new FileStream("simpleTable.docx", FileMode.Create);
doc.Write(out1);
out1.Close();
}

二、设定单元格宽度

   table.GetRow().GetCell().GetCTTc().AddNewTcPr().tcW.w = "";//单元格宽
table.GetRow().GetCell().GetCTTc().AddNewTcPr().tcW.type = ST_TblWidth.dxa; CT_TcPr ctPr = cttc.AddNewTcPr(); //添加TcPr
ctPr.tcW = new CT_TblWidth();
ctPr.tcW.w = "";//单元格宽
ctPr.tcW.type = ST_TblWidth.dxa;

三、复制单元格样式,复制行样式,创建新行

注:表格的行没有设置样式的地方,样式设置具体到了单元格

特别说明: 在表格SetText() 的时候不能传入null 值,尤其是字符串需要处理成“”

XWPFTableRow row4 = table.GetRow();
row4.GetCell().SetText(stu.JapanHistory??"");
static void TestWord2()
{
//1.读取word 文档
string tempPath = @"D:\QLWork\QL.Ohter\LiuXue.Web.CRM\数据库\pinggu.docx";
string targetPath = "D:\\test_pg.docx"; using (FileStream fs = new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
XWPFDocument doc = new XWPFDocument(fs); //2.修改内容
//获取表格
CT_Tbl tbl = doc.Document.body.getTblArray().FirstOrDefault();
XWPFTable table = doc.GetTable(tbl);
//第一行
XWPFTableRow row0 = table.GetRow();
string str = row0.GetCell().GetText();
Console.WriteLine(str);
row0.GetCell().SetText("京国际文化学院");
row0.GetCell().SetText("2019年10月");
//第三行
XWPFTableRow row2 = table.GetRow();
row2.GetCell().SetText("张三丰 (拼音): zhagnsanfeng"); //创建一行
//CT_Row ctrow12 = new CT_Row();
//XWPFTableRow row12 = new XWPFTableRow(ctrow12, table);
//table.AddRow(row12, 12); //row12.CreateCell().SetText("小学");
//row12.CreateCell().SetText("聊城三中");
//row12.CreateCell().SetText("2019年10月~2019年3月");
//row12.CreateCell().SetText("5年制");
//row12.CreateCell().SetText("计算机应用"); //根据上一行创建一行,使用上一行的样式
XWPFTableRow row11 = table.GetRow();
XWPFTableCell oldCell = row11.GetCell(); CT_Row newctRow = new CT_Row();
XWPFTableRow rowNew = new XWPFTableRow(newctRow, table);
XWPFTableCell cell1 = rowNew.CreateCell(); cell1.SetText("小学");
CopyCellStyle(cell1, oldCell); cell1 = rowNew.CreateCell();
cell1.SetText("张营小学");
oldCell = row11.GetCell();
CopyCellStyle(cell1, oldCell); cell1 = rowNew.CreateCell();
cell1.SetText("2019年-5月~2019年10月");
oldCell = row11.GetCell();
CopyCellStyle(cell1, oldCell); cell1 = rowNew.CreateCell();
oldCell = row11.GetCell();
CopyCellStyle(cell1, oldCell); cell1 = rowNew.CreateCell();
oldCell = row11.GetCell();
CopyCellStyle(cell1, oldCell); table.AddRow(rowNew, ); //3.保存文件
using (FileStream fsw = new FileStream(targetPath, FileMode.OpenOrCreate, FileAccess.Write))
{
doc.Write(fsw);
doc.Close();
Console.WriteLine("word生成成功");
}
}
}
//复制单元格样式
static void CopyCellStyle(XWPFTableCell newCell, XWPFTableCell oldCell)
{
CT_Tc cttc1 = newCell.GetCTTc();
CT_TcPr tcpr1 = cttc1.AddNewTcPr(); tcpr1.tcW = oldCell.GetCTTc().tcPr.tcW;
tcpr1.tcBorders = oldCell.GetCTTc().tcPr.tcBorders;
tcpr1.tcMar = oldCell.GetCTTc().tcPr.tcMar;
tcpr1.gridSpan = oldCell.GetCTTc().tcPr.gridSpan;
}

更多:

.Net Core NOPI操作word(一)

.Net Excel操作之NPOI(二)常用操作封装

.Net Excel操作之NPOI(一)简介