C# 导入Excel数据到Datatable 在本地调试时没问题,上传到服务器就报错。

我的源代码:
System.Data.DataTable UMCIT = new System.Data.DataTable();
string conn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\test.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
System.Data.OleDb.OleDbConnection MyConnection = new System.Data.OleDb.OleDbConnection(conn);
System.Data.OleDb.OleDbDataAdapter MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [VariantsData$]", MyConnection);
MyConnection.Open();
MyCommand.Fill(UMCIT);
MyConnection.Close();
MyCommand = null;
MyConnection = null;
报错:
The Microsoft Jet database engine could not find the object 'C:\test.xls'. Make sure the object exists and that you spell its name and the path name correctly.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: The Microsoft Jet database engine could not find the object 'C:\Creat.xls'. Make sure the object exists and that you spell its name and the path name correctly.

请各位高手帮帮忙!
改成下面这个样子也是不行的,同样报一样的错,文件就再我的C:里
string conn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test.xls;Extended Properties='Excel

Source=c:\test.xls;
上传到服务器,就会在服务器的c盘根目录找这个excel文件。
你要导入的excel文件需要先上传服务器的某个目录,
然后得到真实路径,然后才能操作。
不能使用绝对路径。因为你找的是你自己机子上的excel文件。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-04-15
string conn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\test.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";

前面加了 @ 后面就不需要 改成 C:\\test.xls 了。

希望对你有帮助

就是这样的,你把分给这位兄弟吧!他回答的是对的,你既然用了@,就不需要再用\\了,一个就够了,用了@,字符串就不会把字符串里面的东西当做转义字符来解析了!

不过,看你的错误,应该是没有找到文件!你查看下有没有你指定的文件!
第2个回答  2010-04-16
是路径问题吧
服务器上有 'C:\Creat.xls' 文件吗

文件在你的c盘 在服务器上当然找不到了
确保服务器的c盘有该文件。
第3个回答  2010-04-15
string conn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\test.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";

前面加了 @ 后面就不需要 改成 C:\\test.xls 了。

希望对你有帮助
相似回答