2

我正在尝试从电子表格中提取一堆数据,但是我无法在我的 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();
            }
4

2 回答 2

4

对于 *.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'";
于 2012-09-26T14:51:46.987 回答
0

很久以前写过这个。它在 WebForms 中,但 .cs 文件显示您想要的: 将 excel 电子表格转换为数据集、数据表和多维数组

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
}
于 2012-09-26T14:56:02.843 回答