使用C#和Excel 2010,打开时如何保存Excel文件而不会收到格式警告 [英] How do I save an Excel file without getting a format warning when opening, using C# and Excel 2010
问题描述
我正在使用Excel 2010.
我正在尝试使用此代码保存我的excel文件。
它保存一个.xls文件,但是当我打开文件时,我收到这个消息:
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:
你正在尝试的文件要打开tre.xls,与文件扩展名指定的格式不同。在打开文件之前,验证文件是否已损坏,并且来自推送的源。你现在要打开文件吗?
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?
我的代码:
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();
}
}
当我以正常的方式保存在excel中时Book1.xlsx,没有问题要打开。
When I save in excel in the normal way I get "Book1.xlsx", that have no problem to open.
============= FINAL SOLUTION ======= =====
============= 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
}
}
推荐答案
使用您的代码将工作簿保存为Open XML,因为 XlFileFormat.xlWorkbookDefault
评估为 XlFileFormat.xlOpenXMLWorkbook
for Excel 2007+。这是警告的原因:您将文件命名为XLS,但实际上它是XLSX。
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.
您可以将文件扩展名更改为xlsx,您的 saveFileDialog.Filter
属性,或强制Excel对象使用 XlFileFormat.xlExcel8
保存为XLS格式。
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);
这篇关于使用C#和Excel 2010,打开时如何保存Excel文件而不会收到格式警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!