C# 从上传的 Excel 文件中获取数据而不保存到文件系统

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/262341/
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-03 20:22:01  来源:igfitidea点击:

Get Data From An Uploaded Excel File Without Saving to File System

c#asp.netexcelweb-applicationsfile-upload

提问by

I have a requirement to allow a user of this ASP.NET web application to upload a specifically formatted Excel spreadsheet, fill arrays with data from the spreadsheet, and bind the arrays to a Oracle stored procedure for validation and insertion into the database. I must be able to read the data from the Excel spreadsheet without being able to save it to the web server's hard disk. This is the part I cannot figure out how to do. Here's a simple code example.

我需要允许此 ASP.NET Web 应用程序的用户上传特定格式的 Excel 电子表格,使用电子表格中的数据填充数组,并将数组绑定到 Oracle 存储过程以进行验证并插入到数据库中。我必须能够从 Excel 电子表格中读取数据,而不能将其保存到 Web 服务器的硬盘上。这是我无法弄清楚该怎么做的部分。这是一个简单的代码示例。

<%--ASP.NET Declarative--%>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Send File" OnClick="Button1_Click" />

// C# Code-Behind
protected void Button1_Click(object sender, EventArgs e) {
    var postedFile = FileUpload1.PostedFile;

    // ... Read file in memory and put in format to send to stored procedure ...

}

Can anyone help me with this? I appreciate anyone's consideration.

谁能帮我这个?我感谢任何人的考虑。

thx,
gabe

谢谢,
加布

回答by CMS

Use the FileUpload1.FileContentStream. I guess your Excel library can handle streams directly.

使用 FileUpload1。文件内容流。我猜你的 Excel 库可以直接处理流。

回答by TcKs

The COM libraries of Excel does not support loading file from another source than file. But there exists a lot of third-party components, which allows you read/write excel files.

Excel 的 COM 库不支持从文件以外的其他源加载文件。但是存在很多第三方组件,可以让您读/写excel文件。

Othervise you can see a documentation for th XLS file format at [MS-XLS]: Excel Binary File Format (.xls) Structure Specification.

另外,您可以在[MS-XLS]: Excel Binary File Format (.xls) Structure Specification 中查看有关 XLS 文件格式的文档。

Or you can use a same way of office files processing like in Sharepoint Server. See Microsoft.Office.Excel.Server.WebServices Namespace.

或者您可以使用与 Sharepoint Server 中相同的办公文件处理方式。请参阅Microsoft.Office.Excel.Server.WebServices 命名空间

回答by hearn

This is something I've been playing with recently.

这是我最近一直在玩的东西。

Check this post: Write an excel workbook to a memory stream .NET

查看这篇文章:将 excel 工作簿写入内存流 .NET

It points to a great library by Carlos Aguilar Mares, which lets you work with Excel workbooks as XML.

它指向 Carlos Aguilar Mares 的一个很棒的库,它允许您将 Excel 工作簿作为 XML 处理。

ExcelXMLWriter

ExcelXMLWriter

You dont need Excel installed on the server (which is kinda breaking the MS licensing anyway as you are accessing this over the web).

您不需要在服务器上安装 Excel(当您通过 Web 访问它时,这会破坏 MS 许可)。

You can load the Excel workbook as a stream using Workbook.Load(stream)

您可以使用以下方法将 Excel 工作簿加载为流 Workbook.Load(stream)

回答by Ricardo Villamil

Could you have your users upload a CSV file instead? Dealing with a plain text file would be much easier. I had a similar issue before and I asked the users and they were OK, saved me tons of work.

您可以让您的用户上传一个 CSV 文件吗?处理纯文本文件会容易得多。我之前遇到过类似的问题,我问过用户,他们很好,为我节省了大量的工作。

Good luck.

祝你好运。

回答by Christoph

maybe have look on csvreader, it reads csv, xls and xlsx:

也许看看 csvreader,它读取 csv、xls 和 xlsx:

http://www.csvreader.com

http://www.csvreader.com

回答by gabe

I found a great lightweight open source API on Codeplex for doing this called ExcelDataReader.

我在 Codeplex 上找到了一个很棒的轻量级开源 API,称为 ExcelDataReader。

It can transform an input stream of an excel file into a System.Data.DataSetobject (probably parsing using BIFF specs).

它可以将 excel 文件的输入流转换为System.Data.DataSet对象(可能使用 BIFF 规范进行解析)。

Here's the link:

这是链接:

http://www.codeplex.com/ExcelDataReader

http://www.codeplex.com/ExcelDataReader

Here's a code sample:

这是一个代码示例:

<%--ASP.NET Declarative--%>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Send File" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" />

// C# Code-Behind
protected void Button1_Click(object sender, EventArgs e) {
    // the ExcelDataReader takes a System.IO.Stream object
    var excelReader = new ExcelDataReader(FileUpload1.FileContent);
    FileUpload1.FileContent.Close();

    DataSet wb = excelReader.WorkbookData;
    // get the first worksheet of the workbook
    DataTable dt = excelReader.WorkbookData.Tables[0];

    GridView1.DataSource = dt.AsDataView();
    GridView1.DataBind();
}