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

49,253

Solution 1

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);

Solution 2

try to set alerts to OFF

oExcel.DisplayAlerts = false;   

Solution 3

This problem results from a feature called Extension Hardening, and you can find more information about it here.

Unfortunately, the link above also states that there are no expected changes to this code until at least Office 14.

If you don’t want to look for a solution, but just want to solve the problem, insert this key in your registry to suppress the notification:

[HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security] “ExtensionHardening”=dword:00000000

You can accomplish the above by doing the following:

Open your Registry (Start -> Run -> regedit.exe).

Navigate to HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\OFFICE\12.0\EXCEL\SECURITY.

Right click in the right window and choose New -> DWORD

Type “ExtensionHardening” as the name (without the quotes)

Verify that the data has the value “0″.

Share:
49,253
radbyx
Author by

radbyx

Backend developer at al dente. Former WebDeveloper at CapaSystems. I like to code in C#, MVC, JavaScript, Jquery, winforms, MSSQL, MySql ect. :) email: radbyx AT gmail DOT com

Updated on July 09, 2022

Comments

  • radbyx
    radbyx almost 2 years

    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 yesthe 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
        }
    }