Farzad M
Farzad M

Reputation: 67

Cannot be accessed. The file may be corrupted, located on a server that is not responding, or read-only

I am attempting to export data to a excel spreadsheet and so open that Excel spreadsheet using Visual Studio 2010 and Excel 2010 on Windows 7, but I get the following error.

'1.xls' cannot be accessed. The file may be corrupted, located on a server that is not responding, or read-only.

I have checked the file path and given Everyone full control of the file. Here is my code.

        saveFileDialog1.FileName = "";
        saveFileDialog1.Filter = "(Excel Files)|*.xls";
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            System.IO.Stream st = new System.IO.FileStream(saveFileDialog1.FileName,
                                      FileMode.Create,
                                      FileAccess.Write,
                                      FileShare.None);
            this.gridEXExporter1.ExportMode = Janus.Windows.GridEX.ExportMode.AllRows;
            gridEXExporter1.Export(st);

      //////////////////////////////////////
      /////   Opening the Excel File
      /////////////////////////////////////
       Process.Start(saveFileDialog1.FileName);

Thanks in advance.

Upvotes: 1

Views: 10189

Answers (1)

Justin
Justin

Reputation: 6711

You probably have to close the output file before you try to open it using Excel. Call st.Close() before Process.Start.

Upvotes: 2

Related Questions