NPOI generates Word document

NPOI generates Word document

 

    Continuing the previous article "NPOI generates excel form " , this blog mainly introduces the basic method of using NPOI to generate Word documents. Without further ado, let's go directly to the code example:

       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 = "Chapter 1";
            rTitle.SetBold(true);
            rTitle.FontSize = 16;
            return doc;
        }

 Execute the method CreateWordFile(""), and a simple word document is generated!

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327026083&siteId=291194637