我的Visual Studio项目存储在C:\Projects
项目名称是Project1
,文件名是Test.xls
string directory = AppDomain.CurrentDomain.BaseDirectory;
// directory = C:\\Projects\\Project1 (local drive)
directory = directory.Remove(0, 2);
// directory = \\Projects\\Project1
string newDirectory = "\\server" + directory;
// newDirectory = \\server\\Projects\\Project1 (now, network shared drive)
string pathCreate = System.IO.Path.Combine(newDirectory, file);
// pathCreate = \\server\\Projects\\Project1\\Test.xls
string excelConStr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + pathCreate + @"; Extended Properties=""Excel 8.0;HDR=Yes; MultipleActiveResultSets=True"";";
private void CreateExcel(string excelConStr)
{
DataTable table = new DataTable();
using (OleDbConnection excelCreateCon = new OleDbConnection(excelConStr))
{
OleDbCommand excelCreateCmd = new OleDbCommand();
excelCreateCmd.Connection = excelCreateCon
...
使用调试,excelConStr 返回
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=\\server\\Projects\\Project1\\Test.xls; Extended Properties=\"Excel 8.0;HDR=Yes; MultipleActiveResultSets=True\";"
excelCreateCmd.Connection.DataSource returns "\\server\\Projects\\Project1\\Test.xls"
看起来不错。但是,它给了我以下错误:'C:\server\Projects\Project1\Test.xls' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.
有什么建议可以解决这个问题吗?请高手!!(注:不存在PERMISSON问题)