0

Here is my code:

        SaveFileDialog sd = new SaveFileDialog();
        sd.Title = "Select Excel Sheet to Export or Create New !";
        sd.Filter = "Excel files (*.xls)|*.xls";
        sd.FilterIndex = 0;

        sd.ShowDialog();

        if (sd.FileName != null)
        {
            AddWorksheetToExcelWorkbook(sd.FileName);
        }

Here is a simple 10 lines of code which i have been using for a very long time never any issue, but recently i am using this but i get error when i need to write to the file as below:

filename.. could not be found. Check the spelling of the file name, and verify that the file location is correct.

See image below its what we do , write filename and click save button , i only get error if i am making a new file not when i select existing. enter image description here

Why is this happening i have used this code many times, also i see there is no file created on the folder i save it to so why is the SaveFileDialog not saving files am i missing something?

UPDATE :

Work's fine if i select an already existing file issue only when i write name and press save.

confusedMind
  • 2,573
  • 7
  • 33
  • 74

1 Answers1

2

You need to call the OpenFile method in order to create or overwrite the file.

if (!String.IsNullOrEmpty(sd.FileName))
{
   using(var fileStream = sd.OpenFile())
   {
      //you can use the stream if you need it (otherwise just close it)
   }

   AddWorksheetToExcelWorkbook(sd.FileName);    
}

Caution from the MSDN page

For security purposes, this method creates a new file with the selected name and opens it with read/write permissions. This can cause unintentional loss of data if you select an existing file to save to. To save data to an existing file while retaining existing data, use the File class to open the file using the file name returned in the FileName property.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • But i have done this many times it works , why any different now? – confusedMind Sep 12 '13 at 00:02
  • I have another tool i run and it exports perfectly fine only thing different is that is to a txt file. – confusedMind Sep 12 '13 at 00:04
  • If the exact same code is working elsewhere then I suspect there's something else causing the issue. The extension shouldn't make a difference (i.e. if you replace `txt` with `xls`, it should still create a file even if it's not a valid xls file). I can only guess that the other code manually creates a file using the `File` class instead of the `OpenFile` method of the `SaveFileDialog`. – keyboardP Sep 12 '13 at 00:08
  • Yes, the thing is the code above only matter, if i am saving file using SaveFileDialog it should atleast create the file, but it does not do that and no errors... – confusedMind Sep 12 '13 at 00:10
  • doing your code above now i get error : Excel cannot open the file 'testNew.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file – confusedMind Sep 12 '13 at 00:11
  • it does create the file but even cannot open it in excel same error. – confusedMind Sep 12 '13 at 00:12
  • SaveFileDialog doesn't automatically create the file if the user selects `Save`. Create a new project and try it. The reason you're getting the invalid file error is because `OpenFile` simply creates an empty file. Just because you give it an `xls` extension, it doesn't make it an Excel file. You need to fill that file with content such as the data format and actual data. – keyboardP Sep 12 '13 at 00:14
  • How do it create an excel file with SaveFileDialog thn whats the purpose of it :| .. – confusedMind Sep 12 '13 at 00:16
  • So yo are saying the only way to do an export is first manually make a file on excel and then select it? – confusedMind Sep 12 '13 at 00:18
  • The purpose of the SaveFileDialog is to allow the user to select where to save a file using the standard dialog box that's consistent across Windows. Your job is to take in the filename and do the work of creating the file (either manually or with the `OpenFile` method) and fill the content of that file. I'm not familiar with creating XLS files but here's a thread that might help http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp – keyboardP Sep 12 '13 at 00:20
  • No, I'm saying that you're expecting the SaveFileDialog to do work that it's not designed to do. How you create the XLS file is up to you (see previous comment for some examples). If you want specifics on creating an XLS file from scratch then that's a whole different question someone else might be able to help with (or maybe there are other threads posted). – keyboardP Sep 12 '13 at 00:21