1、建立表格操做html
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(), 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(); }
2、設定單元格寬度數據庫
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(); //添加TcPr ctPr.tcW = new CT_TblWidth(); ctPr.tcW.w = "100";//單元格寬 ctPr.tcW.type = ST_TblWidth.dxa;
3、複製單元格樣式,複製行樣式,建立新行app
注:表格的行沒有設置樣式的地方,樣式設置具體到了單元格tcp
特別說明: 在表格SetText() 的時候不能傳入null 值,尤爲是字符串須要處理成「」post
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; }
更多:字體