NPOI 导出Excel表报错
当导出2007格式的时候,打开文件总是报错“发现 xxx中的部分内容有问题。是否让我们尽量尝试恢复?”。

导出的程序:
protected void btnValidateInternalData_Click(object sender, EventArgs e)
{
if (!FileUploadEmployee.HasFile)
{
ShowMessage("请先选择文件。");
return;
} var employeeData = GetDataTable();
if (employeeData.Rows.Count == )
{
ShowMessage("文件数据为空。");
return;
} ValidateEmployeeField(employeeData); var fileName = FileUploadEmployee.FileName;
DataSet dataSet = new DataSet();
dataSet.Tables.Add(employeeData);
byte[] fileBinary = null;
fileBinary = ExcelHelper.ExportToExcel(dataSet, DataFormat.Excel2007);
var reportFileName = Path.GetFileNameWithoutExtension(fileName) + "Validated" + DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(fileName);
Response.Buffer = true;
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", @"attachment;filename=""" + reportFileName + @"""");
Response.AddHeader("Content-Length", fileBinary.Length.ToString());
Response.ContentType = "application/vnd.ms-excel"; //"application/octet-stream";
Response.BinaryWrite(fileBinary);
//说明:当仅使用Response.End()发送缓冲输出时,打开导出的excel会出现 部分内容有问题 的错误。
Response.Flush();//向客户端发送当前所有缓冲的输出。
Response.End();//将当前所有缓冲的输出发送到客户端,停止该页的执行,并引发 EndRequest 事件。
}
从dataset创建excel表格的方法:
public static byte[] ExportToExcel(DataSet origDataSet, DataFormat dataFormat, string culture = "", bool shouldCheckHideColumnsForReport = false)
{
IWorkbook workbook = null;
switch (dataFormat)
{
case DataFormat.Excel97_2003:
workbook = new HSSFWorkbook();
break;
case DataFormat.Excel2007:
workbook = new XSSFWorkbook();
break;
} ICellStyle cellstyleDate = workbook.CreateCellStyle();
short df = workbook.CreateDataFormat().GetFormat(DateUtils.FORMAT_DATETIME);
if (culture == new Language(LanguageEnum.zhcn).Code)
df = workbook.CreateDataFormat().GetFormat(DateUtils.FORMAT_DATETIME);
cellstyleDate.DataFormat = df; foreach (DataTable dt in origDataSet.Tables)
{
ISheet sheet = workbook.CreateSheet(dt.TableName); int columnIndex = ;
IRow row = sheet.CreateRow();
ICell cell;
foreach (DataColumn dc in dt.Columns)
{
string columnName = dc.ColumnName;
if (shouldCheckHideColumnsForReport && ShouldSkipColumnForReport(columnName))
{
//dont add this column in this external report
continue;
}
cell = row.CreateCell(columnIndex);
cell.SetCellValue(dc.ColumnName); columnIndex++;
} List<int> lockedColumnList = new List<int>();
int rowIndex = ;
foreach (DataRow dr in dt.Rows)
{
row = sheet.CreateRow(rowIndex);
columnIndex = ;
foreach (DataColumn dc in dt.Columns)
{
string columnName = dc.ColumnName;
if (shouldCheckHideColumnsForReport && ShouldSkipColumnForReport(columnName))
{
//dont add this column in this external report
continue;
}
cell = row.CreateCell(columnIndex); if (dc.DataType == Type.GetType("System.DateTime"))
{
DateTime dateTime = DateTime.MinValue;
if (DateTime.TryParse(dr[columnName].ToString(), out dateTime))
{
cell.CellStyle = cellstyleDate;
cell.SetCellValue(dateTime);
}
else
cell.SetCellValue(dr[columnName].ToString());
}
else if (dc.DataType == Type.GetType("System.Decimal"))
{
double decimalValue = ;
if (double.TryParse(dr[columnName].ToString(), out decimalValue))
{
cell.SetCellValue(decimalValue);
}
else
cell.SetCellValue(dr[columnName].ToString());
}
else
{
string columnValue = dr[columnName].ToString();
cell.SetCellValue(columnValue);
}
columnIndex++;
}
rowIndex++;
}
}
Response.Flush() Response.End()的区别
//Response.Flush() 将缓存中的内容立即显示出来
//Response.End() 缓冲的输出发送到客户端 停止页面执行
//例:
//Response.Write("520");
//Response.End(); \\执行到这里结束页面显示"520" 下面的语句不再执行 (和没写一样)
//Response.Write("025");
//如果是Response.Flush() 将缓存中的内容立即显示出来,然后再执行后面的语句
NPOI 导出Excel表报错的更多相关文章
- NPOI导出Excel (C#) 踩坑 之--The maximum column width for an individual cell is 255 charaters
/******************************************************************* * 版权所有: * 类 名 称:ExcelHelper * 作 ...
- NPOI导出EXCEL报_服务器无法在发送 HTTP 标头之后追加标头
虽然发表了2篇关于NPOI导出EXCEL的文章,但是最近再次使用的时候,把以前的代码粘贴过来,居然报了一个错误: “服务器无法在发送 HTTP 标头之后追加标头” 后来也查询了很多其他同学的文章,都没 ...
- NPOI导出Excel及使用问题
NPOI导出Excel及使用问题 因为最近公司质管部门提出了一个统计报表的需求:要求导出一个2016及2017年度深圳区域的所有供应商的费用成本计算--一个22列的Excel表,其中还包括多列的合并单 ...
- Asp.Net 使用Npoi导出Excel
引言 使用Npoi导出Excel 服务器可以不装任何office组件,昨天在做一个导出时用到Npoi导出Excel,而且所导Excel也符合规范,打开时不会有任何文件损坏之类的提示.但是在做导入时还是 ...
- NPOI导出EXCEL 打印设置分页及打印标题
在用NPOI导出EXCEL的时候设置分页,在网上有查到用sheet1.SetRowBreak(i)方法,但一直都没有起到作用.经过研究是要设置 sheet1.FitToPage = false; 而 ...
- .NET NPOI导出Excel详解
NPOI,顾名思义,就是POI的.NET版本.那POI又是什么呢?POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office的文件. 支持的文件格式包括xls, ...
- NPOI导出Excel(含有超过65335的处理情况)
NPOI导出Excel的网上有很多,正好自己遇到就学习并总结了一下: 首先说明几点: 1.Excel2003及一下:后缀xls,单个sheet最大行数为65335 Excel2007 单个sheet ...
- [转]NPOI导出EXCEL 打印设置分页及打印标题
本文转自:http://www.cnblogs.com/Gyoung/p/4483475.html 在用NPOI导出EXCEL的时候设置分页,在网上有查到用sheet1.SetRowBreak(i)方 ...
- java导出excel报错:getOutputStream() has already been called for this response
对于java导出excel报错的问题,查了很多都说是在使用完输出流以后调用以下两行代码即可 out.clear(); out = pageContext.pushBody(); 但这也许是页面上输出时 ...
随机推荐
- MATLAB用“fitgmdist”函数拟合高斯混合模型(一维数据)
MATLAB用“fitgmdist”函数拟合高斯混合模型(一维数据) 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 在MATLAB中“fitgmdis ...
- 2020牛客寒假算法基础集训营1 I-nico和niconiconi
#include <bits/stdc++.h> #define dbg(x) cout << #x << "=" << x < ...
- HDU 4544 湫湫系列故事——消灭兔子 (优先队列)
湫湫减肥 越减越肥! 最近,减肥失败的湫湫为发泄心中郁闷,在玩一个消灭免子的游戏. 游戏规则很简单,用箭杀死免子即可. 箭是一种消耗品,已知有M种不同类型的箭可以选择,并且每种箭都会对兔子 ...
- Gin_Cookie
1. cookie HTTP是无状态协议,服务器不能记录浏览器的访问状态,也就是说服务器不能区分两次请求是否由同一个客户端发出 Cookie就是解决HTTP协议无状态的方案之一,中文是小甜饼的意思 C ...
- 【Python】画一个心形
#!/usr/bin/env python # -*- coding:utf-8 -*- import turtle import time # 画心形圆弧 def hart_arc(): for i ...
- spark 为什么要用broadcast[转]
为什么要用broadcast? 21down vote If you have huge array that is accessed from Spark Closures, for example ...
- (转)classload和class.forname()区别
转自:http://carl-java.iteye.com/blog/978680 java中class.forName和classLoader都可用来对类进行加载.前者除了将类的.class文件加载 ...
- dgango 反射
相关 """ 反射 由字符串反向找 变量.函数.类 """ import sys class Person(object): def __i ...
- JavaScript的jQuery
JavaScript的jQuery 不通过JavaScript的原生代码,如document.getElementById("") 而是通过jQuery的$符号选择器. jQuer ...
- PolandBall and Forest
PolandBall lives in a forest with his family. There are some trees in the forest. Trees are undirect ...