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. C++探究foreach算法

    for_each在algorithm.h 中 template<class _InIt, class _Fn1> inline _Fn1 for_each(_InIt _First, _I ...

  2. s2 Docker环境的快速搭建方法

    常规linux下安装 centos7 下配置docker源并安装 cat >/etc/yum.repos.d/docker.repo< [dockerrepo] name=Docker R ...

  3. 根据Excel模板存储数据,并下载

    @RequestMapping("/exportList") @ResponseBody public Map<String, Object> exportList(H ...

  4. Unity3D中播放视频的方法

    播放视频其实和贴图非常相像,因为播放视频用到的 MovieTexture 属于贴图 Texture 的子类.Unity3D 支持的视频格式有很多,但是还是建议使用 ogv 格式的视频,使用其他格式依然 ...

  5. Django URLConf 进阶

    Django处理一个请求 项目启动后根据 settings  ROOT_URLCONF 决定项目根URLconf urlpatterns是django.conf.urls.url()实例的一个Pyth ...

  6. Django View 进阶

    返回404 from django.http import HttpResponse, HttpResponseNotFound def not_found(request): ) 或 return ...

  7. Xcode8.0 / OS X EI Capitan 10.11.6 提交报错90111

    改用新系统和新版xcode(都是正式版)后,提交App Store审核时报错: INFO ITMS-90111: "Beta Toolchain. 构建新的 App 和App 更新时,必须使 ...

  8. NET Core微服务之路:实战SkyWalking+Exceptionless体验生产环境下的追踪系统

    前言 当一个APM或一个日志中心实际部署在生产环境中时,是有点力不从心的. 比如如下场景分析的问题: 从APM上说,知道某个节点出现异常,或延迟过过高,却不能及时知道日志反馈情况,总不可能去相应的节点 ...

  9. CPP之内存分配

    new & delete expression 1. Introduction A new expression allocates and constructs an object of a ...

  10. 「WC2018」州区划分(FWT)

    「WC2018」州区划分(FWT) 我去弄了一个升级版的博客主题,比以前好看多了.感谢 @Wider 不过我有阅读模式的话不知为何 \(\text{LATEX}\) 不能用,所以我就把这个功能删掉了. ...