从上传的 Excel 文件中获取数据而不保存到文件系统 [英] Get Data From An Uploaded Excel File Without Saving to File System

查看:34
本文介绍了从上传的 Excel 文件中获取数据而不保存到文件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 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.

谢谢,
加布

推荐答案

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

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

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

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

这是链接:

http://www.codeplex.com/ExcelDataReader

这是一个代码示例:

<%--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();
}

这篇关于从上传的 Excel 文件中获取数据而不保存到文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