WordTest.aspx.cs

using System;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using NPOI.OpenXmlFormats.Wordprocessing;
using NPOI.XWPF.UserModel; namespace WebDemo
{
public partial class WordTest : Page
{
protected void Page_Load(object sender, EventArgs e)
{
} /// <summary>
/// 新增
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnPrint_Click(object sender, EventArgs e)
{
//创建document对象
var doc = new XWPFDocument(); //创建段落对象1
var p1 = doc.CreateParagraph();
p1.Alignment = ParagraphAlignment.CENTER; //字体居中
//创建run对象
//本节提到的所有样式都是基于XWPFRun的,
//你可以把XWPFRun理解成一小段文字的描述对象,
//这也是Word文档的特征,即文本描述性文档。
//来自Tony Qu http://tonyqus.sinaapp.com/archives/609
var runTitle = p1.CreateRun();
runTitle.IsBold = true;
runTitle.SetText("军检验收单");
runTitle.FontSize = ;
runTitle.SetFontFamily("宋体", FontCharRange.None); //设置雅黑字体 //创建段落对象2
var p2 = doc.CreateParagraph();
var run1 = p2.CreateRun();
run1.SetText(" 军检项目号:");
run1.FontSize = ;
run1.SetFontFamily("华文楷体", FontCharRange.None); //设置雅黑字体 #region 头部(6 rows) //基本row12,列5;头部6行,4列
var tableTop = doc.CreateTable(, );
tableTop.Width = *;
tableTop.SetColumnWidth(, ); /* 设置列宽 */
tableTop.SetColumnWidth(, ); /* 设置列宽 */
tableTop.SetColumnWidth(, ); /* 设置列宽 */
tableTop.SetColumnWidth(, ); /* 设置列宽 */
tableTop.SetColumnWidth(, ); /* 设置列宽 */ tableTop.GetRow().MergeCells(, ); /* 合并行 */
tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, "产品名称"));
tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, " ")); tableTop.GetRow().MergeCells(, );
tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, "项目名称"));
tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, " ")); tableTop.GetRow().MergeCells(, );
tableTop.GetRow()
.GetCell()
.SetParagraph(SetCellText(doc, tableTop, "施工依据", ParagraphAlignment.CENTER, ));
tableTop.GetRow()
.GetCell()
.SetParagraph(SetCellText(doc, tableTop, " ", ParagraphAlignment.CENTER, )); tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, "检验方式"));
tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, "独立检验"));
tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, " "));
tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, "联合检验"));
tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, " ")); tableTop.GetRow().MergeCells(, );
tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, "设备名称及编号"));
tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, " "));
tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, "设备制造厂"));
tableTop.GetRow().GetCell().SetParagraph(SetCellText(doc, tableTop, " "));
//tableTop.GetRow(4).GetCell(3).SetBorderBottom(XWPFtableTop.XWPFBorderType.NONE,0,0,""); tableTop.GetRow().MergeCells(, );
var para = new CT_P();
var pCell = new XWPFParagraph(para, tableTop.Body);
pCell.Alignment = ParagraphAlignment.LEFT; //字体居中 var r1c1 = pCell.CreateRun();
r1c1.SetText("检验要素共9项");
r1c1.FontSize = ;
r1c1.SetFontFamily("华文楷体", FontCharRange.None); //设置雅黑字体
tableTop.GetRow().GetCell().SetParagraph(pCell); //table.GetRow(6).GetCell(0).SetParagraph(SetCellText(doc, table, "序号"));
//table.GetRow(6).GetCell(1).SetParagraph(SetCellText(doc, table, "检验要素"));
//table.GetRow(6).GetCell(2).SetParagraph(SetCellText(doc, table, "指标要求"));
//table.GetRow(6).GetCell(3).SetParagraph(SetCellText(doc, table, "实测值"));
//table.GetRow(6).GetCell(4).SetParagraph(SetCellText(doc, table, "测量工具编号及有效期")); #endregion #region 检验要素列表部分(数据库读取循环显示) /* 打印1页:小于8行数据,创建9行;
* 打印2页:大于8小于26行数据,创建27行。增加18
* 打印3页:大于26小于44行数据,创建45行。增加18
*/
var tableContent = doc.CreateTable(, );
tableContent.Width = *;
tableContent.SetColumnWidth(, ); /* 设置列宽 */
tableContent.SetColumnWidth(, ); /* 设置列宽 */
tableContent.SetColumnWidth(, ); /* 设置列宽 */
tableContent.SetColumnWidth(, ); /* 设置列宽 */
tableContent.SetColumnWidth(, ); /* 设置列宽 */ tableContent.GetRow().GetCell().SetParagraph(SetCellText(doc, tableContent, "序号"));
tableContent.GetRow().GetCell().SetParagraph(SetCellText(doc, tableContent, "检验要素"));
tableContent.GetRow().GetCell().SetParagraph(SetCellText(doc, tableContent, "指标要求"));
tableContent.GetRow().GetCell().SetParagraph(SetCellText(doc, tableContent, "实测值"));
tableContent.GetRow().GetCell().SetParagraph(SetCellText(doc, tableContent, "测量工具编号及有效期")); for (var i = ; i < ; i++)
{
tableContent.GetRow(i)
.GetCell()
.SetParagraph(SetCellText(doc, tableContent, i.ToString(), ParagraphAlignment.CENTER, ));
tableContent.GetRow(i)
.GetCell()
.SetParagraph(SetCellText(doc, tableContent, "检验要素", ParagraphAlignment.CENTER, ));
tableContent.GetRow(i)
.GetCell()
.SetParagraph(SetCellText(doc, tableContent, "指标要求", ParagraphAlignment.CENTER, ));
tableContent.GetRow(i)
.GetCell()
.SetParagraph(SetCellText(doc, tableContent, "实测值", ParagraphAlignment.CENTER, ));
tableContent.GetRow(i)
.GetCell()
.SetParagraph(SetCellText(doc, tableContent, "测量工具编号及有效期", ParagraphAlignment.CENTER, ));
} #endregion #region 底部内容 var tableBottom = doc.CreateTable(, );
tableBottom.Width = *; tableBottom.SetColumnWidth(, ); /* 设置列宽 */
tableBottom.SetColumnWidth(, ); /* 设置列宽 */
tableBottom.SetColumnWidth(, ); /* 设置列宽 */
tableBottom.SetColumnWidth(, ); /* 设置列宽 */ tableBottom.GetRow().MergeCells(, ); /* 合并行 */
tableBottom.GetRow()
.GetCell()
.SetParagraph(SetCellText(doc, tableBottom, "附件:", ParagraphAlignment.LEFT, ));
tableBottom.GetRow().Height = ; tableBottom.GetRow().MergeCells(, ); /* 合并行 */
tableBottom.GetRow()
.GetCell()
.SetParagraph(SetCellText(doc, tableBottom, "检验结论:", ParagraphAlignment.LEFT, ));
tableBottom.GetRow().Height = ; tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, "施工部门"));
tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, " "));
tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, "报验日期"));
tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, " ")); tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, "军检次数"));
tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, " "));
tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, "军检日期"));
tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, " ")); tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, "检验员"));
tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, " "));
tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, "军代表"));
tableBottom.GetRow().GetCell().SetParagraph(SetCellText(doc, tableBottom, " ")); #endregion //保存文件到磁盘WinForm
//string docPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "DocxWord");
//if (!Directory.Exists(docPath)) { Directory.CreateDirectory(docPath); }
//string fileName = string.Format("{0}.doc", HttpUtility.UrlEncode("jjysd" + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff"), System.Text.Encoding.UTF8));
//FileStream out1 = new FileStream(Path.Combine(docPath, fileName), FileMode.Create);
//doc.Write(out1);
//out1.Close(); #region 保存导出WebForm //Response.Redirect(ResolveUrl(string.Format(@"~\DocxWord\{0}", fileName))); var ms = new MemoryStream();
doc.Write(ms);
Response.AddHeader("Content-Disposition",
string.Format("attachment; filename={0}.doc",
HttpUtility.UrlEncode("文件名" + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff"),
Encoding.UTF8)));
Response.BinaryWrite(ms.ToArray());
Response.End(); ms.Close();
ms.Dispose(); //using (MemoryStream ms = new MemoryStream())
//{
// doc.Write(ms);
// Response.ClearContent();
// Response.Buffer = true;
// Response.ContentType = "application/octet-stream";
// Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.doc", HttpUtility.UrlEncode("军检验收单" + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff"), System.Text.Encoding.UTF8)));
// Response.BinaryWrite(ms.ToArray());
// //Response.End();
// Response.Flush();
// doc = null;
// ms.Close();
// ms.Dispose();
//} #endregion
} /// <summary>
/// 设置字体格式
/// </summary>
/// <param name="doc"></param>
/// <param name="table"></param>
/// <param name="setText"></param>
/// <returns></returns>
public XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText)
{
//table中的文字格式设置
var para = new CT_P();
var pCell = new XWPFParagraph(para, table.Body);
pCell.Alignment = ParagraphAlignment.CENTER; //字体居中
pCell.VerticalAlignment = TextAlignment.CENTER; //字体居中 var r1c1 = pCell.CreateRun();
r1c1.SetText(setText);
r1c1.FontSize = ;
r1c1.SetFontFamily("华文楷体", FontCharRange.None); //设置雅黑字体 return pCell;
} /// <summary>
/// 设置单元格格式
/// </summary>
/// <param name="doc">doc对象</param>
/// <param name="table">表格对象</param>
/// <param name="setText">要填充的文字</param>
/// <param name="align">文字对齐方式</param>
/// <param name="textPos">rows行的高度</param>
/// <returns></returns>
public XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText, ParagraphAlignment align,
int textPos)
{
var para = new CT_P();
var pCell = new XWPFParagraph(para, table.Body);
//pCell.Alignment = ParagraphAlignment.LEFT;//字体
pCell.Alignment = align; var r1c1 = pCell.CreateRun();
r1c1.SetText(setText);
r1c1.FontSize = ;
r1c1.SetFontFamily("华文楷体", FontCharRange.None); //设置雅黑字体
r1c1.SetTextPosition(textPos); //设置高度 return pCell;
}
}
}

