无法使用C#将数据从excel文件上传到我的数据库(mysql)

时间:2018-10-17 11:04:34

标签: c# excel

我无法将数据从excel上传到我的数据库中,但这种方法存在异常:

private void ReadFileExcel(string filename)
    {
        Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@filename);
        Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
        Excel.Range xlRange = xlWorksheet.UsedRange;
        ConnectMysqlBase.GetDBConnection();
        try
        {
            ConnectMysqlBase.connection.Open();
            MySqlCommand command = new MySqlCommand("INSERT INTO  userinformation VALUES (" + xlRange.Cells[2][1] + "," + xlRange.Cells[2][2] + "," + xlRange.Cells[2][3] + ","
                + xlRange.Cells[2][4] + "," + xlRange.Cells[2][5] + "," + xlRange.Cells[2][6] + "," + xlRange.Cells[2][7] + "," + xlRange.Cells[2][8] + "," + xlRange.Cells[2][9] + "," + xlRange.Cells[2][10] + "," + xlRange.Cells[2][11] + ") ;"
                , ConnectMysqlBase.connection);
            MySqlDataAdapter dataAdapter = new MySqlDataAdapter(command);
            DataTable dt = new DataTable();
            dataAdapter.Fill(dt);
            TableUser = dt;

        }
        catch (Exception e)
        {
        }
        finally
        {
            ConnectMysqlBase.connection.Close();
        }
    }

女巫像: “字段列表”中的未知列“ System .__ ComObject” !!!

1 个答案:

答案 0 :(得分:0)

您要插入一个单元格对象,我假设该单元格对象可以访问其中的数据,您需要执行此操作。

您需要执行Cells [x] [y] .getValue()或其他任何操作来代替Cell [x] [y],而不是getValue()来访问内部数据。