Popularity
0.8
Growing
Activity
3.4
-
3
1
1

Description

This is a library for importing Excel spreadsheets into SQL Server tables. In a nutshell, use the `ExcelLoader` class and call one of the `Save` overloads accepting a filename or `Stream`. Customize with ability to add custom columns, removing whitespace and non-printing characters.

Programming language: C#
License: MIT License
Tags: ORM     Excel     SqlServer     Import    

Excel2SqlServer alternatives and similar packages

Based on the "ORM" category.
Alternatively, view Excel2SqlServer alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of Excel2SqlServer or a related project?

Add another 'ORM' Package

README

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);

Reference

  • 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 tableNames ], [ Options options ])
  • Task<int> SaveAsync (string fileName, SqlConnection connection, string schemaName, string tableName, [ Options options ])
  • Task<int> SaveAsync (Stream stream, SqlConnection connection, [ Dictionary 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)