导入Excel文件到Microsoft SQL Server的使用C#(Import Excel f

2019-06-24 19:12发布

我有我想要导入Excel文件导入Microsoft SQL Server 2008 R2的C#程序。

有人可以给我一个链接或教程,我可以完全对如何理解这样做呢?

我已经做了很长一段时间,我真的没有对如何实现这一点的想法。

请帮忙。 谢谢。

Answer 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

我们可以导入Excel到SQL Server这样的

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


Answer 2:

此代码还可以帮助你。

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


Answer 3:

看看这篇文章:

使用C#的DataTable和SQL服务器的OpenXML功能数据的批量插入

导入数据从Excel到SQL Server



Answer 4:

我认为,已经被赋予了最有用的答案。 我不打算来形容C#的方式,但rathher给你做,不C#一个简单的方法:

打开您的MS Access,通过ODBC连接你的MS SQL数据库,在Access中打开您的表并粘贴您的Excel记录有通过复制粘贴(预格式化,以匹配表模式。)

这种方式是非常快的和有用的,当你做一些一次性的数据导入。



文章来源: Import Excel file into Microsoft SQL Server using C#