C# 从流中读取excel文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/560435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:11:35  来源:igfitidea点击:

Read excel file from a stream

c#asp.netexcelfile-uploadfilestream

提问by Kristian

I need a way to read a Excel file from a stream. It doesn't seem to work with the ADO.NET way of doing things.

我需要一种从流中读取 Excel 文件的方法。它似乎不适用于 ADO.NET 的做事方式。

The scenario is that a user uploads a file through a FileUpload and i need to read some values from the file and import to a database.

场景是用户通过 FileUpload 上传文件,我需要从文件中读取一些值并导入到数据库中。

For several reasons I can'tsave the file to disk, and there is no reason to do so either.

出于多种原因,我无法将文件保存到磁盘,也没有理由这样做。

So, anyone know of a way to read a Excel file from a FileUpload stream?

那么,有人知道从 FileUpload 流中读取 Excel 文件的方法吗?

回答by Rune Grimstad

Infragisticshas an excel componentthat can read an excel file from a stream.

Infragistics有一个excel 组件,可以从流中读取 excel 文件。

I'm using it in a project here and it works well.

我在这里的一个项目中使用它,效果很好。

Also the open source myXls componentcould easily be modified to support this. The XlsDocument contstructor only supports loading from a file given by a file name, but it works by creating a FileStream and then reading the Stream, so changing it to support loading from streams should be trivial.

此外,可以轻松修改开源myXls 组件以支持这一点。XlsDocument 构造函数仅支持从由文件名给出的文件加载,但它通过创建 FileStream 然后读取 Stream 来工作,因此将其更改为支持从流加载应该是微不足道的。

Edit: I see that you found a solution but I just wanted to note that I updated the source code for the component so that it now can read an excel file directly from a stream. :-)

编辑:我看到您找到了一个解决方案,但我只是想说明我更新了组件的源代码,以便它现在可以直接从流中读取 excel 文件。:-)

回答by Kristian

It seems i found a soultion to the problem myself.

看来我自己找到了解决问题的方法。

http://www.codeplex.com/ExcelDataReader

http://www.codeplex.com/ExcelDataReader

This library seems to work nicely and it takes a stream to read the excel file.

这个库似乎工作得很好,它需要一个流来读取 excel 文件。

ExcelDataReader reader = new ExcelDataReader(ExcelFileUpload.PostedFile.InputStream);

回答by Joe Erickson

SpreadsheetGearcan do it:

SpreadsheetGear可以做到:

SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbookSet().Workbooks.OpenFromStream(stream);

You can try it for yourself with the free evaluation.

您可以通过免费评估亲自试用

Disclaimer: I own SpreadsheetGear LLC

免责声明:我拥有 SpreadsheetGear LLC

回答by VDWWD

This can be done easily with EPPlus.

这可以通过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();
        }
    }
}

回答by RBT

I use ClosedXMLnuget package to read excel content from stream. It has a constructor overload in XLWorkbookclass which takes stream pointing to an excel file (aka workbook).

我使用ClosedXMLnuget 包从流中读取 excel 内容。它在XLWorkbook类中有一个构造函数重载,它接受指向 excel 文件(又名工作簿)的流。

imported namespace at the top of your code file:

在代码文件顶部导入命名空间:

using ClosedXML.Excel;

Source code:

源代码:

var stream = /*obtain the stream from your source*/;
if (stream.Length != 0)
{
    //handle the stream here
    using (XLWorkbook excelWorkbook = new XLWorkbook(stream))
    {
        var name = excelWorkbook.Worksheet(1).Name;
        //do more things whatever you like as you now have a handle to the entire workbook.
        var firstRow = excelWorkbook.Worksheet(1).Row(1);
    }
}