Deepanshu Mehta

Deepanshu Mehta

  • NA
  • 33
  • 2.8k

how to load data tables in excel worksheets in a loop.

Sep 8 2017 1:36 AM
I am developing a DBF file comparison tool, one of the requirement is to get the comparison result in one excel file, but in different worksheets. i had used if-else loop, one to create the file load the first result table and save it at uuser defined location, Another loop is to open the file, adding another worksheet and and save the changes. but in the result file it shows the result from the first comparison loop, or with some modifications it start overriding the result.
 
can anyone please help me with it.
  1. Excel._Application app = new Excel.Application();  
  2. Excel._Workbook workbook = app.Workbooks.Add(7);  
  3. if (s == 0)  
  4. {  
  5. Excel._Worksheet worksheet = workbook.Worksheets.Add();  
  6. try  
  7. { worksheet.Name = dt.TableName; }  
  8. catch  
  9. {  
  10. worksheet.Name = "Extended" + dt.TableName;  
  11. }  
  12. worksheet.Cells[2, 1] = "NAME";  
  13. worksheet.Cells[2, 2] = "TAG";  
  14. worksheet.Cells[2, 3] = "status";  
  15. worksheet.Cells[2, 4] = "Reason";  
  16. for (int k = 0; k < tableC.Rows.Count; k++)  
  17. {  
  18. for (int l = 0; l < tableC.Columns.Count; l++)  
  19. {  
  20. worksheet.Cells[k + 3, l + 1] = tableC.Rows[k][l].ToString();  
  21. }  
  22. }  
  23. using (SaveFileDialog save = new SaveFileDialog())  
  24. {  
  25. save.Filter = "Excel Files(*.xls)|*.xlsx";  
  26. save.InitialDirectory = @"C:\Users\User\Downloads\Report Files";  
  27. save.Title = "Save Report File";  
  28. if (save.ShowDialog() == DialogResult.OK)  
  29. {  
  30. workbook.SaveCopyAs(save.FileName);  
  31. workbook.Saved = true;  
  32. }  
  33. }  
  34. }  
  35. else  
  36. { OpenFileDialog open = new OpenFileDialog();  
  37. open.Title = "Select File";  
  38. open.InitialDirectory = @"C:\Users\User\Downloads\Report Files";  
  39. open.Filter = "Excel Files(*.xlsx)|*.xlsx";  
  40. open.Multiselect = false;  
  41. open.RestoreDirectory = true;  
  42. if (open.ShowDialog() == DialogResult.OK)  
  43. {  
  44. Excel._Worksheet worksheet = workbook.Worksheets.Add();  
  45. //app.ActiveWorkbook.Sheets.Activate();  
  46. //Excel._Worksheet worksheet = workbook.Worksheets.Add();  
  47. //Excel._Worksheet worksheet = (Excel._Worksheet)workbook.ActiveSheet;  
  48. try { worksheet.Name = dt.TableName; }  
  49. catch { worksheet.Name = "Extended" + dt.TableName; }  
  50. worksheet.Cells[2, 1] = "NAME";  
  51. worksheet.Cells[2, 2] = "TAG";  
  52. worksheet.Cells[2, 3] = "status";  
  53. worksheet.Cells[2, 4] = "Reason";  
  54. for (int i = 0; i < tableC.Rows.Count; i++)  
  55. {  
  56. for (int j = 0; j < tableC.Columns.Count; j++)  
  57. {  
  58. worksheet.Cells[i + 3, j + 1] = tableC.Rows[i][j].ToString();  
  59. }  
  60. }  
  61. workbook.Save();  
  62. workbook.Saved = true;  
  63. }  
  64. }  
  65. workbook.Close();  
  66. app.Quit();

Answers (1)