How do I save an Excel file without getting a format warning when opening, using C# and Excel 2010

c#, excel, format, savefiledialog, visual-studio-2010

Solution

With your code you save the workbook as Open XML because `XlFileFormat.xlWorkbookDefault` evaluates to `XlFileFormat.xlOpenXMLWorkbook` for Excel 2007+. This is the reason of the warning: you named the file as XLS but actually it is a XLSX.

You can change the file extension to xlsx in your `saveFileDialog.Filter` property or force the Excel object to save in the XLS format using `XlFileFormat.xlExcel8`.

EDIT Use this to save your document:

wbook.SaveAs(saveFileDialog.FileName, XlFileFormat.xlExcel8, 
            Type.Missing, Type.Missing, false, false, 
            XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, 
            Type.Missing, Type.Missing, Type.Missing);

Problem

I'm using Excel 2010. I'm trying so save my excel file, with this code. It does save a .xls file, but when I open the file I get this message: The file you are trying to open, 'tre.xls', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a thrusted source before opening the file. Do you want to open the file now? If I press `yes`the file opens. But what do I need to get rid of this format-popup? My code: ``` using System; using System.Windows.Forms; using ExcelAddIn1.Classes; using ExcelAddIn1.Classes.Config; using Microsoft.Office.Interop.Excel; using Application = Microsoft.Office.Interop.Excel.Application; using System.Runtime.InteropServices; private void Foo(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Execl files (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = true; saveFileDialog.FileName = null; saveFileDialog.Title = "Save path of the file to be exported"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { // Save. // The selected path can be got with saveFileDialog.FileName.ToString() Application excelObj = (Application)Marshal.GetActiveObject("Excel.Application"); Workbook wbook = excelObj.ActiveWorkbook; wbook.SaveAs(saveFileDialog.FileName, XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, false, false, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); wbook.Close(); } } ``` When I save in excel in the normal way I get "Book1.xlsx", that have no problem to open. ============= FINAL SOLUTION ============ ``` private void Foo(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Execl files (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = true; saveFileDialog.FileName = null; saveFileDialog.Title = "Save path of the file to be exported"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { Application excelObj = (Application)Marshal.GetActiveObject("Excel.Application"); Activate(); // <-- added (recommend by msdn, but maybe not nessary here) Workbook wbook = excelObj.ActiveWorkbook; wbook.SaveAs(saveFileDialog.FileName, XlFileFormat.xlExcel8, Type.Missing, Type.Missing, false, false, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); // wbook.Close(); // <-- don't want this anymore } } ```

Original source