gabe
gabe

Reputation:

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

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

Upvotes: 10

Views: 43302

Answers (6)

gabe
gabe

Reputation: 1950

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

Upvotes: 8

Christoph
Christoph

Reputation: 915

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

http://www.csvreader.com

Upvotes: 1

Ricardo Villamil
Ricardo Villamil

Reputation: 5107

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.

Good luck.

Upvotes: -1

hearn
hearn

Reputation: 1017

This is something I've been playing with recently.

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

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

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

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

Upvotes: 0

TcKs
TcKs

Reputation: 26632

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.

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

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

Upvotes: 1

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827366

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

Upvotes: 4

Related Questions