从Excel读取时,OleDbDataAdapter返回零行

时间:2019-07-25 12:45:27

标签: c# asp.net excel webforms

因此,我尝试使用此代码从excel文件读取数据,但DataTable返回零行,它不会给出任何错误,只是一个空的DataTable。请帮忙。

public DataTable ReadExcel(string fileName)
{
    string conn = string.Empty;
    DataTable dtExcel = new DataTable();

    conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" fileName + ";Extended Properties='Excel 12.0;HDR=NO';";

    using (OleDbConnection con = new OleDbConnection(conn))
    {
        try
        {
            OleDbDataAdapter oleAdpt = new OleDbDataAdapter("select * from [sample$]", con);
            oleAdpt.Fill(dtExcel);
        }
        catch { }
    }
    return dtExcel;
}

0 个答案:

没有答案