NPOI导出Excel2007-xlsx格式文件,用于web时需要注意的问题-XSSFWorkbook处理问题
1.今天再处理Excel2007、2010文件,格式.xlsx文件存在一个问题,在调用 Write方法之后关闭了传入的文件流。
2.今天针对此问题好一顿的测试:
2.1 在有文件构建时不是.xlsx文件格式会报错,构建不成。.xls文件是不行的。
2.2 XSSFWorkbook对象调用 write方法传去MemoryStream对象后,会自动关闭传入的参数。导致往Response.OutputStream会有问题?
HSSFWorkbook对象则不会,针对这个问题还专门查了HSSFWorkbook源代码下面有,本来想查XSSFWorkbook的源代码,现在还没公开呢。
3.再有导出.xlsx文件时,在打开时总报:
错误提示: Excel在“春天Excel2007.xlsx”中发现不可读取内容。是否恢复工作簿的内容?如果信任此工作簿的来源,请单击“是”。 单击“是”后:Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃。
那么这些问题如何处理呢?直接上代码如下:
HSSFWorkbook类对象Write方法:
/// <summary>
/// Write out this workbook to an Outputstream. Constructs
/// a new POI POIFSFileSystem, passes in the workbook binary representation and
/// Writes it out.
/// </summary>
/// <param name="stream">the java OutputStream you wish to Write the XLS to</param>
public override void Write(Stream stream)
{
byte[] bytes = GetBytes();
POIFSFileSystem fs = new POIFSFileSystem();
// For tracking what we've written out, used if we're
// going to be preserving nodes
List<string> excepts = new List<string>(); MemoryStream newMemoryStream = new MemoryStream(bytes);
// Write out the Workbook stream
fs.CreateDocument(newMemoryStream, "Workbook"); // Write out our HPFS properties, if we have them
WriteProperties(fs, excepts); if (preserveNodes)
{
// Don't Write out the old Workbook, we'll be doing our new one
excepts.Add("Workbook");
// If the file had WORKBOOK instead of Workbook, we'll Write it
// out correctly shortly, so don't include the old one
excepts.Add("WORKBOOK"); // Copy over all the other nodes to our new poifs
CopyNodes(this.filesystem, fs, excepts);
}
fs.WriteFileSystem(stream); fs.Dispose();
newMemoryStream.Dispose();
bytes = null;
}
问题2对应代码:
FileStream fileStream = new FileStream(HttpContext.Current.Server.MapPath("~/Resources/Template/" + strHeaderText + ".xlsx"), FileMode.Create, FileAccess.Write);
workbook.Write(fileStream);//调用这个后会关于文件流,在HSSFWorkbook不会关闭所以在处理时应注意
FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("~/Resources/Template/" + strHeaderText + ".xlsx"), FileMode.Open, FileAccess.Read);
long fileSize = fs.Length;
byte[] fileBuffer = new byte[fileSize];
fs.Read(fileBuffer, , (int)fileSize);
HttpContext.Current.Response.BinaryWrite(fileBuffer);
fs.Close();
问题3对应代码:
FileStream fileStream = new FileStream(HttpContext.Current.Server.MapPath("~/Resources/Template/" + strHeaderText + ".xlsx"), FileMode.Create, FileAccess.Write);
workbook.Write(fileStream);//调用这个后会关于文件流,在HSSFWorkbook不会关闭所以在处理时应注意
FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("~/Resources/Template/" + strHeaderText + ".xlsx"), FileMode.Open, FileAccess.Read);
long fileSize = fs.Length;
//加上设置大小下载下来的.xlsx文件打开时才没有错误
HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
byte[] fileBuffer = new byte[fileSize];
fs.Read(fileBuffer, , (int)fileSize);
HttpContext.Current.Response.BinaryWrite(fileBuffer);
fs.Close();
NPOI导出Excel2007-xlsx格式文件,用于web时需要注意的问题-XSSFWorkbook处理问题的更多相关文章
- asp.net NPOI导出xlsx格式文件,打开文件报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”
NPOI导出xlsx格式文件,会出现如下情况: 点击“是”: 导出代码如下: /// <summary> /// 将datatable数据写入excel并下载 /// </summa ...
- SPOOL 命令使用实例【oracle导出纯文本格式文件】
SPOOL 命令使用实例[oracle导出纯文本格式文件] SET echo off --在用start命令执行一个sql脚本时,是否显示脚本中正在执行的SQL语句: SET ...
- (转载)DBGridEh导出Excel等格式文件
DBGridEh导出Excel等格式文件 uses DBGridEhImpExp; {--------------------------------------------------------- ...
- 将Mysql的一张表导出至Excel格式文件
将Mysql的一张表导出至Excel格式文件 导出语句 进入mysql数据库,输入如下sql语句: select id, name, age from tablename into outfile ' ...
- 用 python 来操作 docx, xlsx 格式文件(一)(使用 xlsxwriter 库操作xlsx格式文件)
需要从数据库读取日志生成相应的 docx,xlsx 文件做相应的记录 所以自然要用到docx, xlsxwriter 库 但是这些库的应用场景非常广泛,任何需要对 word,excel 文件执行重复性 ...
- 用 python 来操作 docx, xlsx 格式文件(二)(使用 docx 库操作 docx 格式文件
docx 库 文章结构: 一.docx 基本用,创建 docx 文件并添加数据 二.深入理解文本格式(format),并设置所格式属性(attribute) 三.深入理解样式(styles),以及如何 ...
- R语言学习——R读取txt、csv、xls和xlsx格式文件
最近项目中运用到了R读取文件数据,所以把相关好用的.经过验证的方法总结了一下,有效避免下次入坑. 1. R读取txt文件 使用R读取txt文件直接使用read.table()方法进行读取即可,不需要加 ...
- NPOI导出Excel2007板
Excel2003有最大行限制相信大家在日常导出时都不会考虑再使用Excel2003,其实NPOI是一个听简单又好用的多里office组件的导出插件. 为了便于以后使用记录一下 第一步下载NPOI插件 ...
- 关于markdown(typora)的操作指南,以及导出为word格式文件插件(pandoc-2.6-windows-x86_64)的下载
Markdown简介 插件链接文章结尾处 目录 Markdown简介1. Markdown是什么?2. 谁创造了它?3. 为什么要使用它?4. 怎么使用?4.1 标题4.2 段落4.3 区块引用4.4 ...
随机推荐
- 【Ansible】Playbook实例
Learn to build Ansible playbooks with our guide, one step at a time In our previous posts, we introd ...
- CPC广告反作弊
原文:http://blog.csdn.net/xwm1000/article/details/45460957 CPC广告上线也2年了,从上线以来就一直存在着作弊和反作弊的斗争,刚开始的时候流量少, ...
- IETESTER ie10.local 下载
下载地址:ietester.ie10.exe.local.zip 技术交流QQ群: 15129679
- Jmeter-Maven-Plugin高级应用:Log Levels
Log Levels Pages 12 Home Adding additional libraries to the classpath Advanced Configuration Basic C ...
- 空格在Html中的表示方法( 含义)
转自:http://www.cnblogs.com/hailexuexi/archive/2010/07/25/1784611.html 在web开发经常会遇到如: 这样的字符.它其实是Html将 ...
- C++:cin、cin.getline()、getline()的用法
主要内容: 1.cin用法 2.cin.getline()用法 3.getline()用法 3.注意的问题 一.cin>> 用法1:输入一个数字或字符 #include <iostr ...
- Python访问MySQL数据库
#encoding: utf-8 import mysql.connector __author__ = 'Administrator' config={'host':'127.0.0.1',#默认1 ...
- sqoop安装部署(笔记)
sqoop是一个把关系型数据库数据抽向hadoop的工具.同时,也支持将hive.pig等查询的结果导入关系型数据库中存储.由于,笔者部署的hadoop版本是2.2.0,所以sqoop的版本是:sqo ...
- ArcEngine中的缩放地图
在ArcEngine地图操作中,缩放地图的功能经常用到,这里做一个小结. 缩放地图一般可分为以下几种情况: 1.缩放地图:与放大地图相对,一般是手动绘制区域或固定比例缩放,可调用命令或Expand函数 ...
- validationEngine 表单验证插件使用
废话少说,直接上代码,可拷贝直接运行: <!DOCTYPE html> <html lang="zh"> <head> <meta cha ...