WordTest.aspx.cs

  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Web;
  5. using System.Web.UI;
  6. using NPOI.OpenXmlFormats.Wordprocessing;
  7. using NPOI.XWPF.UserModel;
  8.  
  9. namespace WebDemo
  10. {
  11. public partial class WordTest : Page
  12. {
  13. protected void Page_Load(object sender, EventArgs e)
  14. {
  15. }
  16.  
  17. /// <summary>
  18. /// 新增
  19. /// </summary>
  20. /// <param name="sender"></param>
  21. /// <param name="e"></param>
  22. protected void btnPrint_Click(object sender, EventArgs e)
  23. {
  24. //创建document对象
  25. var doc = new XWPFDocument();
  26.  
  27. //创建段落对象1
  28. var p1 = doc.CreateParagraph();
  29. p1.Alignment = ParagraphAlignment.CENTER; //字体居中
  30. //创建run对象
  31. //本节提到的所有样式都是基于XWPFRun的,
  32. //你可以把XWPFRun理解成一小段文字的描述对象,
  33. //这也是Word文档的特征,即文本描述性文档。
  34. //来自Tony Qu http://tonyqus.sinaapp.com/archives/609
  35. var runTitle = p1.CreateRun();
  36. runTitle.IsBold = true;
  37. runTitle.SetText("军检验收单");
  38. runTitle.FontSize = ;
  39. runTitle.SetFontFamily("宋体", FontCharRange.None); //设置雅黑字体
  40.  
  41. //创建段落对象2
  42. var p2 = doc.CreateParagraph();
  43. var run1 = p2.CreateRun();
  44. run1.SetText(" 军检项目号:");
  45. run1.FontSize = ;
  46. run1.SetFontFamily("华文楷体", FontCharRange.None); //设置雅黑字体
  47.  
  48. #region 头部(6 rows)
  49.  
  50. //基本row12,列5;头部6行,4列
  51. var tableTop = doc.CreateTable(, );
  52. tableTop.Width = *;
  53. tableTop.SetColumnWidth(, ); /* 设置列宽 */
  54. tableTop.SetColumnWidth(, ); /* 设置列宽 */
  55. tableTop.SetColumnWidth(, ); /* 设置列宽 */
  56. tableTop.SetColumnWidth(, ); /* 设置列宽 */
  57. tableTop.SetColumnWidth(, ); /* 设置列宽 */
  58.  
  59. tableTop.GetRow().MergeCells(, ); /* 合并行 */
  60. tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, "产品名称"));
  61. tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, " "));
  62.  
  63. tableTop.GetRow().MergeCells(, );
  64. tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, "项目名称"));
  65. tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, " "));
  66.  
  67. tableTop.GetRow().MergeCells(, );
  68. tableTop.GetRow()
  69. .GetCell()
  70. .SetParagraph(SetCellText(doc, tableTop, "施工依据", ParagraphAlignment.CENTER, ));
  71. tableTop.GetRow()
  72. .GetCell()
  73. .SetParagraph(SetCellText(doc, tableTop, " ", ParagraphAlignment.CENTER, ));
  74.  
  75. tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, "检验方式"));
  76. tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, "独立检验"));
  77. tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, " "));
  78. tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, "联合检验"));
  79. tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, " "));
  80.  
  81. tableTop.GetRow().MergeCells(, );
  82. tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, "设备名称及编号"));
  83. tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, " "));
  84. tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, "设备制造厂"));
  85. tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, " "));
  86. //tableTop.GetRow(4).GetCell(3).SetBorderBottom(XWPFtableTop.XWPFBorderType.NONE,0,0,"");
  87.  
  88. tableTop.GetRow().MergeCells(, );
  89. var para = new CT_P();
  90. var pCell = new XWPFParagraph(para, tableTop.Body);
  91. pCell.Alignment = ParagraphAlignment.LEFT; //字体居中
  92.  
  93. var r1c1 = pCell.CreateRun();
  94. r1c1.SetText("检验要素共9项");
  95. r1c1.FontSize = ;
  96. r1c1.SetFontFamily("华文楷体", FontCharRange.None); //设置雅黑字体
  97. tableTop.GetRow().GetCell().SetParagraph(pCell);
  98.  
  99. //table.GetRow(6).GetCell(0).SetParagraph(SetCellText(doc, table, "序号"));
  100. //table.GetRow(6).GetCell(1).SetParagraph(SetCellText(doc, table, "检验要素"));
  101. //table.GetRow(6).GetCell(2).SetParagraph(SetCellText(doc, table, "指标要求"));
  102. //table.GetRow(6).GetCell(3).SetParagraph(SetCellText(doc, table, "实测值"));
  103. //table.GetRow(6).GetCell(4).SetParagraph(SetCellText(doc, table, "测量工具编号及有效期"));
  104.  
  105. #endregion
  106.  
  107. #region 检验要素列表部分(数据库读取循环显示)
  108.  
  109. /* 打印1页:小于8行数据,创建9行;
  110. * 打印2页:大于8小于26行数据,创建27行。增加18
  111. * 打印3页:大于26小于44行数据,创建45行。增加18
  112. */
  113. var tableContent = doc.CreateTable(, );
  114. tableContent.Width = *;
  115. tableContent.SetColumnWidth(, ); /* 设置列宽 */
  116. tableContent.SetColumnWidth(, ); /* 设置列宽 */
  117. tableContent.SetColumnWidth(, ); /* 设置列宽 */
  118. tableContent.SetColumnWidth(, ); /* 设置列宽 */
  119. tableContent.SetColumnWidth(, ); /* 设置列宽 */
  120.  
  121. tableContent.GetRow().GetCell().SetParagraph(SetCellText(doc, tableContent, "序号"));
  122. tableContent.GetRow().GetCell().SetParagraph(SetCellText(doc, tableContent, "检验要素"));
  123. tableContent.GetRow().GetCell().SetParagraph(SetCellText(doc, tableContent, "指标要求"));
  124. tableContent.GetRow().GetCell().SetParagraph(SetCellText(doc, tableContent, "实测值"));
  125. tableContent.GetRow().GetCell().SetParagraph(SetCellText(doc, tableContent, "测量工具编号及有效期"));
  126.  
  127. for (var i = ; i < ; i++)
  128. {
  129. tableContent.GetRow(i)
  130. .GetCell()
  131. .SetParagraph(SetCellText(doc, tableContent, i.ToString(), ParagraphAlignment.CENTER, ));
  132. tableContent.GetRow(i)
  133. .GetCell()
  134. .SetParagraph(SetCellText(doc, tableContent, "检验要素", ParagraphAlignment.CENTER, ));
  135. tableContent.GetRow(i)
  136. .GetCell()
  137. .SetParagraph(SetCellText(doc, tableContent, "指标要求", ParagraphAlignment.CENTER, ));
  138. tableContent.GetRow(i)
  139. .GetCell()
  140. .SetParagraph(SetCellText(doc, tableContent, "实测值", ParagraphAlignment.CENTER, ));
  141. tableContent.GetRow(i)
  142. .GetCell()
  143. .SetParagraph(SetCellText(doc, tableContent, "测量工具编号及有效期", ParagraphAlignment.CENTER, ));
  144. }
  145.  
  146. #endregion
  147.  
  148. #region 底部内容
  149.  
  150. var tableBottom = doc.CreateTable(, );
  151. tableBottom.Width = *;
  152.  
  153. tableBottom.SetColumnWidth(, ); /* 设置列宽 */
  154. tableBottom.SetColumnWidth(, ); /* 设置列宽 */
  155. tableBottom.SetColumnWidth(, ); /* 设置列宽 */
  156. tableBottom.SetColumnWidth(, ); /* 设置列宽 */
  157.  
  158. tableBottom.GetRow().MergeCells(, ); /* 合并行 */
  159. tableBottom.GetRow()
  160. .GetCell()
  161. .SetParagraph(SetCellText(doc, tableBottom, "附件:", ParagraphAlignment.LEFT, ));
  162. tableBottom.GetRow().Height = ;
  163.  
  164. tableBottom.GetRow().MergeCells(, ); /* 合并行 */
  165. tableBottom.GetRow()
  166. .GetCell()
  167. .SetParagraph(SetCellText(doc, tableBottom, "检验结论:", ParagraphAlignment.LEFT, ));
  168. tableBottom.GetRow().Height = ;
  169.  
  170. tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, "施工部门"));
  171. tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, " "));
  172. tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, "报验日期"));
  173. tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, " "));
  174.  
  175. tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, "军检次数"));
  176. tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, " "));
  177. tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, "军检日期"));
  178. tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, " "));
  179.  
  180. tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, "检验员"));
  181. tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, " "));
  182. tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, "军代表"));
  183. tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, " "));
  184.  
  185. #endregion
  186.  
  187. //保存文件到磁盘WinForm
  188. //string docPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "DocxWord");
  189. //if (!Directory.Exists(docPath)) { Directory.CreateDirectory(docPath); }
  190. //string fileName = string.Format("{0}.doc", HttpUtility.UrlEncode("jjysd" + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff"), System.Text.Encoding.UTF8));
  191. //FileStream out1 = new FileStream(Path.Combine(docPath, fileName), FileMode.Create);
  192. //doc.Write(out1);
  193. //out1.Close();
  194.  
  195. #region 保存导出WebForm
  196.  
  197. //Response.Redirect(ResolveUrl(string.Format(@"~\DocxWord\{0}", fileName)));
  198.  
  199. var ms = new MemoryStream();
  200. doc.Write(ms);
  201. Response.AddHeader("Content-Disposition",
  202. string.Format("attachment; filename={0}.doc",
  203. HttpUtility.UrlEncode("文件名" + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff"),
  204. Encoding.UTF8)));
  205. Response.BinaryWrite(ms.ToArray());
  206. Response.End();
  207.  
  208. ms.Close();
  209. ms.Dispose();
  210.  
  211. //using (MemoryStream ms = new MemoryStream())
  212. //{
  213. // doc.Write(ms);
  214. // Response.ClearContent();
  215. // Response.Buffer = true;
  216. // Response.ContentType = "application/octet-stream";
  217. // Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.doc", HttpUtility.UrlEncode("军检验收单" + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff"), System.Text.Encoding.UTF8)));
  218. // Response.BinaryWrite(ms.ToArray());
  219. // //Response.End();
  220. // Response.Flush();
  221. // doc = null;
  222. // ms.Close();
  223. // ms.Dispose();
  224. //}
  225.  
  226. #endregion
  227. }
  228.  
  229. /// <summary>
  230. /// 设置字体格式
  231. /// </summary>
  232. /// <param name="doc"></param>
  233. /// <param name="table"></param>
  234. /// <param name="setText"></param>
  235. /// <returns></returns>
  236. public XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText)
  237. {
  238. //table中的文字格式设置
  239. var para = new CT_P();
  240. var pCell = new XWPFParagraph(para, table.Body);
  241. pCell.Alignment = ParagraphAlignment.CENTER; //字体居中
  242. pCell.VerticalAlignment = TextAlignment.CENTER; //字体居中
  243.  
  244. var r1c1 = pCell.CreateRun();
  245. r1c1.SetText(setText);
  246. r1c1.FontSize = ;
  247. r1c1.SetFontFamily("华文楷体", FontCharRange.None); //设置雅黑字体
  248.  
  249. return pCell;
  250. }
  251.  
  252. /// <summary>
  253. /// 设置单元格格式
  254. /// </summary>
  255. /// <param name="doc">doc对象</param>
  256. /// <param name="table">表格对象</param>
  257. /// <param name="setText">要填充的文字</param>
  258. /// <param name="align">文字对齐方式</param>
  259. /// <param name="textPos">rows行的高度</param>
  260. /// <returns></returns>
  261. public XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText, ParagraphAlignment align,
  262. int textPos)
  263. {
  264. var para = new CT_P();
  265. var pCell = new XWPFParagraph(para, table.Body);
  266. //pCell.Alignment = ParagraphAlignment.LEFT;//字体
  267. pCell.Alignment = align;
  268.  
  269. var r1c1 = pCell.CreateRun();
  270. r1c1.SetText(setText);
  271. r1c1.FontSize = ;
  272. r1c1.SetFontFamily("华文楷体", FontCharRange.None); //设置雅黑字体
  273. r1c1.SetTextPosition(textPos); //设置高度
  274.  
  275. return pCell;
  276. }
  277. }
  278. }

