Reputation: 8188
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?
Upvotes: 0
Views: 2973
Reputation:
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.
Upvotes: 0
Reputation: 4127
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
Upvotes: 0
Reputation: 5600
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:
Upvotes: 0
Reputation: 128417
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.
Upvotes: 5