/DasCoreLib/ExcelManager/ExcelFile.cs

https://bitbucket.org/kumarvvr/data-acquisition-systems · C# · 332 lines · 249 code · 56 blank · 27 comment · 26 complexity · b0094bafd195491a7c8f7b993c096d6c MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Excel = Microsoft.Office.Interop.Excel;
  6. using System.Diagnostics;
  7. namespace ExcelManager
  8. {
  9. public class ExcelFile
  10. {
  11. string fileName;
  12. Excel.Application xlApp;
  13. Excel.Workbook workBook;
  14. Dictionary<string, Excel.Worksheet> xlSheets;
  15. Excel.Worksheet currentWorksheet;
  16. Excel.Range currentWorksheetCells;
  17. bool isWorksheetOpen = false;
  18. bool isOpen = false;
  19. public ExcelFile(string fullName)
  20. {
  21. this.fileName = fullName;
  22. }
  23. public bool Open(bool isRunningInBackground)
  24. {
  25. if (!isOpen)
  26. {
  27. try
  28. {
  29. if (xlApp == null)
  30. {
  31. xlApp = new Excel.ApplicationClass();
  32. xlApp.Visible = isRunningInBackground;
  33. }
  34. workBook = xlApp.Workbooks.Open(fileName);
  35. GetWorksheets();
  36. isOpen = true;
  37. }
  38. catch (Exception ex)
  39. {
  40. DisposeComObjects();
  41. throw new Exception("Count not open Excel File : " + ex.Source + " : " + ex.Message);
  42. }
  43. }
  44. else
  45. {
  46. return false;
  47. }
  48. return true;
  49. }
  50. public int GetNumberOfWorksheets()
  51. {
  52. return xlSheets.Count;
  53. }
  54. public List<string> ListWorksheets()
  55. {
  56. List<string> result = new List<string>();
  57. if (isWorksheetOpen)
  58. {
  59. for (int i = 0; i < xlSheets.Count; i++)
  60. {
  61. result.Add(xlSheets.ElementAt(i).Key);
  62. }
  63. }
  64. return result;
  65. }
  66. public void SelectWorksheet(string sheetName)
  67. {
  68. if (!isWorksheetOpen)
  69. {
  70. currentWorksheet = xlSheets[sheetName];
  71. currentWorksheetCells = GetCurrentWorksheetCells();
  72. isWorksheetOpen = true;
  73. }
  74. else
  75. {
  76. //ReleaseInteropObject(currentWorksheetCells);
  77. //ReleaseInteropObject(currentWorksheet);
  78. currentWorksheet = xlSheets[sheetName];
  79. currentWorksheetCells = GetCurrentWorksheetCells();
  80. }
  81. }
  82. public bool FindAndReplaceMarkers(Dictionary<string, string> data)
  83. {
  84. if (isWorksheetOpen)
  85. {
  86. Excel.Range cells = currentWorksheetCells;
  87. Excel.Range found = null;
  88. KeyValuePair<string, string> keyval;
  89. for(int i = 0;i<data.Count;i++)
  90. {
  91. keyval = data.ElementAt(i);
  92. found = cells.Find(keyval.Key);
  93. if (found != null)
  94. {
  95. found.Value = found.Value.ToString().Replace(keyval.Key, keyval.Value);
  96. }
  97. }
  98. //ReleaseInteropObject(found);
  99. }
  100. return false;
  101. }
  102. public bool FindMarkedCell(out int rowIndex, out int columnIndex, string marker)
  103. {
  104. rowIndex = -1;
  105. columnIndex = -1;
  106. if (isWorksheetOpen)
  107. {
  108. Excel.Range foundCell;
  109. foundCell = currentWorksheetCells.Find(marker);
  110. if (foundCell != null)
  111. {
  112. rowIndex = foundCell.Row;
  113. columnIndex = foundCell.Column;
  114. //ReleaseInteropObject(foundCell);
  115. return true;
  116. }
  117. else
  118. {
  119. //ReleaseInteropObject(foundCell);
  120. return false;
  121. }
  122. }
  123. else
  124. {
  125. return false;
  126. }
  127. return false;
  128. }
  129. public bool FillDataInColumn(int startRow, int startCol, List<string> data)
  130. {
  131. int row, col;
  132. row = startRow;
  133. col = startCol;
  134. Excel.Range currentCell = null;
  135. try
  136. {
  137. for (int i = 0; i < data.Count; i++)
  138. {
  139. currentCell = (Excel.Range)currentWorksheet.Cells[row + i, col];
  140. currentCell.Value = data[i];
  141. }
  142. }
  143. catch (Exception ex)
  144. {
  145. throw new Exception("Error writing values to excel sheet :" + currentWorksheet.Name + " ERROR : " + ex.Message);
  146. }
  147. //ReleaseInteropObject(currentCell);
  148. return true;
  149. }
  150. public bool SaveFile()
  151. {
  152. try
  153. {
  154. workBook.Save();
  155. }
  156. catch (Exception ex)
  157. {
  158. throw new Exception(ex.Message);
  159. }
  160. return true;
  161. }
  162. public void CloseFile()
  163. {
  164. DisposeComObjects();
  165. }
  166. private void DisposeComObjects()
  167. {
  168. GC.Collect();
  169. GC.WaitForPendingFinalizers();
  170. if (currentWorksheetCells != null)
  171. ReleaseInteropObject(currentWorksheetCells);
  172. if (currentWorksheet != null)
  173. ReleaseInteropObject(currentWorksheet);
  174. for (int i = 0; i < xlSheets.Count; i++)
  175. ReleaseInteropObject(xlSheets.ElementAt(i).Value);
  176. if (workBook != null)
  177. workBook.Close();
  178. ReleaseInteropObject(workBook);
  179. if (xlApp != null)
  180. xlApp.Quit();
  181. ReleaseInteropObject(xlApp);
  182. try
  183. {
  184. Process proc;
  185. proc = Process.GetProcessesByName("EXCEL")[0];
  186. proc.Kill();
  187. }
  188. catch (Exception ex)
  189. {
  190. // Write to log file....
  191. }
  192. }
  193. private void GetWorksheets()
  194. {
  195. Excel.Worksheet sheet = null;
  196. xlSheets = new Dictionary<string, Excel.Worksheet>();
  197. for (int i = 1; i <= workBook.Sheets.Count; i++)
  198. {
  199. sheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
  200. xlSheets.Add(sheet.Name, sheet);
  201. }
  202. //ReleaseInteropObject(sheet);
  203. }
  204. private Excel.Range GetCurrentWorksheetCells()
  205. {
  206. return currentWorksheet.Cells;
  207. }
  208. private void ReleaseInteropObject(object target)
  209. {
  210. try
  211. {
  212. if (target != null)
  213. {
  214. System.Runtime.InteropServices.Marshal.ReleaseComObject(target);
  215. target = null;
  216. }
  217. }
  218. catch (Exception ex)
  219. {
  220. throw new Exception("Error releasing excel application resources. Error : " + ex.Message);
  221. }
  222. finally
  223. {
  224. GC.WaitForPendingFinalizers();
  225. GC.Collect();
  226. }
  227. }
  228. public bool PrintFile(bool showPreview, int copies)
  229. {
  230. try
  231. {
  232. currentWorksheet.PrintOutEx(1, 1, copies, showPreview, Type.Missing, false, true, Type.Missing, Type.Missing);
  233. }
  234. catch (Exception ex)
  235. {
  236. throw new Exception("Error Printing file...." + ex.Message);
  237. }
  238. return true;
  239. }
  240. public bool PrintWorkSheet(string sheetname,bool showPreview, int copies)
  241. {
  242. Excel.Worksheet sheet = xlSheets[sheetname];
  243. try
  244. {
  245. sheet.PrintOutEx(1, 1, copies, showPreview, Type.Missing, false, true, Type.Missing, Type.Missing);
  246. }
  247. catch (Exception ex)
  248. {
  249. ReleaseInteropObject(sheet);
  250. throw new Exception("Error Printing file...." + ex.Message);
  251. }
  252. //ReleaseInteropObject(sheet);
  253. return true;
  254. }
  255. #region TO BE IMPLEMENTED
  256. /*
  257. public bool WriteDataByCellTag(Dictionary<string, List<string>> data)
  258. {
  259. return false;
  260. }
  261. public bool WriteDataByRowTag(Dictionary<string, List<string>> data)
  262. {
  263. return false;
  264. }
  265. public void AddNewSheet(string sheetName)
  266. {
  267. }
  268. */
  269. #endregion
  270. }
  271. }