How to upload a excel's data into a List or DataTable without saving and then deleting the excel file?

Tag: excel Author: all4yu Date: 2009-08-29

How to upload a excel's data into a List or DataTable without saving and then deleting the excel file?

I use asp.net C#.

For example, I have a excel file with some data, and I want to get the data from the excel file into a List or DataTable without saving the excel into a file system and then deleting the excel file. So I can avoid the IO operation.

I have found an answer:

Best Answer

Its doable, but it depends on the type of Excel file.

If its a CSV, you can use a StreamReader on the response stream to parse the CSV line by line. You can then use the TextFieldParser (don't be scared by VisualBasic in the namespace!) to parse the CSV from the stream. Here's a link with some info about the TFP.

If you're talking about a .xls or an .xlsx, it depends on the libraries you use. Different libraries will do things differently, but most likely they will take a stream that contains the file (whether that stream is a FileStream or a response stream--gotta love polymorphism!) and load it from there.

Other Answer1

SpreadsheetGear for .NET can read from / write to files, streams and memory (byte arrays) and has a method to get a DataTable from a range of cells (IRange.GetDataTable(...)).

You can download a free trial here if you want to try it out.

Disclaimer: I own SpreadsheetGear LLC

comments:

Is it open source and free?
@Mike108 - it is not open source and not free.