NPOI生成Word文档
NPOI生成Word文档
承接上一篇“NPOI生成excel表格”,这篇博客主要介绍使用NPOI生成Word文档基本方法。话不多说,直接上代码示例:
public void CreateWordFile(String filePath)
{
XWPFDocument doc = new XWPFDocument();
doc = this.WriteHelloWorld(doc);
String fileName = "HelloWorld.docx";
FileStream sw = File.Create(filePath + @"\" + fileName);
doc.Write(sw);
sw.Close();
}
public XWPFDocument WriteHelloWorld(XWPFDocument doc)
{
CT_P m_p = doc.Document.body.AddNewP();
var pTitle = doc.CreateParagraph();
pTitle.Alignment = ParagraphAlignment.CENTER;
var rTitle = pTitle.CreateRun();
rTitle.AddBreak(BreakClear.ALL);
rTitle.SetBold(true);
rTitle.SetText("Hello World");
rTitle.FontFamily = "宋体";
rTitle.SetBold(true);
rTitle.FontSize = 16;
return doc;
}
执行方法CreateWordFile(""),一个简单的word文档就生成了!