DataTable 导出Excel 下载 (NPOI)
public class ExcelHelper
{
public void DownLoadExcelNew(System.Data.DataTable data, Hashtable h, string fileName)
{
MemoryStream m = DataTableToMemoryNew(data, h);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = false;
System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", m.Length.ToString());
m.WriteTo(System.Web.HttpContext.Current.Response.OutputStream);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
public MemoryStream DataTableToMemoryNew(System.Data.DataTable data, Hashtable h)
{
IWorkbook wk = new HSSFWorkbook();
ISheet sheet = wk.CreateSheet("work");
IRow rowCloumn = sheet.CreateRow();
int count = ;
for (int i = ; i < data.Columns.Count; i++)
{
if (h.ContainsKey(data.Columns[i].ColumnName.ToLower()))
{
rowCloumn.CreateCell(count).SetCellValue(h[data.Columns[i].ColumnName.ToLower()].ToString());
count++;
}
}
for (int r = ; r < data.Rows.Count; r++)
{
//创建行
IRow row = sheet.CreateRow(r + );
count = ;
for (int c = ; c < data.Columns.Count; c++)
{
if (h.ContainsKey(data.Columns[c].ColumnName.ToLower()))
{
row.CreateCell(count).SetCellValue(data.Rows[r][c].ToString());
count++;
}
}
}
MemoryStream memoryStream = new MemoryStream();
wk.Write(memoryStream);
return memoryStream;
}
}
DataTable 导出Excel 下载 (NPOI)的更多相关文章
- ASP.NET导出Excel(利用NPOI和EPPlus库,无需安装Office)
网上提供了很多Asp.net中操作Excel的方法,其中大部分是调用微软的Office组件,下面提供三个无须安装Office即可从Asp.net输出Excel的方法. 1 简单方法 //下面代码输出的 ...
- asp.net DataTable导出Excel 自定义列名
1.添加引用NPOI.dll 2.cs文件头部添加 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System.IO; 3.代码如 ...
- Datatable导出Excel
; IRow headerRow = sheet.CreateRow(); ; ; ; iRowIndex++; } ; i < icolIndex; i++) { sheet.AutoSize ...
- c# Datatable导出Excel
using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; ...
- C# DataTable导出EXCEL后身份证、银行卡号等长数字信息显示乱码解决
在DataTable导出EXCEL后发现有些格式显示有问题,比如身份证.银行卡号等大于11位的数字显示为科学计数法.13681-1等 带中划线的两段数字显示为日期格式等. 处理方法如下: public ...
- 【转】C# DataTable 导出 Excel 进阶 多行表头、合并单元格、中文文件名乱码
本文原创地址:http://blog.csdn.net/ranbolwb/article/details/8083983 ,转载请保留本行. 本例子是上一篇 DataTable 导出 Excel 的进 ...
- NPOI DataTable导出excel
/// <summary> /// DataTable导出到Excel文件 /// </summary> /// <param name="dtSource&q ...
- WinForm 使用 NPOI 2.2.1从datatable导出Excel
最新的NOPI应该是2.3了,但在官网上还是2.2.1. 也是第一次使用NPOI来导出Excel文件. 在写的时候搜不到2.2.1的教程,搜了一个2.2.0的教程. 不过也没什么问题,NPOI是真的方 ...
- 【ASP.NET】DataTable导出EXCEL,弹窗提示下载保存(完整代码)
//新建ASPX protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); Data ...
随机推荐
- 关于ubuntu 系统
1. ubuntu怎么卸载自带的软件中心?sudo apt-get remove software-center 2. 重新安装ubuntu软件中心!sudo apt-get install --re ...
- css中“zoom:1”是什么意思
继承性: 无 兼容性: IE 基本语法 zoom : normal | number 语法取值 normal : 默认值.使用对象的实际尺寸 number : 百分数 | 无符号浮点实数.浮点实数 ...
- dedecms文章的更新时间问题 每次更改文章时间变成最新的
dedecms 每次更改文章,更新时间这里每次改了后再来看又变成当前最新时间的了. 解决方法: 查找后台目录的 templets/article_edit.htm 这个文件. 然后打开,查找如下代码: ...
- pmp论坛
PMP论坛: http://www.px101.com/specialpmp/ http://www.pmp.cn/ http://www.pmptuan.com/ http://www.mypm.n ...
- js深入研究之无法理解的js类代码,extend扩展
<script type="text/javascript"> function Person(name) { this.name = name; } Person.p ...
- java开发经验分享(四)
四. 关于测试 1. 在整个项目计划中,测试时间安排的合理性,对测试阶段的情况应作充分预计,不可为了赶发布点而忽略质量. 2. 务必清楚产品包.更新包.bug包的提交规范.具体请参照<开发规范手 ...
- db2的select语句在db2 client上执行正确,JDBC连接数据库时报错
db2的select语句在db2 client上执行正确,JDBC连接数据库时报错. sql语句是:select ...from QUALIFIER.tableName fetch first 21 ...
- iphone开发之适配iphone5
iphone5出来了,从不用适配的我们也要像android一样适配不同分辨率的屏幕了. 公司产品新版本需要适配iphone5,经过一番折腾算是搞定了.下面分享给大家: iphone5的屏幕分辨 ...
- 使用PLSql连接Oracle时报错ORA-12541: TNS: 无监听程序
非常多时候为了优化我们的启动项把oracle的服务禁止了.但是重新启动启动之后使用PLSQL登陆oracle时会出现无监听程序,这说明我们有一些服务没有启动.我们先查看一下oracle的服务是否启动, ...
- [Node.js] Use "prestart" in scripts
Usually we run : npm start to start an app, then we we might call other script to do something: npm ...