简体   繁体   中英

Importing an Excel WorkSheet into a Datatable

I have been asked to create import functionality in my application. I am getting an excel worksheet as input. The worksheet has column headers followed by data. The users want to simply select an xls file from their system, click upload and the tool deletes the table in the database and adds this new data.

I thought the best way would be too bring the data into a datatable object and do a foeach for every row in the datatable insert row by row into the db.

My question is what can anyone give me code to open an excel file, know what line the data starts on in the file, and import the data into a datable object?

Take a look at Koogra .

You instantiate a WorkBook object from a path to an XLS file.

You access a WorkSheet object from the workbook's Sheets property.

You can enumerate over the rows in the worksheet by accessing the sheet's Rows property from index MinRow to MaxRow .

You can enumerate over the cells in a given row by accessing the row's Cells property from index MinColumn to MaxColumn .

Each cell has a Value property ( object ) as well as a FormattedValue method ( string ).

Give it a try -- I've found it to be extremely intuitive and easy to use.

You can make use of an OleDbConnection to connect to excel file and the query it using SQL queries.

If it is an Asp.Net application, then you make use of the FileUpload control and get the bytes from the file. Then you will have to manually convert it to a datatable.

Try out these links:

OleDbConnection to excel file

Byte array to datatable

What your looking for is the concept described Here

Providing you dont want to use a third party library anyway, else Dans solution will suit you

First you have to download the dll file namely NExcel.dll
By using this dll you can make various object which are very useful for import excel data in .net using both vb as well as c#.

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM