{"id":13706476,"url":"https://github.com/adamfoneil/Excel2SqlServer.Library","last_synced_at":"2025-05-05T20:31:35.191Z","repository":{"id":115833320,"uuid":"174638096","full_name":"adamfoneil/Excel2SqlServer.Library","owner":"adamfoneil","description":"Library for importing Excel spreadsheets into SQL Server tables","archived":false,"fork":false,"pushed_at":"2024-01-24T00:54:55.000Z","size":5522,"stargazers_count":6,"open_issues_count":3,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-03T10:53:51.006Z","etag":null,"topics":["adodotnet","excel-import","netstandard20","sql-server"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adamfoneil.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-03-09T02:20:24.000Z","updated_at":"2024-10-31T07:45:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"f12cdaf6-1bee-4f1c-9caf-4f6015677a85","html_url":"https://github.com/adamfoneil/Excel2SqlServer.Library","commit_stats":null,"previous_names":["adamosoftware/excel2sqlserver.library"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamfoneil%2FExcel2SqlServer.Library","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamfoneil%2FExcel2SqlServer.Library/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamfoneil%2FExcel2SqlServer.Library/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamfoneil%2FExcel2SqlServer.Library/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adamfoneil","download_url":"https://codeload.github.com/adamfoneil/Excel2SqlServer.Library/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252571086,"owners_count":21769772,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["adodotnet","excel-import","netstandard20","sql-server"],"created_at":"2024-08-02T22:00:57.476Z","updated_at":"2025-05-05T20:31:32.283Z","avatar_url":"https://github.com/adamfoneil.png","language":"C#","readme":"[![Nuget](https://img.shields.io/nuget/v/Excel2SqlServer)](https://www.nuget.org/packages/Excel2SqlServer/)\n\nThis is a library for importing Excel spreadsheets into SQL Server tables using [Excel Data Reader](https://github.com/ExcelDataReader/ExcelDataReader).\n\nNuget package: **Excel2SqlServer**\n\nIn a nutshell, use the [ExcelLoader](https://github.com/adamosoftware/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/ExcelLoader.cs) class and call one of the `SaveAsync` [overloads](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/ExcelLoader.cs#L108-L130). You can use a local filename or a stream as input. Here's a simple example that loads a single table from a local file:\n\n```csharp\nusing (var cn = GetConnection())\n{\n    var loader = new ExcelLoader();\n    await loader.SaveAsync(\"MyFile.xlsx\", cn, \"dbo\", \"MyTable\");\n}\n```\nThis will save an Excel file called `MyFile.xlsx` to a database table `dbo.MyTable`. The table is created if it doesn't exist. Note also there is an `int identity(1,1)` column created called `Id` if it doesn't already exist in the spreadsheet.\n\nIf a spreadsheet has multiple sheets and you want to import all the sheets into multiple tables, omit the schema and table name from the `SaveAsync` call. `ExcelLoader` will use the sheet names in the spreadsheet to build the table names. If you need to customize the table names, you can pass a `Dictionary\u003cstring, ObjectName\u003e` where the key represents the sheet name, and the `ObjectName` is the schema + object of the resulting table.\n\nBy default, data is always appended to existing data. You can pass an optional [Options](https://github.com/adamosoftware/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Options.cs) object to customize the load behavior. For example:\n```csharp\nusing (var stream = await blob.OpenReadAsync())\n{\n    using (var cn = GetConnection())\n    {\n        var loader = new ExcelLoader();\n        int rows = await loader.SaveAsync(stream, cn, \"dbo\", \"MyTable\", new Options() \n        {\n            TruncateFirst = true,\n            AutoTrimStrings = true,\n            RemoveNonPrintingChars = true,\n            CustomColumns = new string[]\n            {\n                \"[IsProcessed] bit NOT NULL DEFAULT (0)\",\n                \"[DateUploaded] datetime NOT NULL DEFAULT getdate()\"\n            }\n        });\n    }\n}\n```\nThis will append some extra columns to the table when it's created `IsProcessed` and `DateUploaded`.\n\n## An encoding error you might see\n\nNote, if you see an error like this...\n\n![img](https://adamosoftware.blob.core.windows.net:443/images/encoding-error.png)\n\n...try adding this line before you use `ExcelLoader`:\n\n\n```csharp\nEncoding.RegisterProvider(CodePagesEncodingProvider.Instance);\n```\n\n## Inline Lookup Feature\nUse the [InlineLookup\\\u003cT\\\u003e](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/InlineLookup.cs) class to convert string values to corresponding key values. The `T` generic argument indicates the type of keys being used. Currently `int` is the only type supported. For users who need to upload spreadsheets with key values, allowing them to use text values instead of numeric keys can make an upload process easier.\n\n- See the integration [test](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Testing/LoadTests.cs#L105) showing this in use along with the sample [Excel file](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Testing/Resources/inline-lookup.xlsx) it uses.\n    \n- Use the [InlineLookup.ExecuteAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/InlineLookup.cs#L31) method to generate a table of mapped key values from a user's upload.\n    \n- Use the [InlineLookup.GetErrorsAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/InlineLookup.cs#L98) method to find text values that don't have a mapping.\n    \n### Validation Features\nUse the [Validate](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Validate.cs) static class to use these methods to help with data validation:\n- IEnumerable\\\u003cValidationInfo\\\u003e [ColumnTypes](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Validate.cs#L19)\n (SqlConnection connection, string query, string reportColumn, params TypeValidator[] columns)\n- IEnumerable\\\u003cValidationInfo\\\u003e [ColumnTypes](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Validate.cs#L33)\n (DataTable dataTable, string reportColumn, params TypeValidator[] columns)\n- Task\\\u003cIEnumerable\\\u003cValidationInfo\\\u003e\\\u003e [SqlServerTypeConversionAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Validate.cs#L53)\n (SqlConnection connection, string schema, string table, string keyColumn, string convertColumn, string convertType, [ string criteria ])\n- Task\\\u003cDictionary\\\u003cstring, (string value, int data, int allowed)\\\u003e\\\u003e [GetOversizedDataAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Validate.cs#L101)\n (SqlConnection connection, ObjectName source, ObjectName destination, Dictionary\u003cstring, string\u003e columnMappings)\n- Task\\\u003cDictionary\\\u003cstring, (string value, int length, int allowed)\\\u003e\\\u003e [GetOversizedDataAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Validate.cs#L109)\n (SqlConnection connection, Dictionary\u003cstring, (string value, int length)\u003e maxDataLengths, ObjectName destination, Dictionary\u003cstring, string\u003e columnMappings)\n- Task [EnsureNoOversizedDataAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Validate.cs#L125)\n (SqlConnection connection, ObjectName source, ObjectName destination, Dictionary\u003cstring, string\u003e columnMappings)\n- Task [EnsureNoOversizedDataAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Validate.cs#L133)\n (SqlConnection connection, Dictionary\u003cstring, (string value, int length)\u003e maxDataLengths, ObjectName destination, Dictionary\u003cstring, string\u003e columnMappings)\n- Task\\\u003cDictionary\\\u003cstring, (string value, int length)\\\u003e\\\u003e [GetMaxDataLengthsAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Validate.cs#L176)\n (SqlConnection connection, ObjectName table)\n\n# Reference\n## Excel2SqlServer.Library.ExcelLoader [ExcelLoader.cs](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/ExcelLoader.cs)\n- Task [CreateTableAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/ExcelLoader.cs#L18)\n (string fileName, SqlConnection connection, string schemaName, string tableName, [ IEnumerable\u003cstring\u003e customColumns ])\n- Task [CreateTableAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/ExcelLoader.cs#L24)\n (Stream stream, SqlConnection connection, string schemaName, string tableName, [ IEnumerable\u003cstring\u003e customColumns ])\n- Task\\\u003cint\\\u003e [SaveAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/ExcelLoader.cs#L108)\n (string fileName, SqlConnection connection, [ Dictionary\u003cstring, ObjectName\u003e tableNames ], [ [Options](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Options.cs#L5) options ])\n- Task\\\u003cint\\\u003e [SaveAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/ExcelLoader.cs#L114)\n (string fileName, SqlConnection connection, string schemaName, string tableName, [ [Options](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Options.cs#L5) options ])\n- Task\\\u003cint\\\u003e [SaveAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/ExcelLoader.cs#L120)\n (Stream stream, SqlConnection connection, [ Dictionary\u003cstring, ObjectName\u003e tableNames ], [ [Options](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Options.cs#L5) options ])\n- Task\\\u003cint\\\u003e [SaveAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/ExcelLoader.cs#L126)\n (Stream stream, SqlConnection connection, string schemaName, string tableName, [ [Options](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Options.cs#L5) options ])\n- Task\\\u003cDataSet\\\u003e [ReadAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/ExcelLoader.cs#L253)\n (string fileName)\n- Task\\\u003cDataSet\\\u003e [ReadAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/ExcelLoader.cs#L261)\n (Stream stream)\n\n## Excel2SqlServer.Library.InlineLookup [InlineLookup.cs](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/InlineLookup.cs#L13)\n### Properties\n- string [SourceTable](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/InlineLookup.cs#L23)\n- string [IdentityColumn](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/InlineLookup.cs#L24)\n- string [ResultTable](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/InlineLookup.cs#L25)\n- Dictionary\\\u003cstring, Lookup\\\u003e [Lookups](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/InlineLookup.cs#L26)\n### Methods\n- Task [ExecuteAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/InlineLookup.cs#L31)\n (SqlConnection connection)\n- Task\\\u003cILookup\\\u003cstring, string\\\u003e\\\u003e [GetErrorsAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/InlineLookup.cs#L98)\n (SqlConnection cn)\n\n## Excel2SqlServer.Library.Extensions.SqlConnectionExtensions [SqlConnectionExtensions.cs](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Extensions/SqlConnectionExtensions.cs#L9)\n### Methods\n- bool [TableExists](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Extensions/SqlConnectionExtensions.cs#L11)\n (this SqlConnection connection, string schemaName, string tableName)\n- bool [SchemaExists](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Extensions/SqlConnectionExtensions.cs#L18)\n (this SqlConnection connection, string schemaName)\n- Task\\\u003cIEnumerable\\\u003cstring\\\u003e\\\u003e [GetColumnNamesAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Extensions/SqlConnectionExtensions.cs#L24)\n (this SqlConnection connection, string schemaName, string tableName)\n- Task\\\u003cIEnumerable\\\u003cstring\\\u003e\\\u003e [GetColumnNamesAsync](https://github.com/adamfoneil/Excel2SqlServer.Library/blob/master/Excel2SqlServer.Library/Extensions/SqlConnectionExtensions.cs#L27)\n (this SqlConnection connection, ObjectName table)\n","funding_links":[],"categories":["C# #"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadamfoneil%2FExcel2SqlServer.Library","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadamfoneil%2FExcel2SqlServer.Library","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadamfoneil%2FExcel2SqlServer.Library/lists"}