2

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

找不到可安装的 ISAM

我不确定这意味着什么,经过数小时的搜索,我转向了这么多不同的主题。有很多关于 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");
                    }
                }

            }
        }
    }
}

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

4

3 回答 3

4

您需要将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""";
于 2013-03-10T22:26:21.120 回答
0

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

于 2013-03-10T21:27:23.197 回答
0

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

epplus

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

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

如何将流excel文件转换为数据表C#?

于 2013-03-10T22:11:16.230 回答