如何在没有消息的情况下保存/覆盖现有的Excel文件

时间:2014-08-06 07:37:17

标签: c# winforms overwrite

我需要从视图列表中导出excel,我使用了这段代码

Excel.Application app = new Excel.Application();
            //app.Visible = true;
            Excel.Workbook wb = app.Workbooks.Add(1);
            Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];

            int i = 1;
            int i2 = 1;
            foreach (ListViewItem lvi in lvLogs.Items)
            {
                i = 1;
                foreach (ListViewItem.ListViewSubItem lvs in lvi.SubItems)
                {
                    ws.Cells[i2, i] = lvs.Text;
                    i++;
                }
                i2++;
            }


            wb.SaveAs(@"C:\1\myExcel.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing,
                        Type.Missing, Type.Missing);
            wb.Close(false, Type.Missing, Type.Missing);

            app.Quit();

        }

现在我需要在没有任何消息的情况下覆盖excel文件,我需要每10分钟执行一次此操作。

1 个答案:

答案 0 :(得分:26)

尝试这个。

       app.DisplayAlerts = false;
        wb.SaveAs(@"C:\1\myExcel.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, Type.Missing);
        workbook.Close();
        app.Quit();