Skip to content

adamfoneil/Excel2SqlServer.Library

Repository files navigation

Nuget

This is a library for importing Excel spreadsheets into SQL Server tables using Excel Data Reader.

Nuget package: Excel2SqlServer

In a nutshell, use the ExcelLoader class and call one of the SaveAsync overloads. 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:

using (var cn = GetConnection())
{
    var loader = new ExcelLoader();
    await loader.SaveAsync("MyFile.xlsx", cn, "dbo", "MyTable");
}

This 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.

If 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<string, ObjectName> where the key represents the sheet name, and the ObjectName is the schema + object of the resulting table.

By default, data is always appended to existing data. You can pass an optional Options object to customize the load behavior. For example:

using (var stream = await blob.OpenReadAsync())
{
    using (var cn = GetConnection())
    {
        var loader = new ExcelLoader();
        int rows = await loader.SaveAsync(stream, cn, "dbo", "MyTable", new Options() 
        {
            TruncateFirst = true,
            AutoTrimStrings = true,
            RemoveNonPrintingChars = true,
            CustomColumns = new string[]
            {
                "[IsProcessed] bit NOT NULL DEFAULT (0)",
                "[DateUploaded] datetime NOT NULL DEFAULT getdate()"
            }
        });
    }
}

This will append some extra columns to the table when it's created IsProcessed and DateUploaded.

An encoding error you might see

Note, if you see an error like this...

img

...try adding this line before you use ExcelLoader:

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

Inline Lookup Feature

Use the InlineLookup<T> 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.

Validation Features

Use the Validate static class to use these methods to help with data validation:

  • IEnumerable<ValidationInfo> ColumnTypes (SqlConnection connection, string query, string reportColumn, params TypeValidator[] columns)
  • IEnumerable<ValidationInfo> ColumnTypes (DataTable dataTable, string reportColumn, params TypeValidator[] columns)
  • Task<IEnumerable<ValidationInfo>> SqlServerTypeConversionAsync (SqlConnection connection, string schema, string table, string keyColumn, string convertColumn, string convertType, [ string criteria ])
  • Task<Dictionary<string, (string value, int data, int allowed)>> GetOversizedDataAsync (SqlConnection connection, ObjectName source, ObjectName destination, Dictionary<string, string> columnMappings)
  • Task<Dictionary<string, (string value, int length, int allowed)>> GetOversizedDataAsync (SqlConnection connection, Dictionary<string, (string value, int length)> maxDataLengths, ObjectName destination, Dictionary<string, string> columnMappings)
  • Task EnsureNoOversizedDataAsync (SqlConnection connection, ObjectName source, ObjectName destination, Dictionary<string, string> columnMappings)
  • Task EnsureNoOversizedDataAsync (SqlConnection connection, Dictionary<string, (string value, int length)> maxDataLengths, ObjectName destination, Dictionary<string, string> columnMappings)
  • Task<Dictionary<string, (string value, int length)>> GetMaxDataLengthsAsync (SqlConnection connection, ObjectName table)

Reference

Excel2SqlServer.Library.ExcelLoader ExcelLoader.cs

  • Task CreateTableAsync (string fileName, SqlConnection connection, string schemaName, string tableName, [ IEnumerable customColumns ])
  • Task CreateTableAsync (Stream stream, SqlConnection connection, string schemaName, string tableName, [ IEnumerable customColumns ])
  • Task<int> SaveAsync (string fileName, SqlConnection connection, [ Dictionary<string, ObjectName> tableNames ], [ Options options ])
  • Task<int> SaveAsync (string fileName, SqlConnection connection, string schemaName, string tableName, [ Options options ])
  • Task<int> SaveAsync (Stream stream, SqlConnection connection, [ Dictionary<string, ObjectName> tableNames ], [ Options options ])
  • Task<int> SaveAsync (Stream stream, SqlConnection connection, string schemaName, string tableName, [ Options options ])
  • Task<DataSet> ReadAsync (string fileName)
  • Task<DataSet> ReadAsync (Stream stream)

Excel2SqlServer.Library.InlineLookup InlineLookup.cs

Properties

Methods

Excel2SqlServer.Library.Extensions.SqlConnectionExtensions SqlConnectionExtensions.cs

Methods

  • bool TableExists (this SqlConnection connection, string schemaName, string tableName)
  • bool SchemaExists (this SqlConnection connection, string schemaName)
  • Task<IEnumerable<string>> GetColumnNamesAsync (this SqlConnection connection, string schemaName, string tableName)
  • Task<IEnumerable<string>> GetColumnNamesAsync (this SqlConnection connection, ObjectName table)

About

Library for importing Excel spreadsheets into SQL Server tables

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages