【问题标题】:Import Excel file into Microsoft SQL Server using C#使用 C# 将 Excel 文件导入 Microsoft SQL Server
【发布时间】:2012-05-25 08:09:30
【问题描述】:

我有一个 C# 程序,我想将 Excel 文件导入 Microsoft SQL Server 2008 R2。

谁能给我一个链接或教程,我可以完全理解如何做到这一点?

我已经这样做了很长时间,但我真的不知道如何实现它。

请帮忙。谢谢。

【问题讨论】:

标签: c# excel sql-server-2008-r2 import-from-excel


【解决方案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); 
    } 

【讨论】:

    【解决方案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();
        }
    }
    

    【讨论】:

    • 嗨,这很有帮助,但是我导入时出错,有时数据类型不匹配,即使它们相同/相似。
    【解决方案3】:
    【解决方案4】:

    我认为已经给出了最有用的答案。 我不会描述 C# 的方式,而是给你一个不用 C# 的简单方法:

    打开您的 MS Access,通过 ODBC 链接您的 MS SQL 数据库,在 Access 中打开您的表格并通过复制粘贴将您的 Excel 记录粘贴到那里(预先格式化以匹配表格架构。)

    当您进行一些一次性数据导入时,这种方式非常快速且有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 2015-03-08
      相关资源
      最近更新 更多