C# 生成word文档(NPOI)
using NPOI.XWPF.UserModel 1 XWPFDocument doc = new XWPFDocument(); //创建新的word文档 2 3 XWPFParagraph p1 = doc.CreateParagraph(); //向新文档中添加段落 4 p1.SetAlignment(ParagraphAlignment.CENTER); //段落对其方式为居中 5 6 XWPFRun r1 = p1.CreateRun(); //向该段落中添加文字 7 r1.SetText("测试段落一"); 8 9 XWPFParagraph p2 = doc.CreateParagraph(); 10 p2.SetAlignment(ParagraphAlignment.LEFT); 11 12 XWPFRun r2 = p2.CreateRun(); 13 r2.SetText("测试段落二"); 14 r2.SetFontSize(16);//设置字体大小 r2.SetBlod(true);//设置粗体 15 16 FileStream sw = File.Create("cutput.docx"); //... 17 doc.Write(sw); //... 18 sw.Close(); //在服务端生成文件 19 20 FileInfo file = new FileInfo("cutput.docx");//文件保存路径及名称 21 //注意: 文件保存的父文件夹需添加Everyone用户,并给予其完全控制权限 22 Response.Clear(); 23 Response.ClearHeaders(); 24 Response.Buffer = false; 25 Response.ContentType = "application/octet-stream"; 26 Response.AppendHeader("Content-Disposition", "attachment;filename=" 27 + HttpUtility.UrlEncode("output.docx", System.Text.Encoding.UTF8)); 28 Response.AppendHeader("Content-Length", file.Length.ToString()); 29 Response.WriteFile(file.FullName); 30 Response.Flush(); //以上将生成的word文件发送至用户浏览器 31 32 File.Delete("cutput.docx");