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

一、创建表格操作

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/609XWPFRun r1 = p1.CreateRun();r1.SetBold(true);r1.SetText("数据导出demo");r1.SetBold(true);r1.SetFontFamily("Arial");//设置雅黑字体//创建表格对象列数写死了,可根据自己需要改进或者自己想想解决方案XWPFTable table = doc.CreateTable(list.Count(), 4);for (int i = 0; i < list.Count(); i++){table.GetRow(i).GetCell(0).SetText(list[i].Id.ToString());table.GetRow(i).GetCell(1).SetText(list[i].Title);table.GetRow(i).GetCell(2).SetText(list[i].Content);table.GetRow(i).GetCell(3).SetText(list[i].AddTime);}//保存文件到磁盘FileStream out1 = new FileStream("simpleTable.docx", FileMode.Create);doc.Write(out1);out1.Close();}

 

二、设定单元格宽度

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

 

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

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

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

XWPFTableRow row4 = table.GetRow(4);
row4.GetCell(0).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(0);string str = row0.GetCell(0).GetText();Console.WriteLine(str);row0.GetCell(0).SetText("京国际文化学院");row0.GetCell(1).SetText("2019年10月");//第三行XWPFTableRow row2 = table.GetRow(2);row2.GetCell(0).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(12);XWPFTableCell oldCell = row11.GetCell(0);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(1);CopyCellStyle(cell1, oldCell);cell1 = rowNew.CreateCell();cell1.SetText("2019年-5月~2019年10月");oldCell = row11.GetCell(2);CopyCellStyle(cell1, oldCell);cell1 = rowNew.CreateCell();oldCell = row11.GetCell(3);CopyCellStyle(cell1, oldCell);cell1 = rowNew.CreateCell();oldCell = row11.GetCell(4);CopyCellStyle(cell1, oldCell);table.AddRow(rowNew, 12);//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(一)简介

转载于:https://www.cnblogs.com/tianma3798/p/10953315.html


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部