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

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

问题描述

我有一个要求,允许这个ASP.NET Web应用程序的用户上传一个专门格式化的Excel电子表格,使用电子表格中的数据填充数组,并将数组绑定到一个Oracle存储过程,以验证和插入数据库。我无法将其保存到Web服务器的硬盘上,因此我可以从Excel电子表格中读取数据。这是我不能弄清楚如何做的部分。这是一个简单的代码示例。

 <% -  ASP.NET声明式 - %> 
< asp:FileUpload ID =FileUpload1runat =server/>
< asp:Button ID =Button1runat =serverText =发送文件OnClick =Button1_Click/>

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

// ...在内存中读取文件并放入发送到存储过程的格式...

}
/ pre>

有人可以帮我吗?我感谢任何人的考虑。



thx,

gabe

解决方案

我在Codeplex上找到了一个很好的轻量级开源API,用于做这个称为ExcelDataReader。



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



这里是链接:


http://www.codeplex.com/ExcelDataReader


这是一个代码示例:

 <% -  ASP.NET声明式 - %> 
< asp:FileUpload ID =FileUpload1runat =server/>
< asp:Button ID =Button1runat =serverText =发送文件OnClick =Button1_Click/>
< asp:GridView ID =GridView1runat =server/>

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

DataSet wb = excelReader.WorkbookData;
//获取工作簿的第一个工作表
DataTable dt = excelReader.WorkbookData.Tables [0];

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


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.

thx,
gabe

解决方案

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

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

Here's the link:

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

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

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