【问题标题】:C#: How to import excel to datagridview? [closed]C#:如何将excel导入datagridview? [关闭]
【发布时间】:2015-08-30 06:42:51
【问题描述】:

我想知道如何导入 excel .xslsx 我可以做 .xsls 但如果你不能回答就无法使用 xslsx 只是给我有用的链接

【问题讨论】:

标签: c# .net visual-studio-2010 datagridview


【解决方案1】:

我正在使用一个函数将 Excel 工作表导入数据表,它正在与我一起工作

请检查一下,如果您需要什么,请告诉我

    public DataTable ImportExceltoDatatable(string filepath)
    {
        string sqlquery = "Select * From [Sheet1$]";
        DataSet ds = new DataSet();
        string constring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0;HDR=YES;\"";
        OleDbConnection con = new OleDbConnection(constring + "");
        OleDbDataAdapter da = new OleDbDataAdapter(sqlquery, con);
        da.Fill(ds);
        DataTable dt = ds.Tables[0];
        return dt;
    }

此函数将返回一个数据表,您可以在 datagridview 绑定中使用该数据表

问候,

【讨论】:

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