HTML JPG PDF XML EXCEL
  Product Family
MYSQL

Convert EXCEL to MYSQL in C#

High-speed C# library for converting EXCEL to MYSQL. This is a professional software solution to import and export EXCEL, MYSQL, and many other formats on .NET Framework, .NET Core or Mono Platforms.

Convert EXCEL to MYSQL Using C#

How do I convert EXCEL to MYSQL? With Aspose.Cells for .NET library, you can easily convert EXCEL to MYSQL 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 MYSQL via C#

Need to convert EXCEL files to MYSQL programmatically? .NET developers can easily load & convert EXCEL to MYSQL in just a few lines of code.

  1. Install ‘Aspose.Cells for .NET’.
  2. Add a library reference (import the library) to your C# project.
  3. Load EXCEL file with an instance of Workbook.
  4. Build an Insert statement based on column names and values.
  5. Execute statements to insert data into the MYSQL database.
 

Sample Code to Convert EXCEL to MYSQL - C#

var connectionString = "server=localhost;port=3306;user=root;password=root;database=testdb;charset=utf8mb4;";
var tableName = "areas";
string excelFilePath = "MySQLData.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 (MySqlConnection connection = new MySqlConnection(connectionString))
{
    connection.Open();
    using (MySqlTransaction transaction = connection.BeginTransaction())
    {
        using (MySqlCommand cmd = new MySqlCommand())
        {
            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 MYSQL

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:

  1. Install a NuGet Package . See Documentation
  2. 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.

EXCEL TO HTML (Hyper Text Markup Language)
EXCEL TO MD (Markdown Language)
EXCEL TO MHTML (Web Page Archive Format)
EXCEL TO ODS (OpenDocument Spreadsheet File)
EXCEL TO PDF (Portable Document Format)
EXCEL TO PNG (Portable Network Graphics)
EXCEL TO SVG (Scalable Vector Graphics)
EXCEL TO TIFF (Tagged Image Format)
EXCEL TO TSV (Tab-Separated Values)
EXCEL TO TXT (Text Document)
EXCEL TO XLS (Excel Binary Format)
EXCEL TO XLSB (Binary Excel Workbook File)
EXCEL TO XLSM (Spreadsheet File)
EXCEL TO XLSX (OOXML Excel File)
EXCEL TO XLT (Microsoft Excel Template)
EXCEL TO XLTM (Excel Macro-enabled Template)
EXCEL TO XLTX (Office OpenXML Excel Template)
EXCEL TO XML (Extensible Markup Language)
EXCEL TO XPS (XML Paper Specifications)
EXCEL TO JSON (JavaScript Object Notation)