运行效果

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

添加下列代码

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

必须是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. 原生js添加博客点击鼠标出小心心效果~~

    昨天刚申请成功JS权限,心血来潮想添加点东西,记得之前看到别人家博客首页点击鼠标的时候会出现炫酷的 “小心心”,自己也来搞一个.没有用jquery啥的框架,原生js写起来麻烦了点,不过主要是怕博客首页 ...

  2. phpmail发送phpexcel生成的附件,php导出的Excel 作为邮件附件发送

    PHP发送邮件戳这里https://www.cnblogs.com/wangzhaobo/p/8931328.html PHP导出excel戳这里https://www.cnblogs.com/wan ...

  3. Pycharm 项目无法导入自己写的模块(问题记录贴)

    问题背景: 从外部导入一个Python项目后,发现包错误,如图: 解决步骤: 1.将目录下的"5-6——apriori.py"复制一份,重命名为”apriori.py": ...

  4. 使用Kazoo操作ZooKeeper服务治理

    单机服务的可靠性及可扩展性有限,某台服务宕机可能会影响整个系统的正常使用:分布式服务能够有效地解决这一问题,但同时分布式服务也会带来一些新的问题,如:服务发现(新增或者删除了服务如何确保能让客户端知道 ...

  5. Extjs在树上加右键菜单--2019-04-15

    效果图如下: 使用规则:将监听加到按钮或树上,监听代码如下. 代码如下: listeners : { //节点单击事件 'rowcontextmenu' : function(view, record ...

  6. SQL Server 获取 ActiveDirectory 用户信息

    SQL Server 是可以访问活动目录中的用户信息的. 1. 先用domain administrator建立一个linked server 2. 然后用OpenQuery去查询活动目录   大家可 ...

  7. UICollectionView使用相关博文链接

    有关UICollectionView的几篇文章:1.UICollectionView简介及简单示例: http://puttin.github.io/blog/2013/04/08/a-simple- ...

  8. 1.3 正则表达式和python语言-1.3.8 创建字符集([ ])

    1.3.8 创建字符集([ ]) (2018-05-0815:24:00) 下面的示例将说明对于 r2d2|c3po 的限制将比[cr][23][dp][o2]更为严格 import re # 下面的 ...

  9. 【C语言编程练习】新娘与新郎

    1. 题目要求 新郎A,B,C与新娘 X,Y,Z.有人不知道她们谁和谁结婚了,询问了6位新人中的三位,A说他将和X结婚,X说她的未婚夫是C,C说她会和Z结婚,一听就知道是全是假话,请编程找出谁和谁结婚 ...

  10. NOI2017总结

    时光剥离你我像一袭华美衣衫 却要被追悔爬满 退役之战,去得匆匆,看得蒙蒙. 第三次全国赛,曾经的APIO初二选手也走到了时间的尽头. 第一次走向全国舞台的激动与忐忑,第一次在大赛中失利的沮丧与绝望,第 ...