Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. public class ExcelWriter
  2. {
  3. /// <summary>
  4. /// Constructs a new instance.
  5. /// </summary>
  6. public ExcelWriter()
  7. {
  8. // Do not delete - a parameterless constructor is required!
  9. }
  10.  
  11.  
  12. public void Driver(int row , int col, string time, string sheetName){
  13.  
  14. string sDataFile = "Ranorex_Reports.xls";
  15. string sFilePath = Path.GetFullPath(sDataFile);
  16. //Report.Success(sFilePath);
  17. //C:Usersmudit.srivastavDocumentsRanorexRanorexStudio ProjectsVopakAutomationVopak_AutomationVopak_AutomationbinDebugRanorex_Reports.xls
  18. //C:Usersmudit.srivastavDocumentsRanorexRanorexStudio ProjectsVopakAutomationVopak_AutomationPEPI_PerformanceExecutionReport
  19. string sOldvalue = "Vopak_Automation\bin\Debug\" + sDataFile;
  20. sFilePath = sFilePath.Replace(sOldvalue,"")+ "PEPI_Performance\ExecutionReport\" + sDataFile;
  21. fnOpenExcel(sFilePath,sheetName);
  22. writeExcel(row,col,time);
  23. fnCloseExcel();
  24. }
  25. Excel.Application exlApp ;
  26. Excel.Workbook exlWB ;
  27. Excel.Sheets excelSheets ;
  28. Excel.Worksheet exlWS;
  29. //Open Excel file
  30. public int fnOpenExcel(string sPath, string iSheet){
  31.  
  32. int functionReturnValue = 0;
  33. try {
  34.  
  35. exlApp = new Excel.ApplicationClass(); //Microsoft.Office.Interop.Excel.Application();
  36. exlApp.Visible = true;
  37. exlWB = exlApp.Workbooks.Open(sPath,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
  38.  
  39. // get all sheets in workbook
  40. excelSheets = exlWB.Worksheets;
  41.  
  42. // get some sheet
  43. //string currentSheet = "Cycle1";
  44. exlWS = (Excel.Worksheet)excelSheets.get_Item(iSheet);
  45. functionReturnValue = 0;
  46. }
  47. catch (Exception ex) {
  48. functionReturnValue = -1;
  49. Report.Error(ex.Message);
  50. }
  51. return functionReturnValue;
  52. }
  53.  
  54.  
  55. // Close the excel file and release objects.
  56. public int fnCloseExcel(){
  57. //exlWB.Close();
  58.  
  59. try{
  60. exlApp.ActiveWorkbook.Save();
  61. exlApp.Quit();
  62.  
  63. System.Runtime.InteropServices.Marshal.ReleaseComObject(exlWS);
  64. System.Runtime.InteropServices.Marshal.ReleaseComObject(exlWB);
  65. System.Runtime.InteropServices.Marshal.ReleaseComObject(exlApp);
  66.  
  67. GC.GetTotalMemory(false);
  68. GC.Collect();
  69. GC.WaitForPendingFinalizers();
  70. GC.Collect();
  71. GC.GetTotalMemory(true);
  72. }catch(Exception ex){
  73. Report.Error(ex.Message);
  74. }
  75. return 0;
  76. }
  77.  
  78. public void writeExcel(int i, int j , string time){
  79. Excel.Range exlRange = null;
  80. exlRange = (Excel.Range)exlWS.UsedRange;
  81. ((Excel.Range)exlRange.Cells[i,j]).Formula = time;
  82.  
  83. }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement