上传 excel 文件时,获取 Oledb 异常 - 定义的字段过多



我正在上传一个有 5 张纸的 Excel。上传时,我收到错误" 定义的字段太多"

public DataSet ExportQCCheckData(string filepath)
{
    string path = filepath;
    OleDbConnection con;
    System.Data.DataTable dt = null;
    //Connection string for oledb
    string conn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + path + "; Extended Properties='Excel 8.0;IMEX=1;'";
    con = new OleDbConnection(conn);
    try
    {
        con.Open();
        //get the sheet name in to a table
        dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        String[] excelsheets = new String[dt.Rows.Count];
        int i = 0;
        //using foreach get the sheet name in a string array called excelsheets[]
        foreach (DataRow dr in dt.Rows)
        {
            excelsheets[i] = dr["TABLE_NAME"].ToString();
            i++;
        }
        DataSet ds = new DataSet();
        int cnt = 0;
        foreach (string temp in excelsheets)
        {
            if (temp == "BASIC$" || temp == "SHORT$" || temp == "CLASS$" || temp == "MEMO$" || temp == "SOURCE$")
            {
                string query = "select * from [" + temp + "]";
                OleDbDataAdapter adp = new OleDbDataAdapter(query, con);
                adp.Fill(ds, temp);
                cnt++;
            }
        }
        if (cnt != 5)
        {
            ds = null;
        }
        return ds;
    }
    catch (Exception ex)
    {
        Console.Write(ex.Message);
        DataSet ds = null;
        return ds;
    }
    finally
    {
        con.Close();
    }
}

该错误属于Excel工作表上的字段数 请添加Excel工作表。 您需要验证 Excel 工作表不包含很多列

这个问题是由于excel包含许多列。 所以我的意思是,只需选择所有空单元格

控制-

然后我上传了 excel 表,它上传了。

最新更新