Monday 22 April 2019

c# - The file name or path doesn't exist or used by other program while writing Excel file




I am trying to create an Excel file and write some data to it.
Here is the code I am using.



using excel = Microsoft.Office.Interop.Excel;
excel.Application xlapp;
excel.Workbook xlworkbook;
excel.Worksheet xlworksheet;
object misvalue = System.Reflection.Missing.Value;


xlapp = new excel.ApplicationClass();
xlworkbook = xlapp.Workbooks.Add(misvalue);

xlworksheet = (excel.Worksheet)xlworkbook.Worksheets.get_Item(1);
xlworksheet.Cells[1, 1] = "Muneeb Hassan Soomro";
xlworkbook.SaveAs("csharp-excelwrite.xls",excel.XlFileFormat.xlWorkbookNormal,misvalue,misvalue,misvalue,misvalue,excel.XlSaveAsAccessMode.xlExclusive,misvalue,misvalue,misvalue,misvalue,misvalue);
//xlworkbook.SaveAs("csharp-Excel.xls", excel.XlFileFormat.xlWorkbookNormal);
xlworkbook.Close(true, misvalue, misvalue);
xlapp.Quit();



I get an exception on the xlworkbook.saveas() call. says:




The file name or path doesn't exist or used by other program




What i am doing wrong here?


Answer




So from one of your comments on another answer I finally got the exception text (This information should have been included in the question!)




The file name or path doesn't exist or used by other program




The solution should be simple: Specify a full path in the SaveAs call, not only a file name. How should Excel know in which folder it should save the file otherwise?


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...