Import Excel file into Microsoft SQL Server using

2019-01-27 11:27发布

问题:

I have a C# program that I want to import an Excel file into Microsoft SQL Server 2008 R2.

Can someone give me a link or tutorial where I can fully understand on how to this?

I've been doing this for a long time and I really don't have an idea on how to implement this.

Please help. Thanks.

回答1:

string excelConnectionString = @"Provider=Microsoft 
.Jet.OLEDB.4.0;Data Source=Book1.xls;Extended 
Properties=""Excel 8.0;HDR=YES;"""; 

// Create Connection to Excel Workbook

We can Import excel to sql server like this

using (OleDbConnection connection = new OleDbConnection(excelConnectionString)){

OleDbCommand command = new OleDbCommand ("Select ID,Data FROM [Data$]", connection);

connection.Open(); 


`// Create DbDataReader to Data Worksheet `
using (DbDataReader dr = command.ExecuteReader()) 
{ 
    // SQL Server Connection String 
    string sqlConnectionString = "Data Source=.; 
       Initial Catalog=Test;Integrated Security=True"; 


    // Bulk Copy to SQL Server 
    using (SqlBulkCopy bulkCopy = 
               new SqlBulkCopy(sqlConnectionString)) 
    { 
        bulkCopy.DestinationTableName = "ExcelData"; 
        bulkCopy.WriteToServer(dr); 
    } 


回答2:

This code may also help you.

private void processExcel(string filename)
{
    filename = Server.MapPath("~/Files/WM-0b23-productsBook.xlsx");
    Microsoft.Office.Interop.Excel.Application xlApp;
    Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

    var missing = System.Reflection.Missing.Value;

    xlApp = new Microsoft.Office.Interop.Excel.Application();
    xlWorkBook = xlApp.Workbooks.Open(filename, false, true, missing, missing, missing, true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, '\t', false, false, 0, false, true, 0);
    xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

    Microsoft.Office.Interop.Excel.Range xlRange = xlWorkSheet.UsedRange;
    Array myValues = (Array)xlRange.Cells.Value2;

    int vertical = myValues.GetLength(0);
    int horizontal = myValues.GetLength(1);

    System.Data.DataTable dt = new System.Data.DataTable();

    // must start with index = 1
    // get header information
    for (int i = 1; i <= horizontal; i++)
    {
        dt.Columns.Add(new DataColumn(myValues.GetValue(1, i).ToString()));
    }

    // Get the row information
    for (int a = 2; a <= vertical; a++)
    {
        object[] poop = new object[horizontal];
        for (int b = 1; b <= horizontal; b++)
        {
            poop[b - 1] = myValues.GetValue(a, b);
        }
        DataRow row = dt.NewRow();
        row.ItemArray = poop;
        dt.Rows.Add(row);
    }

    // assign table to default data grid view
    GridView1.DataSource = dt;
    GridView1.DataBind();

    xlWorkBook.Close(true, missing, missing);
    xlApp.Quit();

    releaseObject(xlWorkSheet);
    releaseObject(xlWorkBook);
    releaseObject(xlApp);
}

private void releaseObject(object obj)
{
    try
    {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        obj = null;
    }
    catch (Exception ex)
    {
        obj = null;
        MessageBox.Show("Unable to release the Object " + ex.ToString());
    }
    finally
    {
        GC.Collect();
    }
}


回答3:

Check out this post :

Bulk Insertion of Data Using C# DataTable and SQL server OpenXML function

Import Data from Excel to SQL Server



回答4:

I think that most useful answers already have been given. I am not going to describe the C# way, but rathher give you an easy way of doing it without C#:

Open your MS Access, link your MS SQL database via ODBC, open your table in Access and paste your Excel records there via copy-paste (preformatted to match the table schema.)

This way is very fast and useful when you do some one-time data import.