NPOI导出Excel2007-xlsx格式文件,用于web时需要注意的问题-XSSFWorkbook处理问题
2016-08-18 09:46
567 查看
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方法:
问题2对应代码:
问题3对应代码:
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>(1); 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, 0, (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, 0, (int)fileSize); HttpContext.Current.Response.BinaryWrite(fileBuffer); fs.Close();
相关文章推荐
- NPOI导出Excel2007-xlsx格式文件,用于web时需要注意的问题-XSSFWorkbook处理问题
- NPOI导出Excel2007-xlsx格式文件,用于web时需要注意的问题-XSSFWorkbook处理问题
- NPOI 1.2.4 是否可以读取 excel2007的xlsx格式文件
- asp.net<Web版> ---GridView 数据导出到excel表,系列问题<一>---导出的Excel文件格式与文件扩展名指定的格式不一致
- asp.net NPOI导出xlsx格式文件,打开文件报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”
- Discuz! 中导出数据为 CSV 格式文件需要注意的事项
- 解决 struts2上传的excel2007文件,在用poi处理时通过new XSSFWorkbook(inputStream)出错
- Npoi Web 项目中(XSSFWorkbook) 导出出现无法访问已关闭的流的解决方法
- nginx的web目录下处理中文文件和文件夹以及编码格式问题
- 使用POI导出xls和xlsx格式要注意的问题
- Trac - 常见问题 - 导出的csv格式文件在Excel中处理unix时间戳
- Web下使用lucene.net需要注意的问题
- Web页面的数据导出excel时的格式问题
- Web页面的数据导出excel时的格式问题
- Web页面的数据导出excel时的格式问题
- 利用HttpHandler处理自定义控件中需要引用大量js文件问题
- 实现各种文件的下载方法:用于 WEB上的报表导出(jxl 实现Excel报表下载),核心思想就是将文件的输出流传给HttpResponseOutputStream 里就成了。
- 用UltraEdit做Rails编辑器需要注意文件格式
- Web页面的数据导出excel时的格式问题
- 权限问题实在头疼啊?不光是上传文件夹的权限,还有很多程序文件的特殊权限需要注意!