C# 生成word文档(NPOI)
发布日期:2021-09-11 05:52:44 浏览次数:26 分类:技术文章

本文共 1061 字,大约阅读时间需要 3 分钟。

using NPOI.XWPF.UserModel

XWPFDocument doc = new XWPFDocument(); //创建新的word文档
XWPFParagraph p1 = doc.CreateParagraph(); //向新文档中添加段落
p1.SetAlignment(ParagraphAlignment.CENTER); //段落对其方式为居中

XWPFRun r1 = p1.CreateRun(); //向该段落中添加文字

r1.SetText("测试段落一");

XWPFParagraph p2 = doc.CreateParagraph();

p2.SetAlignment(ParagraphAlignment.LEFT);

XWPFRun r2 = p2.CreateRun();

r2.SetText("测试段落二");
     r2.SetFontSize(16);//设置字体大小
       r2.SetBlod(true);//设置粗体

FileStream sw = File.Create("cutput.docx"); //...

doc.Write(sw); //...
sw.Close(); //在服务端生成文件

FileInfo file = new FileInfo("cutput.docx");//文件保存路径及名称

//注意: 文件保存的父文件夹需添加Everyone用户,并给予其完全控制权限
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename="
+ HttpUtility.UrlEncode("output.docx", System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", file.Length.ToString());
Response.WriteFile(file.FullName);
Response.Flush(); //以上将生成的word文件发送至用户浏览器

File.Delete("cutput.docx");

转载地址:https://blog.csdn.net/weixin_34406061/article/details/86015388 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:看完这篇分析,还不懂分布式事物,请给我差评
下一篇:【亲测】<g++/gcc>CentOS下g++: command not found问题的解决(c++环境安装)

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月03日 15时53分58秒