private void ToExcel(string id)
{
//模板文件
string TempletFileName = Server.MapPath("template.xls");
//导出文件
string ReportFileName = Server.MapPath("out.xls");
FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read);
HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
HSSFSheet ws = hssfworkbook.GetSheet("Sheet1");
//添加或修改WorkSheet里的数据
System.Data.DataTable dt = new System.Data.DataTable();
dt = DbHelperMySQLnew.Query("select * from t_jb_info where id='" + id + "'").Tables[0];
#region
if (dt.Rows.Count > 0)
{
if (!string.IsNullOrEmpty(dt.Rows[0]["blrq"].ToString()))
{
DateTime blrq = DateTime.Parse(dt.Rows[0]["blrq"].ToString());
ws.GetRow(7).GetCell(4).SetCellValue("56565");
ws.GetRow(7).GetCell(6).SetCellValue(blrq.Month.ToString());
ws.GetRow(7).GetCell(8).SetCellValue(blrq.Day.ToString());
ws.GetRow(1).GetCell(2).SetCellValue(blrq.Year.ToString());
ws.GetRow(1).GetCell(4).SetCellValue(blrq.Month.ToString());
ws.GetRow(1).GetCell(6).SetCellValue(blrq.Day.ToString());

//strnian = strblnian = blrq.Year.ToString();
//stryue = strblyue = blrq.Month.ToString();
//strri = strblri = blrq.Day.ToString();

}
ws.GetRow(1).GetCell(10).SetCellValue(id);

// strbh = id;
ws.GetRow(2).GetCell(1).SetCellValue(dt.Rows[0]["fyr"].ToString());
ws.GetRow(2).GetCell(4).SetCellValue(dt.Rows[0]["dizhi"].ToString());

ws.GetRow(2).GetCell(10).SetCellValue(dt.Rows[0]["lxdh"].ToString());
ws.GetRow(3).GetCell(1).SetCellValue(dt.Rows[0]["bfyr"].ToString());
ws.GetRow(5).GetCell(0).SetCellValue(dt.Rows[0]["wtnr"].ToString());
if (!string.IsNullOrEmpty(dt.Rows[0]["chuliyijian"].ToString()))
{
ws.GetRow(6).GetCell(1).SetCellValue(dt.Rows[0]["chuliyijian"].ToString());

}
if (!string.IsNullOrEmpty(dt.Rows[0]["ldps"].ToString()))
{
ws.GetRow(8).GetCell(1).SetCellValue(dt.Rows[0]["ldps"].ToString());

}
if (!string.IsNullOrEmpty(dt.Rows[0]["psrq"].ToString()))
{
DateTime blrq = DateTime.Parse(dt.Rows[0]["psrq"].ToString());
ws.GetRow(10).GetCell(4).SetCellValue(blrq.Year.ToString());
ws.GetRow(10).GetCell(6).SetCellValue(blrq.Month.ToString());
ws.GetRow(10).GetCell(8).SetCellValue(blrq.Day.ToString());

}
if (!string.IsNullOrEmpty(dt.Rows[0]["jgrq"].ToString()))
{
DateTime blrq = DateTime.Parse(dt.Rows[0]["jgrq"].ToString());
ws.GetRow(12).GetCell(4).SetCellValue(blrq.Year.ToString());
ws.GetRow(12).GetCell(6).SetCellValue(blrq.Month.ToString());
ws.GetRow(12).GetCell(8).SetCellValue(blrq.Day.ToString());

}
if (!string.IsNullOrEmpty(dt.Rows[0]["jieguozt"].ToString()))
{
ws.GetRow(11).GetCell(1).SetCellValue(dt.Rows[0]["jieguozt"].ToString());

}
}
#endregion
ws.ForceFormulaRecalculation = true;

using (FileStream filess = File.OpenWrite(ReportFileName))
{
hssfworkbook.Write(filess);
}

//filess.Close();

System.IO.FileInfo filet = new System.IO.FileInfo(ReportFileName);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode("55.xls"));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", filet.Length.ToString());

// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";

// 把文件流发送到客户端
Response.WriteFile(filet.FullName);
// 停止页面的执行

Response.End();
}

