我正在尝试从 Excel 文件中读取数据。我的问题是,当我读完后,总是出现一个要求保存的对话框。如何关闭它?
这是我的代码:
Missing missing = Missing.Value;
Excel.Application _app = new Excel.Application();
Excel.Workbook _workBook = _app.Workbooks.Open(e.Argument.ToString());
Excel.Worksheet _workSheet = _workBook.ActiveSheet;
Excel.Range _range = _workSheet.UsedRange;
BackgroundWorker bw = sender as BackgroundWorker;
for (int i = 0; i < _range.Rows.Count; i++) {
if (_workSheet.Cells[i + 2, 1].Value != null && _workSheet.Cells[i + 2, 2].Value != null)
{
inputList.Add(new infoInput(_workSheet.Cells[i + 2, 1].Value, _workSheet.Cells[i + 2, 1].Value));
}
bw.ReportProgress(((i + 1) * 100)/_workSheet.UsedRange.Rows.Count);
}
_workBook.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(_workBook);
_app.DisplayAlerts = false;
_app.Quit();
P/S 对不起我的英语!提前致谢。
最佳答案
您应该将 DisplayAlerts
属性设置为 FALSE,以防止在调用 .Quit()
方法时出现此消息。
_app.DisplayAlerts = false;
_app.Quit();
If unsaved workbooks are open when you use this method, Microsoft Excel displays a dialog box asking whether you want to save the changes. You can prevent this by saving all workbooks before using the Quit method or by setting the DisplayAlerts property to False. When this property is False, Microsoft Excel doesn’t display the dialog box when you quit with unsaved workbooks; it quits without saving them.
关于c# - 读取Excel文件!总是出现一个对话框要求保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20739530/