darma teja

darma teja

  • NA
  • 496
  • 337.5k

save data from every loop in excel

Apr 3 2013 2:27 PM
Hi All,

I wanted to export data to excel. 
here is the code that i wrote:

Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
            Workbook vb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
            var Worksheet = (Worksheet)xla.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                 foreach (logic)
            {
                 //logic to get data
                // I put data to excel cells
                example:  Worksheet.Cells[i + 1, 1] = bla bla;
             }
     Worksheet ws = (Worksheet)xla.ActiveSheet;
     ws.SaveAs(@"c:\Documents and Settings\Desktop\output1.xls");

Now my problem is: I dont want to save xls file in last step.

I want to save data in excel file in every loop. It means that If any exception comes at least I have data of the passed loops.

Many thanks,

Darma



Answers (2)