NPOI 通过excel模板写入数据并导出的更多相关文章

  1. java poi 从服务器下载模板写入数据再导出

    最近写了一个,Excel 的 写入和导出.   需求是这样的.   在新建合同的时候,会有导出合同的数据,    导出的模板是固定的,,需要在模板里面写入合同的信息. first   :  下载模板 ...

  2. NPOI操作excel之写入数据到excel表

    在上一篇<NPOI操作excel之读取excel数据>我们把excel数据写入了datatable中,本篇就讲如何把datatable数据写入excel中. using System; u ...

  3. ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据

    ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案   ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...

  4. 用NPOI从DataTable到Excel,向Excel模板填充数据

    DataTable---->Excel,填充数据 private IWorkbook workbook = null; private ISheet sheet = null; private ...

  5. 在服务端C#如何利用NPOI构建Excel模板

    目前本人接触过两种模板导出的方式:(1)C#利用NPOI接口制作Excel模板,在服务端用数据渲染模板(2)在前端利用前人搭建好的框架,利用office编写xml制作模板,在客户端进行数据的渲染,导出 ...

  6. POI向Excel中写入数据及追加数据

    import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...

  7. .Net NPOI 根据excel模板导出excel、直接生成excel

    一.根据Excel模板导出excel 1.导入NPOI.dll  2.DAL中添加类ExportExcel.cs using NPOI.SS.UserModel; using System; usin ...

  8. tablib把数据导出为Excel、JSON、CSV等格式的Py库(写入数据并导出exl)

    #tablib把数据导出为Excel.JSON.CSV等格式的Py库 #python 3 import tablib #定义列标题 headers = ('1列', '2列', '3列', '4列', ...

  9. NPOI从数据库中调取数据直接导出到EXCEL中

    关于NPOI NPOI是POI项目的.NET版本,是由@Tony Qu(http://tonyqus.cnblogs.com/)等大侠基于POI开发的,可以从http://npoi.codeplex. ...

随机推荐

  1. Authorization Security for Mongodb

    To keep security for the mongodb server, we can create an authorized machanism. db.createUser( { use ...

  2. Html5与Css3知识点拾遗(七)

    布局 实例:规范的命名和编排 <body> <div class="page"><!--page开始--> <header class=& ...

  3. 用Django ORM实现树状结构

    前言 之前看对于用关系数据库实现树状结构的方法就知道一直做自关联的表,但是感觉自关联查询太慢了,最近看到一篇文章,感觉视野开拓了好多,文章:数据库表设计,没有最好只有最适合来自:微信. 下面就针对这里 ...

  4. HTML之<meta>使用和说明

    关于<meta>,我们都不陌生,随意打开一个网页查看源代码就可以看到<head>里出现它的身影. 简单来说,<meta>是描述 HTML 文档的元数据.例如网页描述 ...

  5. Centos7中docker开启远程访问

    在作为docker远程服务的centos7机器中配置: 1.在/usr/lib/systemd/system/docker.service,配置远程访问.主要是在[Service]这个部分,加上下面两 ...

  6. Learning WCF:Fault Handling

    There are two types of Execptions which can be throwed from the WCF service. They are Application ex ...

  7. fiddler 使用方法汇总

    作为网络开发人员,怎能不使用一些抓包工具呢?fiddler是个不错的选择. 不过,一般情况下,我们往往使用浏览器自带的控制台的[网络]选项就可以达到查看数据的通信情况了,当然,一些浏览器不容易捕捉的事 ...

  8. python中两种栈实现方式的性能对比

    在计算机的世界中,同一个问题,使用不同的数据结构和算法实现,所使用的资源有很大差别 为了方便量化python中算法的资源消耗,对性能做测试非常有必要,这里针对stack做了python语言 下的性能分 ...

  9. 机器学习基石笔记:08 Noise and Error

    噪声:误标.对同一数据点的标注不一致.数据点信息不准确...... 噪声是针对整个输入空间的. 存在噪声的情况下,VC bound依旧有用: 存在噪声,就是f------>p(y|x),f是p的 ...

  10. [POC]SuiteCRM 7.10.7 - 'record' SQL Injection

    #################################################################### # Exploit Title: SuiteCRM - 're ...