![]() ![]() You will notice that at no stage is the Excel file saved to the server, which removes any potential problems arising from permissions, file locking or file system checking. Even if you are using an ORM in your project like the Entity Framework, you are still advised to revert to ADO.NET and SqlBulkCopy for this type of operation purely for performance reasons. This is among the quickest ways to get data into a SQL Server database, and the code can be adapted easily to MVC, although the bulk of the code in the Page_Load event handler should go into a separate method that's called from the controller rather than in the controller itself. Then the data is inserted into the destination table. Each of the columns in the Excel worksheet is compared to the columns in the destination table, and if a match is found, an entry is added to the ColumnMappings collection, ensuring that any unmatched columns are ignored. Then the data is extracted into a DataTable using the ToDataTable extension method.The destination table is specified and then the schema of that table is obtained from the database. This code checks to see if a file has been uploaded, and if one has, its extension is checked to ensure that it is an Excel file. OrdinalIgnoreCase))ī(sourceColumn.ColumnName, ( string )row) Equals(sourceColumn.ColumnName, ( string )row, StringComparison. Var schema = conn.GetSchema( "Columns", new ) įoreach ( DataColumn sourceColumn in dt.Columns) Database=Test Integrated Security=SSPI" )) Using ( var conn = new SqlConnection ( "Server=. ![]() Var excel = new ExcelPackage (Upload.FileContent) GetExtension(Upload.FileName).Equals( ".xlsx" )) Protected void Page_Load( object sender, EventArgs e) Public partial class SqlBulkInsertWithEPPlus :. The code behind that processes the file and performs the bulk insert looks like this: using OfficeOpenXml If you want to manage more than one worksheet in a workbook or your sheet doesn't contain a header row, you will need to modify the method accordingly. It uses those header values for ColumnName values in the DataTable. The code assumes two things: that only the first worksheet is of any interest and that the first row of the worksheet contains headers. This method is a simple utility method that takes the content of an Excel file and puts it into a DataTable. Public static DataTable ToDataTable( this ExcelPackage package)ĮxcelWorksheet workSheet = () įoreach ( var firstRowCell in workSheet.Cells)įor ( var rowNumber = 2 rowNumber <= rowNumber++) Public static class ExcelPackageExtensions The code for processing the uploaded Excel file makes use of an extension method called ToDataTable, which I have defined in a class called ExcelPackageExtenstions: using OfficeOpenXml The following snippet shows a Web Form featuring just a FileUpload control and a Button: It is available by searching for EPPlus in the Nuget Package Manager (VS 2015 onwards) or by typing install-package epplus The package name is EPPlus, and I have written about it before.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |