如何导入excel文件。xls到asp.net

时间:2021-04-20 01:45:58

I am trying to read data from excel.

我试着从excel中读取数据。

if (fileext.Trim() == ".xls")
{
    connStr= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;HDR=Yes;IMEX=2";
}
else if (dosyauzanti.Trim() == ".xlsx")
{
    connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}

I can read Excel 2007 files. but I get an error when I try to read Excel 2003 files.

我可以阅读Excel 2007文件。但是当我试图读取Excel 2003文件时,我得到了一个错误。

External table is not in the expected format.

外部表不是预期的格式。

1 个解决方案

#1


1  

As commented correctly by sous2817 you are missing the quotes in your first connection string

正如sous2817正确地注释的那样,您正在丢失第一个连接字符串中的引号

connStr= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";

Just to add more info to it, Excel 2007 vcersion files are Excel 12.0 and Excel 2003 are Excel 8.0.

为了添加更多的信息,Excel 2007 vcersion文件是Excel 12.0, Excel 2003是Excel 8.0。

#1


1  

As commented correctly by sous2817 you are missing the quotes in your first connection string

正如sous2817正确地注释的那样,您正在丢失第一个连接字符串中的引号

connStr= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";

Just to add more info to it, Excel 2007 vcersion files are Excel 12.0 and Excel 2003 are Excel 8.0.

为了添加更多的信息,Excel 2007 vcersion文件是Excel 12.0, Excel 2003是Excel 8.0。