简体   繁体   中英

C#(WCF) - Create excel file in network shared drive

My visual studio project is stored in C:\\Projects and project name is Project1 , file name is 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

            ...

Using debug, excelConStr returns

"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"

which looks fine. However, it is giving me the following error: '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. '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.

Any suggestion to resolve this issue? Please Experts!! (Note: There is no PERMISSON problem)

对于网络,我认为您不需要\\ server而是\\\\ server,不是吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM