NPOI 通过excel模板写入数据并导出
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模板写入数据并导出的更多相关文章
- java poi 从服务器下载模板写入数据再导出
最近写了一个,Excel 的 写入和导出. 需求是这样的. 在新建合同的时候,会有导出合同的数据, 导出的模板是固定的,,需要在模板里面写入合同的信息. first : 下载模板 ...
- NPOI操作excel之写入数据到excel表
在上一篇<NPOI操作excel之读取excel数据>我们把excel数据写入了datatable中,本篇就讲如何把datatable数据写入excel中. using System; u ...
- 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的路由方案,与原来的方案在使用上差别不 ...
- 用NPOI从DataTable到Excel,向Excel模板填充数据
DataTable---->Excel,填充数据 private IWorkbook workbook = null; private ISheet sheet = null; private ...
- 在服务端C#如何利用NPOI构建Excel模板
目前本人接触过两种模板导出的方式:(1)C#利用NPOI接口制作Excel模板,在服务端用数据渲染模板(2)在前端利用前人搭建好的框架,利用office编写xml制作模板,在客户端进行数据的渲染,导出 ...
- POI向Excel中写入数据及追加数据
import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import ...
- .Net NPOI 根据excel模板导出excel、直接生成excel
一.根据Excel模板导出excel 1.导入NPOI.dll 2.DAL中添加类ExportExcel.cs using NPOI.SS.UserModel; using System; usin ...
- tablib把数据导出为Excel、JSON、CSV等格式的Py库(写入数据并导出exl)
#tablib把数据导出为Excel.JSON.CSV等格式的Py库 #python 3 import tablib #定义列标题 headers = ('1列', '2列', '3列', '4列', ...
- NPOI从数据库中调取数据直接导出到EXCEL中
关于NPOI NPOI是POI项目的.NET版本,是由@Tony Qu(http://tonyqus.cnblogs.com/)等大侠基于POI开发的,可以从http://npoi.codeplex. ...
随机推荐
- ReSharper 10.0.0.1 Ultimate 完美破解补丁使用方法
转自:http://www.leavescn.com/Page/Content.aspx?id=94 ReSharper 10.0.0.1 Ultimate 完美破解补丁使用方法,本资源来自互联网,感 ...
- openresty 安装
1.安装环境.版本说明: 操作系统: Centos 6.5 X86_64 openresty 版本: openresty-1.13.6.1.tar.gz 2.安装 openresty(nginx) 需 ...
- android启动画面隐藏状态栏全屏显示
1.在根部局给一个id,然后直接设置就行了layout.setSystemUiVisibility(View.INVISIBLE); 状态栏就没有了. 2.如果你只是想改变状态栏颜色的也可以 //5. ...
- 注册Docker官网账号 注册按钮不能点
出现如下问题:注册按钮不能点,解决办法,如下 关于docker hub上不能注册dockeID的问题 注意的是,google访问助手,用在线安装,360安全浏览器,再重启下该浏览器,省得装插件.
- NET Core微服务之路:自己动手实现Rpc服务框架,基于DotEasy.Rpc服务框架的介绍和集成
本篇内容属于非实用性(拿来即用)介绍,如对框架设计没兴趣的朋友,请略过. 快一个月没有写博文了,最近忙着两件事; 一:阅读刘墉先生的<说话的魅力>,以一种微妙的,你我大家都会经常遇见 ...
- [算法专题] 二分搜索&排序数组
基础知识 二分非递归写法: int binary_search(const int a[], const int size, const int val) { int lower = 0; int u ...
- c++ 日志输出库 spdlog 简介(2)
继续上一篇,example.cpp解析. 1.set_pattern 自定义日志格式 官方参考:https://github.com/gabime/spdlog/wiki/3.-Custom-form ...
- String、StringBuffer、StringBuild的区别
他们之间的区别主要在两个重大方面 一.处理速度上 StringBuild > StringBuffer > String 原因: String : 它定义为字符串的常量,定以后不能修改 S ...
- maya2019卸载/安装失败/如何彻底卸载清除干净maya2019注册表和文件的方法
maya2019提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装maya2019失败提示maya2019安装未完成,某些产品无法安装,也有时候想重新安装maya ...
- Android OOM 引发的思考
一.为何会出现OOM 因为Android系统的硬件资源是相当有限的,而且分配给一个应用的资源更为有限,尤其是内存.当应用突然申请的内存大于允许的最大值的时候,就会出现OOM. 如果想要获取App的内存 ...