26

I need to export excel from viewlist, I used this code

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

        }

Now I need to overwrite the excel file without any message and I need to do this action every 10 min.

Mwigs
  • 231
  • 1
  • 14
didodido
  • 263
  • 1
  • 3
  • 7

1 Answers1

43

try this one.

       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();        
USER87
  • 547
  • 5
  • 3
  • thanks it's work fine now another thing please i need to run this app at once i open it like " page load " in asp.net web form !!!! without i press any button .. just open it and run it every 10 min ???? – didodido Aug 06 '14 at 10:09
  • 1
    You can also use `wb.SaveAs(Filename: @"C:\1\myExcel.xls", AccessMode: XlSaveAsAccessMode.xlNoChange);` to specify the parameters explicitly. – Mike Nov 23 '21 at 09:54
  • only working with app.DisplayAlerts = false; – L C Mar 23 '23 at 06:45