Resolved SaveAs excel file

AussieBoy

Well-known member
Joined
Sep 7, 2020
Messages
61
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.

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,904
Location
Sydney, Australia
Programming Experience
10+
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");
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,904
Location
Sydney, Australia
Programming Experience
10+
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

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,364
Location
Chesapeake, VA
Programming Experience
10+
Moved to Suites...
 
Top Bottom