Convert EXCEL to SQLite in C#
High-speed C# library for converting EXCEL to SQLite. This is a professional software solution to import and export EXCEL, SQLite, and many other formats on .NET Framework, .NET Core or Mono Platforms.
Convert EXCEL to SQLite Using C#
How do I convert EXCEL to SQLite? With Aspose.Cells for .NET library, you can easily convert EXCEL to SQLite programmatically with a few lines of code. Aspose.Cells for .NET is capable of building cross-platform applications with the ability to generate, modify, convert, render and print all Excel files. .NET Excel API not only convert between spreadsheet formats, it can also render Excel files as images, PDF, HTML, ODS, CSV, SVG, JSON, WORD, PPT and more, thus making it a perfect choice to exchange documents in industry-standard formats. Open NuGet package manager, search for Aspose.Cells and install. You may also use the following command from the Package Manager Console.
Package Manager Console Command
PM> Install-Package Aspose.Cells
How to Convert EXCEL to SQLite via C#
Need to convert EXCEL files to SQLite programmatically? .NET developers can easily load & convert EXCEL to SQLite in just a few lines of code.
- Install ‘Aspose.Cells for .NET’.
- Add a library reference (import the library) to your C# project.
- Load EXCEL file with an instance of Workbook.
- Build an Insert statement based on column names and values.
- Execute statements to insert data into the SQLite database.
Sample Code to Convert EXCEL to SQLite - C#
var connectionString = "Data Source = E:\\SQLiteTestData.sqlite;Cache=Shared;";
var tableName = "countrylanguage";
string excelFilePath = "SQLiteData.xlsx";
string autoIncrementColumnName = "id";
Workbook workbook = new Workbook(excelFilePath);
Worksheet worksheet = workbook.Worksheets[0];
DataTable dataTable = worksheet.Cells.ExportDataTableAsString(0, 0, worksheet.Cells.MaxDataRow + 1, worksheet.Cells.MaxDataColumn + 1, true);
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
connection.Open();
using (SQLiteTransaction transaction = connection.BeginTransaction())
{
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = connection;
cmd.Transaction = transaction;
foreach (DataRow dr in dataTable.Rows)
{
string columnNames = string.Join(", ", dataTable.Columns.Cast<DataColumn>()
.Select(c => $"`{c.ColumnName}`").Where(c => c != $"`{autoIncrementColumnName}`"));
string valuesPlaceholders = string.Join(", ", dataTable.Columns.Cast<DataColumn>()
.Select((c, index) => $"@value{index + 1}")
.Where((val, index) => dataTable.Columns[index].ColumnName != autoIncrementColumnName));
string insertCmd = $"INSERT INTO `{tableName}` ({columnNames}) VALUES ({valuesPlaceholders})";
cmd.CommandText = insertCmd;
cmd.Parameters.Clear();
for (int i = 0; i < dataTable.Columns.Count; i++)
{
if (dataTable.Columns[i].ColumnName != autoIncrementColumnName)
{
cmd.Parameters.AddWithValue($"@value{i + 1}", dr[i]);
}
}
cmd.ExecuteNonQuery();
}
}
transaction.Commit();
}
}
C# library to convert EXCEL to SQLite
There are two alternative options to install “Aspose.Cells for .NET” onto your system. Please choose one that resembles your needs and follow the step-by-step instructions:
- Install a NuGet Package . See Documentation
- Install the library using Package Manager Console within Visual Studio IDE
System Requirements
Before running the .NET conversion example code, make sure that you have the following prerequisites.
- Microsoft Windows or a compatible OS with .NET, .NET Core, Windows Azure or Mono Platforms..
- Development environment like Microsoft Visual Studio.
- Add reference to the Aspose.Cells for .NET DLL in your project.
Other Supported Conversions
You can also convert EXCEL to many other file formats including few listed below.