NOPI使用方式html
1.安裝nuget包 便可使用post
Install-Package NPOI
1、建立word文檔字體
//建立生成word文檔 string path = "D:\\test.docx"; XWPFDocument doc = new XWPFDocument(); // 添加段落 XWPFParagraph gp = doc.CreateParagraph(); gp.Alignment = ParagraphAlignment.CENTER;//水平居中 XWPFRun gr = gp.CreateRun(); gr.GetCTR().AddNewRPr().AddNewRFonts().ascii = "黑體"; gr.GetCTR().AddNewRPr().AddNewRFonts().eastAsia = "黑體"; gr.GetCTR().AddNewRPr().AddNewRFonts().hint = ST_Hint.eastAsia; gr.GetCTR().AddNewRPr().AddNewSz().val = (ulong)44;//2號字體 gr.GetCTR().AddNewRPr().AddNewSzCs().val = (ulong)44; gr.GetCTR().AddNewRPr().AddNewB().val = true; //加粗 gr.GetCTR().AddNewRPr().AddNewColor().val = "red";//字體顏色 gr.SetText("NPOI建立Word2007Docx"); gp = doc.CreateParagraph(); gr = gp.CreateRun(); CT_RPr rpr = gr.GetCTR().AddNewRPr(); CT_Fonts rfonts = rpr.AddNewRFonts(); rfonts.ascii = "宋體"; rfonts.eastAsia = "宋體"; rpr.AddNewSz().val = (ulong)21;//5號字體 rpr.AddNewSzCs().val = (ulong)21; gr.SetText("NPOI,顧名思義,就是POI的.NET版本。那POI又是什麼呢?POI是一套用Java寫成的庫,可以幫助開 發者在沒有安裝微軟Office的狀況下讀寫Office 97-2003的文件,支持的文件格式包括xls, doc, ppt等 。目前POI的穩定版本中支持Excel文件格式(xls和xlsx),其餘的都屬於不穩定版本(放在poi的scrachpad目錄 中)。"); gp = doc.CreateParagraph(); //添加表格 XWPFTable table = doc.CreateTable(1, 4);//建立一行4列表 CT_Tbl m_CTTbl = doc.Document.body.GetTblArray()[0];//得到文檔第一張表 CT_TblPr m_CTTblPr = m_CTTbl.AddNewTblPr(); m_CTTblPr.AddNewTblW().w = "2000"; //表寬 m_CTTblPr.AddNewTblW().type = ST_TblWidth.dxa; m_CTTblPr.tblpPr = new CT_TblPPr();//表定位 m_CTTblPr.tblpPr.tblpX = "4003";//表左上角座標 m_CTTblPr.tblpPr.tblpY = "365"; m_CTTblPr.tblpPr.tblpXSpec = ST_XAlign.center;//若不爲「Null」,則優先tblpX,即表由tblpXSpec定位 m_CTTblPr.tblpPr.tblpYSpec = ST_YAlign.center;//若不爲「Null」,則優先tblpY,即表由tblpYSpec定位 m_CTTblPr.tblpPr.leftFromText = (ulong)180; m_CTTblPr.tblpPr.rightFromText = (ulong)180; m_CTTblPr.tblpPr.vertAnchor = ST_VAnchor.text; m_CTTblPr.tblpPr.horzAnchor = ST_HAnchor.page; //表1行4列充值:a,b,c,d table.GetRow(0).GetCell(0).SetText("a"); table.GetRow(0).GetCell(1).SetText("b"); table.GetRow(0).GetCell(2).SetText("c"); table.GetRow(0).GetCell(3).SetText("d"); CT_Row m_NewRow = new CT_Row();//建立1行 XWPFTableRow m_Row = new XWPFTableRow(m_NewRow, table); table.AddRow(m_Row); //必需要!!! XWPFTableCell cell = m_Row.CreateCell();//建立單元格,也建立了一個CT_P CT_Tc cttc = cell.GetCTTc(); CT_TcPr ctPr = cttc.AddNewTcPr(); //ctPr.gridSpan.val = "3";//合併3列 cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center; cttc.GetPList()[0].AddNewR().AddNewT().Value = "666"; cell = m_Row.CreateCell();//建立單元格,也建立了一個CT_P cell.GetCTTc().GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center; cell.GetCTTc().GetPList()[0].AddNewR().AddNewT().Value = "e"; using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write)) { doc.Write(fs); Console.WriteLine("生成word成功"); }
生成word內容以下:spa
更多參考:.net
.Net Excel操做之NPOI(二)經常使用操做封裝code
https://blog.csdn.net/qq_35534449/article/details/79205468blog
https://blog.csdn.net/qq_32109957/article/details/86577938ci