简体   繁体   English

使用C#和Excel 2010打开时如何在不获取格式警告的情况下保存Excel文件

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

I'm using Excel 2010. I'm trying so save my excel file, with this code. 我正在使用Excel 2010.我正在尝试使用此代码保存我的excel文件。 It does save a .xls file, but when I open the file I get this message: 它确实保存了一个.xls文件,但是当我打开文件时,我收到了这条消息:

The file you are trying to open, 'tre.xls', is in a different format than specified by the file extension. 您尝试打开的文件“tre.xls”的格式与文件扩展名指定的格式不同。 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. 如果我按yes则文件打开。 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. 当我以正常方式保存excel时,我得到“Book1.xlsx”,打开没有问题。

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

With your code you save the workbook as Open XML because XlFileFormat.xlWorkbookDefault evaluates to XlFileFormat.xlOpenXMLWorkbook for Excel 2007+. 使用您的代码将工作簿保存为Open XML,因为XlFileFormat.xlWorkbookDefault计算结果为XlFileFormat.xlOpenXMLWorkbook for Excel 2007+。 This is the reason of the warning: you named the file as XLS but actually it is a XLSX. 这是警告的原因:您将文件命名为XLS,但实际上它是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 . 您可以在saveFileDialog.Filter属性中将文件扩展名更改为xlsx,或强制Excel对象使用XlFileFormat.xlExcel8以XLS格式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);

尝试将警报设置为OFF

oExcel.DisplayAlerts = false;   

This problem results from a feature called Extension Hardening, and you can find more information about it here . 此问题是由名为Extension Hardening的功能引起的,您可以在此处找到有关它的更多信息。

Unfortunately, the link above also states that there are no expected changes to this code until at least Office 14. 不幸的是,上面的链接还指出,至少在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 ). 打开注册表( 开始 - > 运行 - > regedit.exe )。

Navigate to HKEY_CURRENT_USER\\SOFTWARE\\MICROSOFT\\OFFICE\\12.0\\EXCEL\\SECURITY . 导航到HKEY_CURRENT_USER\\SOFTWARE\\MICROSOFT\\OFFICE\\12.0\\EXCEL\\SECURITY

Right click in the right window and choose New -> DWORD 右键单击右侧窗口,选择New - > DWORD

Type “ ExtensionHardening ” as the name (without the quotes) 输入“ ExtensionHardening ”作为名称(不带引号)

Verify that the data has the value “ 0 ″. 验证数据的值是否为“ 0 ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有收到警告的情况下存储Excel文件 - How do I store Excel File without getting the warning 如何在不使用剪贴板的情况下在C#中保存Excel中的图像? - How do I save images from Excel without using Clipboard, in C#? 如何在发生冲突时保存使用 C# 创建的 excel 文件时禁用提示对话框? - How to disable the prompt dialog when I save the excel file created using C# while conflict occurs? 如何在不打开c#winforms中的excel文件的情况下将excel工作表复制到另一个excel工作簿中? - How to copy excel worksheets Into another excel workbook without opening the excel file in c# winforms? 使用Interop在C#中打开Excel文件时出现异常 - Exception When Opening Excel File in C# Using Interop 将 excel 文件保存为 csv 格式,无需在 .net 核心 3.1 中打开 - save excel file to csv format without opening in .net core 3.1 当我使用Streamwriter在C#中创建Excel文件时,如何更改文本中的Excel列格式 - how to change excel column format in text when i use streamwriter to create a excel file in c# 如何在不使用C#打开EXCEL的情况下突出显示XSL文件中的行 - How to Highlight Row in XSL file without Opening EXCEL using C# 如何使用C#在Excel上保存.xlw格式 - How I can save a .xlw format on excel with c# 如何使用DLL在C#代码中读取Excel 2010文件? - How can I read an Excel 2010 file in my C# code using a DLL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM