Resolved SaveAs excel file

AussieBoy

Well-known member
Joined
Sep 7, 2020
Messages
78
Programming Experience
Beginner
Hi, I am trying to run the below code. I have used similar and it has worked.
However, when I try to open this with excel it complains about the format or file type.
Hopefully something simple. But I cant see it at the moment.
Thanks,
C#:
xlWorkBook.SaveAs("C:\\DM\\Batches\\Batch" + BN + "\\" + strSN + "\\Updated" + strSN + "PWNNs.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlCSVWindows, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges, false, Type.Missing, Type.Missing, Type.Missing);
 
Solution
As for the issue, it presumably relates to the format you're specifying the file be saved as. The issue appears to be that you're you're telling it to save as a CSV file but giving it a ".xlsx" extension. If you really want a CSV file then use the ".csv" extension. If you want an actual XLSX file then specify to save as that format, i.e. use the xlOpenXMLWorkbook field to specify the format.
The very first ting I would say, which likely has nothing to do with your issue, is to not build a file path like that. Use string interpolation, verbatim string literals to avoid having to escape delimiters and use Path.Combine:
C#:
var filePath = Path.Combine($@"C:\DM\Batches\Batch{BN}",
                            strSN,
                            $"Updated{strSN}PWNNs.xlsx");
 
As for the issue, it presumably relates to the format you're specifying the file be saved as. The issue appears to be that you're you're telling it to save as a CSV file but giving it a ".xlsx" extension. If you really want a CSV file then use the ".csv" extension. If you want an actual XLSX file then specify to save as that format, i.e. use the xlOpenXMLWorkbook field to specify the format.
 
Solution
Moved to Suites...
 
Back
Top Bottom