【问题标题】:Attempting to connect to Excel spreadsheet in C#尝试在 C# 中连接到 Excel 电子表格
【发布时间】:2012-09-26 14:49:35
【问题描述】:

我正在尝试从电子表格中提取一堆数据,但是我无法在我的 C# 代码中建立成功的连接。乙

下面是连接字符串和我用来建立连接的代码。该程序的目标是从电子表格中提取数据并将其存入 SQL 数据库。我无法通过 connection.open() 命令,但是没有收到此错误消息:

“外部表的格式不符合预期”

        string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\nearod\Desktop\TestLoad.xlsx;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
        string queryString = "SELECT * FROM [SQL AgentUnique ID Test Load$]";
        try
        {
            OleDbDataReader reader;
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {

                OleDbCommand command = new OleDbCommand(queryString, connection);
                connection.Open();
                reader = command.ExecuteReader();


                while (reader.Read())
                {
                    counter++;

                    //Access db values
                    string compCode = "";
                    string agId = "";
                    string fName = "";
                    string lName = "";
                    string nameSuffix = "";


                    compCode = reader.GetValue(0).ToString();
                    agId = reader.GetString(1);
                    fName = reader.GetString(2);
                    lName = reader.GetString(3);
                    nameSuffix = reader.GetString(4);

                    sqlComm.Parameters.Add(companyCode);
                    sqlComm.Parameters.Add(agentID);
                    sqlComm.Parameters.Add(firstName);
                    sqlComm.Parameters.Add(lastName);
                    sqlComm.Parameters.Add(suffix);

                    //Initialize connection objects
                    cm = Dts.Connections["QUAHILSQ03"];
                    sqlConn = (SqlConnection)cm.AcquireConnection(Dts.Transaction);
                    sqlComm = new SqlCommand("AgentResourcesU01.dbo.sp_AgentIdAprCheck", sqlConn);
                    sqlComm.CommandType = CommandType.StoredProcedure;

                    //Execute stored procedure
                    sqlComm.ExecuteNonQuery();

                }
                reader.Close();
                connection.Close();
                OleDbConnection.ReleaseObjectPool();
            }

【问题讨论】:

  • 顺便说一句,您应该能够通过适当结构化的查询将一组数据从 MS Access 直接加载到 SQL 服务器中。
  • 我需要调用一个存储过程来正确排序数据。您可以在 Access 中执行此操作吗?
  • 您当然可以在 MS Access 中对数据进行排序。您还可以使用 ACE 驱动程序对 Excel 记录集进行排序。在 Access 中,与存储过程最接近的等价物是查询。

标签: c# excel ms-access connection-string


【解决方案1】:

对于 *.xlsx,您需要 Ace 驱动程序:Microsoft.ACE.OLEDB.12.0

string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;
 Data Source=C:\Users\nearod\Desktop\TestLoad.xlsx;
 Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";

【讨论】:

    【解决方案2】:

    很久以前写的。它在 WebForms 中,但 .cs 文件显示您需要: converting an excel spreadsheet to a dataset, datatable and multi-dimensional array

    string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+FileToConvert+";Extended Properties=Excel 8.0;";
    try
    {
        OleDbConnection connection = new OleDbConnection(connectionString);
        connection.Open();
        //this next line assumes that the file is in default Excel format with Sheet1 as the first sheet name, adjust accordingly
        OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connection);
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        adapter.Fill(ds);//now you have your dataset ds now filled with the data and ready for manipulation
        // do stuff
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 2010-11-08
      • 1970-01-01
      • 2012-11-15
      • 2014-01-18
      相关资源
      最近更新 更多