【问题标题】:Importing Excel sheet into SQL Server with C#使用 C# 将 Excel 工作表导入 SQL Server
【发布时间】:2013-03-11 06:26:21
【问题描述】:

我正在编写一个简单的程序来将 Excel 工作表导入我的数据库,但我遇到了以下错误:

找不到可安装的 ISAM

我不确定这意味着什么,经过数小时搜索这么多不同的主题后,我转向了 SO。有很多关于 Jet 和 ACE 的讨论,我不确定它们有什么区别,但这里是概要:我有一个名为 test 或 test1 的 excel 文件,我只想导入文件中的第一张工作表。到目前为止,这是我的源代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;

namespace WindowsFormsApplication2
{

    public partial class Form1 : Form
    {
        string filePath = null;
        public Form1()
        {
            InitializeComponent();
        }

        //Method to check database connection
        private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection cnn;
            connetionString = "Data Source=Zach-PC;Initial Catalog=master;Integrated Security=SSPI;";
            cnn = new SqlConnection(connetionString);
            try
            {
                cnn.Open();
                MessageBox.Show("Connection Open ! ");
                cnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }
        }

        //Method to select a file
        private void button2_Click(object sender, EventArgs e)
        {
            string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/Zach/Documents/test1.xls;Extended Properties=Excel 12.0,HDR=Yes;IMEX=1";


            // Create Connection to Excel Workbook
            using (OleDbConnection connection =
                         new OleDbConnection(excelConnectionString))
            {
                OleDbCommand command = new OleDbCommand
                        ("Select * FROM [Sheet1$]", connection);

                connection.Open(); //HERE IS WHERE THE ERROR IS

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

                    // Bulk Copy to SQL Server
                    using (SqlBulkCopy bulkCopy =
                               new SqlBulkCopy(sqlConnectionString))
                    {
                        bulkCopy.DestinationTableName = "Table";
                        bulkCopy.WriteToServer(dr);
                        MessageBox.Show("Data Exoprted To Sql Server Succefully");
                    }
                }

            }
        }
    }
}

我是不是在正确的庄园里接近这个?

【问题讨论】:

  • 在 Excel 12.0 之后尝试分号:Excel 12.0;HDR=Yes;IMEX=1
  • 这里也有同样的错误,我完全不知所措。
  • "......;Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=1\";" 呢?

标签: c# sql-server


【解决方案1】:

需要将Extended Properties部分连接字符串用引号括起来:

//                                                                                                                                 here                     and here
//  -->                                                                                                                              v                          v
string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/Zach/Documents/test1.xls;Extended Properties=""Excel 12.0,HDR=Yes;IMEX=1""";

【讨论】:

  • 这解决了这个问题,谢谢!现在看来我在访问数据库中的表时遇到了问题哈哈。
【解决方案2】:

您的计算机上可能未安装 Office oledb 驱动程序。您应该从microsoft website 下载它。安装后,您的代码应该运行。

【讨论】:

  • 我刚刚安装了它,但是当我运行代码时收到同样的错误。
【解决方案3】:

如果您正在阅读office 2007(或更新)的excel文件,那么我建议使用开源库Epplus来阅读excel文件。它是纯粹的.NET库,您不会依赖oledb驱动程序。

epplus

EPPlus 是一个 .net 库,它使用 Open Office Xml 格式 (xlsx) 读取和写入 Excel 2007/2010 文件。

您可以使用此库轻松将 excel 文件读入数据表。看看这个帖子

How convert stream excel file to datatable C#?

【讨论】:

  • 我将不得不在一段时间内看看这个,感到沮丧,不得不休息一下。如果这能解决我的问题,我会更新。
猜你喜欢
  • 2015-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-14
  • 2019-11-02
  • 2021-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多