0

我在我的 .net 应用程序中使用 excel 数据导入到 sql 表,但是在保存数据时出现以下错误:

我的代码:

protected void btnSave_Click(object sender, EventArgs e)
{
    string FileName = lblFileName.Text;
    string Extension = Path.GetExtension(FileName);
    string FolderPath = Server.MapPath (ConfigurationManager.AppSettings["FolderPath"]);
    string CommandText = "";
    switch (Extension)
    {
        case ".xls": //Excel 97-03
            CommandText = "spx_ImportFromExcel03";
            break;
        case ".xlsx": //Excel 07
            CommandText = "spx_ImportFromExcel07";
            break;
    }
    //Read Excel Sheet using Stored Procedure
    //And import the data into Database Table
    String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = CommandText;
    cmd.Parameters.Add("@SheetName", SqlDbType.VarChar).Value = ddlSheets.SelectedItem.Text;
    cmd.Parameters.Add("@FilePath", SqlDbType.VarChar).Value = FolderPath + FileName;
    cmd.Parameters.Add("@HDR", SqlDbType.VarChar).Value = rbHDR.SelectedItem.Text;
    cmd.Parameters.Add("@TableName", SqlDbType.VarChar).Value = txtTable.Text;   
    cmd.Connection = con;
    try
    {
        con.Open();
        object count = cmd.ExecuteNonQuery();
        lblMessage.ForeColor = System.Drawing.Color.Green;
        lblMessage.Text = count.ToString() + " records inserted.";  
    }
    catch (Exception ex)
    {
        lblMessage.ForeColor = System.Drawing.Color.Red;     
        lblMessage.Text = ex.Message;  
    }
    finally
    {
        con.Close();
        con.Dispose();
        Panel1.Visible = true;
        Panel2.Visible = false; 
    }
}  

我在 web.config 中使用 excel 格式的连接字符串:

<connectionStrings>
<add name="Excel03ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'"/>
    <add name="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR={1}'"/>

我无法弄清楚这个错误。任何人都可以帮忙吗?

4

2 回答 2

1

要在 c# 代码中读取 excel 文件,您应该有 microsoft oledb 提供程序。Microsoft.ACE.OLEDB.12.0 提供程序用于读取具有 xlsx 格式的文件,Microsoft.Jet.OLEDB.4.0 用于 xls 格式文件。用于读取 excel 的 C# 代码文件如下所示。

using System.Data.OleDb;

   OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Filename.xls") + ";Extended Properties=Excel 8.0"); 
            OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", con);
            con.Open(); 
            OleDbDataReader dr = cmd.ExecuteReader(); 
            string col0 = ""; 
            string col1 = ""; 
            string col2 = ""; 
            while (dr.Read()) 
            { 
                col0 =Convert.ToString(dr[ 0]); 
                col1 =Convert.ToString(dr[ 0]); 
                col2 =Convert.ToString(dr[ 0]); 
                /*******************************
               Write Statements to Insert values of each rows into table 
                *********************************/
            } 
                con.Close(); 
            }
于 2012-12-01T11:15:54.210 回答
0

这是将数据从excel导入数据库的好文章。将 Excel 导入 Sql 按照那里的所有说明进行操作,您将成功。

于 2012-12-01T10:19:13.470 回答