c# - 读取Excel文件!总是出现一个对话框要求保存

标签 c# excel

我正在尝试从 Excel 文件中读取数据。我的问题是,当我读完后,总是出现一个要求保存的对话框。如何关闭它?
enter image description here
这是我的代码:

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

_Application.Quit method :

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/

相关文章:

c# - 向服务器添加 Web 界面的方法?

c# - Request.Url 返回没有域的本地主机

c# - 如果在 ASP.NET MVC 中授权失败,如何显示自定义错误

excel - PowerQuery - 文件夹查询导入新文件

html - 条件双极内表条形图 - Pandas - CSS

c# - WPF 访问 ListView 代码隐藏的滚动查看器

c# - 是否有一个 IEnumerable 实现只迭代一次它的源代码(例如 LINQ)?

vba - 在 Excel 2010 中使用 Enter 键运行宏,无论工作表是否已更改,在特定范围内按下时

javascript - 在 Excel JS Office 加载项的自定义函数中获取当前 URL

excel - VBA 中的双循环