【问题标题】:Read excel file into datagridview c#将excel文件读入datagridview c#
【发布时间】:2020-03-30 23:50:48
【问题描述】:

我正在尝试将特定的 excel 文件读入我的名为 stats_table 的数据网格视图中,但大多数情况下整个系统崩溃或根本没有做任何事情。我在特定按钮下使用的导入代码如下:

private void Predict_Click(object sender, EventArgs e)
{
    string path = @"C:\Users\epifa\Desktop\Master\2nd semester\esports\all_cards.xlsx";
    string constr = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = "+ path + " Extended Properties=\"Excel 12.0 Macro; HDR = YES";
    OleDbConnection con = new OleDbConnection(constr);
    DataTable dt = new DataTable();
    Stats_table.DataSource = dt;
}

你能建议吗?

【问题讨论】:

  • 这能回答你的问题吗? Import Excel file to Datagridview in C# or VB.Net
  • 尝试以下操作:字符串路径 = @"C:\Users\epifa\Desktop\Master\2nd 学期\esports\all_cards.xlsx"; string constr = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = "+ path + " Extended Properties=\"Excel 12.0 Macro; HDR = YES"; OleDbConnection con = new OleDbConnection(constr); string commandText = "Select * From Sheet1"; OleDbDataAdapter adapter = new OleDbDataAdapter(commandText, con); DataTable dt = new DataTable(); adapter.Fill(dt); Stats_table.DataSource = dt;
  • 你是真的想给你的Stats_table一个空的数据表作为数据源,还是这只是一个伪命题?你在填写数据表吗?

标签: c# datagridview


【解决方案1】:

经过多次尝试,我终于得到了它,

OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source =  C:\Users\epifa\Desktop\Master\2nd semester\esports\all_cards.xlsx" + @"; Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";
OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", conn);
DataSet stats = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
adapter.Fill(stats);
Stats_table.DataSource = stats.Tables[0];

还是谢谢你:)

【讨论】:

    【解决方案2】:

    你需要弄清楚发生了什么。

    显然发生了异常。

    您可以捕获异常以了解发生了什么:

    private void Predict_Click(object sender, EventArgs e)
    {
        try 
        {
            string path = @"C:\Users\epifa\Desktop\Master\2nd semester\esports\all_cards.xlsx";
            string constr = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = "+ path + " Extended Properties=\"Excel 12.0 Macro; HDR = YES";
            OleDbConnection con = new OleDbConnection(constr);
            DataTable dt = new DataTable();
            Stats_table.DataSource = dt;
        } catch (Exception e) {
            Console.WriteLine(e.Message);
        }
    }
    

    在你知道问题是什么之后,你可以继续解决它。

    【讨论】:

    • 我认为他是在喂空数据表作为数据源。
    【解决方案3】:

    正如你评论的那样……“然后因为我知道连接正在工作”……这是值得商榷的。然而,假设你有一个有效的连接......代码似乎没有做任何“与”连接......例如,在线......

    OleDbConnection con = new OleDbConnection(constr);
    

    这行代码很可能会成功,但您并没有对连接做任何事情……比如用 excel 工作簿中的数据“填充”数据表。

    在这行代码之后,会创建一个 NEW EMPTY DataTable,然后将这个“空”表设置为网格中的 DataSource。除了“空”网格之外,我不确定您还能期待什么。

    最后,连接字符串似乎缺少某些语法,因此可能会失败。下面是两个将打开 XLS 和 XLSX 文件的示例。请记住,您需要知道要填写表格的工作表的名称。

    要打开带有名为“sheet1”的工作表的 XLSX 文件...

    private void btn_OpenXLSX_Click(object sender, EventArgs e) {
      string path = @"path_to_the_XLSX_file";
      string worksheetName = "sheet1";
      string constr = "Provider = Microsoft.Ace.OLEDB.12.0; Data Source = " + path + "; Extended Properties=\"Excel 12.0; HDR = YES;Imex=1;\";";
      OleDbConnection con = new OleDbConnection(constr);
      OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [" + worksheetName + "$]", con);
      DataTable dt = new DataTable();
      myDataAdapter.Fill(dt);
      Stats_table.DataSource = dt;
    }
    

    要打开带有名为“sheet1”的工作表的 XLS 文件...

    private void btn_OpenXLS_Click(object sender, EventArgs e) {
      string path = @"path_to_the_XLS_file";
      string worksheetName = "sheet1";
      string constr = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + path + "; Extended Properties=\"Excel 8.0; HDR = YES;Imex=1;\";";
      OleDbConnection con = new OleDbConnection(constr);
      OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [" + worksheetName + "$]", con);
      DataTable dt = new DataTable();
      myDataAdapter.Fill(dt);
      Stats_table.DataSource = dt;
    }
    

    我希望这有意义并有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 2015-11-27
      • 1970-01-01
      • 2018-02-19
      相关资源
      最近更新 更多