从上传的Excel文件获取数据而不保存到文件系统 [英] Get Data From An Uploaded Excel File Without Saving to File System
问题描述
<% - ASP.NET声明式 - %>
/ pre>
< 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;
// ...在内存中读取文件并放入发送到存储过程的格式...
}
有人可以帮我吗?我感谢任何人的考虑。
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:
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屋!