Mapping and Importing Excel Files to a Database using SqlBulkCopy
Problem:
I was working on a project this week that involved importing data from excel spreadsheets into a SQL database. At first glance I thought no problem, just load the excel document into a DataTable and use SqlBulkCopy to import. Easy enough I thought, but then I was informed that each of these excel files will have different header name and in some cases they will include extra columns. Damn! It’s never that easy. I started looking around for the best way to accomplish this and this is what I pieced together.
Solution:
- Get the table scheme for the Excel and DB table
- Map the columns
- Load Excel data into DataTable
- Use SQLBulkCopy to import in to the DB
- Boom Done!