c# NPOI aspx导出数据
public static class XSSFWorkbook_Excel
{
/// <summary>
/// GetExcel
/// </summary>
/// <param name="list">结果结合</param>
/// <param name="parms_header">要输出的字段名</param>
/// <param name="filename">文件名</param>
/// <returns></returns>
public static bool GetExcel(IList list, IDictionary<string, string> parms_header, string filename)
{
HttpContext curContext = HttpContext.Current;
try
{
MemoryStream ms = new MemoryStream();
EntityListToExcel(list, parms_header, "Sheet1").Write(ms);
curContext.Response.Clear();
curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8) + ".xlsx");
curContext.Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
curContext.Response.ContentEncoding = Encoding.UTF8;
curContext.Response.ContentType = "application/ms-excel";
curContext.Response.BinaryWrite(ms.ToArray());
ms.Close();
ms.Dispose();
curContext.Response.Flush();
curContext.Response.End();
}
catch (Exception ex)
{
curContext.Response.Flush();
curContext.Response.End();
}
finally
{
curContext.Response.Flush();
curContext.Response.End();
}
return true;
} /// <summary>
/// GetExcel
/// </summary>
/// <param name="list">结果结合</param>
/// <param name="parms_header">要输出的字段名</param>
/// <param name="filename">文件名</param>
/// <param name="path">下载地址</param>
/// <returns></returns>
public static bool GetExcel(IList list, IDictionary<string, string> parms_header, string filename, out string path)
{
path = "/Export/" + filename + ".xlsx";
try
{
using (FileStream stm = File.OpenWrite(AppDomain.CurrentDomain.BaseDirectory + path))
{
EntityListToExcel(list, parms_header, "Sheet1").Write(stm);
}
return true;
}
catch (Exception ex)
{
return false;
}
}
public static XSSFWorkbook EntityListToExcel(IList list, IDictionary<string, string> parms_header, string sheetName)
{
try
{
XSSFWorkbook workbook = new XSSFWorkbook();
//workbook.SetSheetHidden(,)
//HSSFWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet(sheetName);
IRow row = sheet.CreateRow();
List<string> keys = parms_header.Keys.ToList();
for (int i = ; i < keys.Count; i++)
{
row.CreateCell(i).SetCellValue(parms_header[keys[i]]);
}
int rowIndex = ;
foreach (var item in list)
{
IRow rowTmp = sheet.CreateRow(rowIndex);
for (int i = ; i < keys.Count; i++)
{
string cellValue = "";
object properotyValue = null;
System.Reflection.PropertyInfo properotyInfo = null; if (keys[i].IndexOf(".") >= )
{
string[] properotyArray = keys[i].Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
string subClassName = properotyArray[];
string subClassProperotyName = properotyArray[];
System.Reflection.PropertyInfo subClassInfo = item.GetType().GetProperty(subClassName);
if (subClassInfo != null)
{
var subClassEn = item.GetType().GetProperty(subClassName).GetValue(item, null);
properotyInfo = subClassInfo.PropertyType.GetProperty(subClassProperotyName);
if (properotyInfo != null)
{
properotyValue = properotyInfo.GetValue(subClassEn, null);
}
}
}
else
{
properotyInfo = item.GetType().GetProperty(keys[i]);
if (properotyInfo != null)
{
properotyValue = properotyInfo.GetValue(item, null);
}
}
if (properotyValue != null)
{
cellValue = properotyValue.ToString();
if (cellValue.Trim() == "0001/1/1 0:00:00" || cellValue.Trim() == "0001/1/1 23:59:59")
{
cellValue = string.Empty;
}
}
rowTmp.CreateCell(i).SetCellValue(cellValue);
}
rowIndex++;
} for (int i = ; i <= parms_header.Count(); i++)
{
sheet.AutoSizeColumn(i);
} for (int columnNum = ; columnNum <= parms_header.Count(); columnNum++)
{
int columnWidth = sheet.GetColumnWidth(columnNum) / ;
for (int rowNum = ; rowNum <= sheet.LastRowNum; rowNum++)
{
IRow currentRow;
if (sheet.GetRow(rowNum) == null)
{
currentRow = sheet.CreateRow(rowNum);
}
else
{
currentRow = sheet.GetRow(rowNum);
} if (currentRow.GetCell(columnNum) != null)
{
ICell currentCell = currentRow.GetCell(columnNum);
int length = Encoding.Default.GetBytes(currentCell.ToString()).Length;
if (columnWidth < length)
{
columnWidth = length;
}
}
}
sheet.SetColumnWidth(columnNum, columnWidth * );
} return workbook;
}
catch (Exception ex)
{
throw ex;
}
}
}
jQuery.download = function (data) {
var inputs = '';
Object.keys(data).forEach(function (v) {
inputs = inputs + '<input type="hidden" name="' + v + '" value="' + data[v] + '" />';
})
jQuery('<form action="/xykj/system/" method="post">' + inputs + '</form>')
.appendTo('body').submit().remove();
};
function Export() {
var parm = {
export: 1,
}
$.each($("#search").serializeArray(), function () {
if (parm[this.name]) {
if (!parm[this.name].push) {
parm[this.name] = [parm[this.name]];
}
parm[this.name].push(this.value || '');
} else {
parm[this.name] = this.value || '';
}
})
$.download(parm);
}
使用Ajax提交会导致 Response.Flush();无效
c# NPOI aspx导出数据的更多相关文章
- 使用npoi.dll导出数据到excel
.net数据导出excel数据有多种方法,最常用的就是使用office组件,但随之而来的问题也很棘手,又要调权限又要确定是否安装office很是麻烦,最近一个项目中也有数据导出功能,随使用excel模 ...
- asp.net使用MVC4框架基于NPOI做导出数据到Excel表
NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. 使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 ...
- NPOI 通用导出数据到Excel 分类: C# Helper 2014-11-04 16:06 246人阅读 评论(0) 收藏
应用场景: 在项目中,经常遇到将数据库数据导出到Excel,针对这种情况做了个程序封装.工作原理:利用NPOI将SQL语句查询出的DataTable数据导出到Excel,所见即所得. 程序界面: ...
- NPOI导出数据到Excel
NPOI导出数据到Excel 前言 Asp.net操作Excel已经是老生长谈的事情了,可下面我说的这个NPOI操作Excel,应该是最好的方案了,没有之一,使用NPOI能够帮助开发者在没有安装微 ...
- 使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中
using System; using System.Collections.Generic; using System.Text; using System.IO; using NPOI.SS.Us ...
- 利用NPOI导出数据到Execl
相信很多童鞋都开发过Execl的导入导出功能,最近产品中无论是后台数据分析的需要,还是前端满足用户管理的方便,都有Execl导入导出的维护需求产生. 以前做这个功能,如果是web,利用HttpCont ...
- NPOI读取excel文件导出数据, 而此时文件正在打开中抛异常怎么办
项目中需要用到一些数值表格, 方便起见都是用excel来的. 而如果excel正打开中, 直接使用npoi制作的工具来导出数据的话, 在这一行将会异常: workbook = new XSSFWork ...
- winfrom 使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中
1.通过NUGET管理器下载nopi,在引入命令空间 using System; using System.Collections.Generic; using System.Text; using ...
- NET使用NPOI组件将数据导出Excel-通用方法 【推荐】
一.Excel导入及导出问题产生: 从接触.net到现在一直在维护一个DataTable导出到Excel的类,时不时还会维护一个导入类.以下是时不时就会出现的问题: 导出问题: 如果是as ...
随机推荐
- 大数据之路【第十二篇】:数据挖掘--NLP文本相似度
一.词频----TF • 假设:如果一个词很重要,应该会在文章中多次出现 • 词频——TF(Term Frequency):一个词在文章中出现的次数 • 也不是绝对的!出现次数最多的是“的”“是”“在 ...
- 阿里云 轻量应用服务器 上传一个HTML文件或者jsp文件 通过外网IP访问
我选择的是 宝塔Linux,现在就说一下如何往服务器里面传文件然后通过外网IP访问 1.打开宝塔Linux面板登录,安装tomcat,安装好后如图 2.点击tomcat有个文件标识处 3.出现如图 4 ...
- Map 集合按字母排序方法
@Testpublic void testMapSort() { Map<String, String> map = new HashMap<>(); map.put(&quo ...
- Vert.x 异步访问数据库 MySQL
Vert.x提供异步访问数据库的API,数据库操作是一个耗时操作,使用传统的同步模型,容易阻塞线程,导致整体性能下降,因此我们对于数据库操作,需要使用Vert.x提供的异步API. Vert.x提供的 ...
- 阿里云ESC服务器配置记录
购买服务器 上周赶着活动购买了一年阿里云服务器,记录一下配置过程: 选择服务器类型: linux服务器,网上说一般都用centOS的"比较新"的版本: 重置密码: 重置密码之后一定 ...
- git安装用法和常用命令
目录 git 下载 安装 git 工作中常用命令 Git常用命令汇总以及其它相关操作 或者 常出错误: 常用修改: 题外话(相关概念) git 下载 安装 1.下载Git,官网地址:https://g ...
- Python进阶(三)----函数名,作用域,名称空间,f-string,可迭代对象,迭代器
Python进阶(三)----函数名,作用域,名称空间,f-string,可迭代对象,迭代器 一丶关键字:global,nonlocal global 声明全局变量: 1. 可以在局部作用域声明一 ...
- 【JVM】jdk1.8-jetty-swap被占满问题排查
背景 线上服务收到报警,报警内容:虚拟机swap区占用比例超过80%,如图: 本文着重描述排查问题的过程,在这个过程中不断的猜测–>验证–>推翻–>再猜测–>再验证–>再 ...
- 如何解决div背景色半透明,里面的图片不透明问题
用rgba可以实现,不能用opacity 背景做成透明的背景图,opacity属性影响子集的,除非把两者独立开~
- Android为TV端助力之解析序列话的JSON
解析json时报错default constructor not found. class............. 比如 public class MediaRepBean implements P ...