运行效果

上面的是webform版本的  用mvc搞了一下午遇到了一些问题 这次更新时间在2018/9/28

添加下列代码

  1. {
  2. FileStream os = new FileStream(Server.MapPath("/Content/Word/" + jibenxinxis.xingming + ".doc"), FileMode.OpenOrCreate);
  3. doc.Write(os);
  4.  
  5. return File(new FileStream(Server.MapPath("/Content/Word") + "\\" + jibenxinxis.xingming + ".doc", FileMode.Open), "text/plain", Server.UrlEncode(jibenxinxis.xingming + ".doc"));
  6. }

必须是get  不可以是ajax

这样才可以return file  最好用a标签访问控制器

利用NPOI生成word文档(c#)的更多相关文章

  1. 利用NPOI导出Word文档帮助类

    /// <summary> /// NPOI操作Word /// </summary> public class NpoiWordHelper { /// <summar ...

  2. 有哪位大侠操作过NPOI生成word文档,如何设置页眉页脚距离顶部和底部距离?

    #region 1.创建文档(页眉.页脚) XWPFDocument doc = new XWPFDocument(); //页面设置 A4:w=11906 h=16838 doc.Document. ...

  3. 利用NPOI生成DOCX文档

    首先安装NPOI控件: Install-Package NPOI 代码: using NPOI.OpenXmlFormats.Wordprocessing; using NPOI.XWPF.UserM ...

  4. 黄聪:利用OpenXml生成Word2007文档(转)

    原文:http://blog.csdn.net/francislaw/article/details/7568317 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 一Op ...

  5. 利用OpenXml生成Word2007文档

    一.OpenXml简介 利用C#生成Word文档并非一定要利用OpenXml技术,至少可以使用微软提供的Office相关组件来编程,不过对于Office2007(确切的说是Word.Excel和Pow ...

  6. JAVAWEB使用FreeMarker利用ftl把含有图片的word模板生成word文档,然后打包成压缩包进行下载

    这是写的另一个导出word方法:https://www.cnblogs.com/pxblog/p/13072711.html 引入jar包,freemarker.jar.apache-ant-zip- ...

  7. 使用Spire.Doc组件利用模板导出Word文档

    以前一直是用Office的组件实现Word文档导出,但是让客户在服务器安装Office,涉及到版权:而且Office安装,包括权限配置也是比较麻烦. 现在流行使用第三方组件来实现对Office的操作, ...

  8. POI生成word文档完整案例及讲解

    一,网上的API讲解 其实POI的生成Word文档的规则就是先把获取到的数据转成xml格式的数据,然后通过xpath解析表单式的应用取值,判断等等,然后在把取到的值放到word文档中,最后在输出来. ...

  9. PoiDemo【Android将表单数据生成Word文档的方案之二(基于Poi4.0.0)】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用Poi实现android中根据模板文件生成Word文档的功能.这里的模板文件是doc文件.如果模板文件是docx文件的话,请阅读 ...

随机推荐

  1. windows jdk安装

    先去官网下载安装包 x86 32位 x64 64位 下载地址 安装jdk 安装目录默认c盘 配置系统环境 JAVA_HOME环境变量.作用:它指向jdk的安装目录,Eclipse/NetBeans/T ...

  2. Alpha冲刺(2/10)——2019.4.24

    作业描述 课程 软件工程1916|W(福州大学) 团队名称 修!咻咻! 作业要求 项目Alpha冲刺(团队) 团队目标 切实可行的计算机协会维修预约平台 开发工具 Eclipse 团队信息 队员学号 ...

  3. Codeforces 126B. Password (KMP)

    <题目链接> 题目大意:给定一个字符串,从中找出一个前.中.后缀最长公共子串("中"代表着既不是前缀,也不是后缀的部分). 解题分析:本题依然是利用了KMP中next数 ...

  4. 多标签caffe重新编译

    说明: Caffe自带的图像转LMDB接口只支持单label,对于多label的任务,可以使用HDF5的格式,也可以通过修改caffe代码来实现.本篇文章介绍怎么通过修改DataLayer来实现带Mu ...

  5. [POJ3630]Phone List (Tire)

    题意 trie字典树模板 LOJ有中文翻译https://loj.ac/problem/10049 思路 TIRE 代码 之前在LOJ上做过 直接交了 #include<cstdio> # ...

  6. 20181125第二章节总结part3

    数据-元祖 元祖的是可存放多个值,不可变,有顺序的,从左向右编号. 作用是可以用来存储一些不可以更改的配置文件 基本 语法: #创建新元祖 tuple = (,,,,,) #索引,写法同list tu ...

  7. web 10

    一.Iterations : 1.do...while : 创建执行指定语句的循环,直到测试条件评估为false.在执行语句后评估条件,导致指定语句至少执行一次. 例子:在以下示例中,do...而循环 ...

  8. Linux一键安装宝塔控制面板

    Linux一键安装宝塔的命令行 yum install -y wget && wget -O install.sh http://download.bt.cn/install/inst ...

  9. 详解 vue-cli 的打包配置文件代码(给大家写写注释)

    一.前言 对于webpack基础不好,node指令不通的童鞋.估计对自己搭建Vue.react脚手架是相当头疼的,有种无从下手的感觉.然而,从头看这2块,耗时太长,而且说实话得练才行,不练练手看不明白 ...

  10. LeetCode笔记:140. Word Break II

    题目描述 给定一个非空的字符串s,一个非空的字符串list作为字典.通过在s中添加空格可以将s变为由list中的word表示的句子,要求返回所有可能组成的句子.设定list中的word不重复,且每一个 ...