Kri*_*ian 15 c# asp.net excel file-upload filestream
我需要一种从流中读取Excel文件的方法.它似乎不适用于ADO.NET的处理方式.
方案是用户通过FileUpload上传文件,我需要从文件中读取一些值并导入到数据库.
由于多种原因,我无法将文件保存到磁盘,也没有理由这样做.
那么,有谁知道从FileUpload流中读取Excel文件的方法?
Kri*_*ian 14
似乎我自己找到了这个问题的灵魂.
http://www.codeplex.com/ExcelDataReader
这个库似乎很好用,它需要一个流来读取excel文件.
ExcelDataReader reader = new ExcelDataReader(ExcelFileUpload.PostedFile.InputStream);
Run Code Online (Sandbox Code Playgroud)
这可以通过EPPlus轻松完成。
//the excel sheet as byte array (as example from a FileUpload Control)
byte[] bin = FileUpload1.FileBytes;
//gen the byte array into the memorystream
using (MemoryStream ms = new MemoryStream(bin))
using (ExcelPackage package = new ExcelPackage(ms))
{
//get the first sheet from the excel file
ExcelWorksheet sheet = package.Workbook.Worksheets[1];
//loop all rows in the sheet
for (int i = sheet.Dimension.Start.Row; i <= sheet.Dimension.End.Row; i++)
{
//loop all columns in a row
for (int j = sheet.Dimension.Start.Column; j <= sheet.Dimension.End.Column; j++)
{
//do something with the current cell value
string currentCellValue = sheet.Cells[i, j].Value.ToString();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Infragistics有一个Excel 组件,可以从流中读取 Excel 文件。
我在这里的一个项目中使用它并且效果很好。
此外,可以轻松修改开源myXls 组件以支持此功能。XlsDocument 构造函数仅支持从文件名给定的文件加载,但它通过创建 FileStream 然后读取 Stream 来工作,因此将其更改为支持从流加载应该很简单。
编辑:我看到您找到了解决方案,但我只是想指出,我更新了该组件的源代码,以便它现在可以直接从流中读取 Excel 文件。:-)
| 归档时间: |
|
| 查看次数: |
47059 次 |
| 最近记录: |