|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Data; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.IO; |
| 7 | +using ExcelDataReader; |
| 8 | +using DocumentFormat.OpenXml.Packaging; |
| 9 | +using Ex = Microsoft.Office.Interop.Excel; |
| 10 | +using System.Xml.Linq; |
| 11 | + |
| 12 | +namespace Framework |
| 13 | +{ |
| 14 | + public class Library |
| 15 | + { |
| 16 | + public DataTable ExcelToDataTable(string fileName, string tableName) |
| 17 | + { |
| 18 | + //open file and returns as Stream |
| 19 | + var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); |
| 20 | + |
| 21 | + //Createopenxmlreader via ExcelReaderFactory |
| 22 | + IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); //.xlsx |
| 23 | + |
| 24 | + //Set the First Row as Column Name |
| 25 | + //excelReader.IsFirstRowAsColumnNames = true; |
| 26 | + |
| 27 | + //Return as DataSet |
| 28 | + using (DataSet result = excelReader.AsDataSet()) |
| 29 | + { |
| 30 | + //Get all the Tables |
| 31 | + DataTableCollection table = result.Tables; |
| 32 | + |
| 33 | + //Store it in DataTable |
| 34 | + DataTable resultTable = table[tableName]; |
| 35 | + |
| 36 | + excelReader.Close(); |
| 37 | + |
| 38 | + //return |
| 39 | + return resultTable; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public List<Datacollection> DataCol = new List<Datacollection>(); |
| 44 | + |
| 45 | + public void PopulateInCollection(string fileName, string tableName) |
| 46 | + { |
| 47 | + DataTable table = new Library().ExcelToDataTable(fileName, tableName); |
| 48 | + |
| 49 | + //Iterate through the rows and columns of the Table |
| 50 | + for (int row = 1; row <= table.Rows.Count; row++) |
| 51 | + { |
| 52 | + for (int col = 0; col < table.Columns.Count; col++) |
| 53 | + { |
| 54 | + var dtTable = new Datacollection |
| 55 | + { |
| 56 | + RowNumber = row, |
| 57 | + ColName = table.Columns[col].ColumnName, |
| 58 | + ColValue = table.Rows[row - 1][col].ToString() |
| 59 | + }; |
| 60 | + //Add all the details for each row |
| 61 | + DataCol.Add(dtTable); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public string Data(int rowNumber, string columnName) |
| 67 | + { |
| 68 | + try |
| 69 | + { |
| 70 | + //Retriving Data using LINQ to reduce iterations |
| 71 | + string data = (from colData in DataCol |
| 72 | + where colData.ColName == columnName && colData.RowNumber == rowNumber |
| 73 | + select colData.ColValue).SingleOrDefault(); |
| 74 | + |
| 75 | + return data; |
| 76 | + } |
| 77 | + catch (Exception) |
| 78 | + { |
| 79 | + return null; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public void OpenAndSave(string fileName) |
| 84 | + { |
| 85 | + var lastOpened = File.GetLastWriteTime(fileName).Date; |
| 86 | + |
| 87 | + Debug.WriteLine("File: " + fileName); |
| 88 | + |
| 89 | + if (lastOpened != DateTime.Today) |
| 90 | + { |
| 91 | + var src = new FileInfo(fileName); |
| 92 | + |
| 93 | + using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(src.FullName, true)) |
| 94 | + { |
| 95 | + var calculationProperties = spreadSheet.WorkbookPart.Workbook.CalculationProperties; |
| 96 | + calculationProperties.ForceFullCalculation = true; |
| 97 | + calculationProperties.FullCalculationOnLoad = true; |
| 98 | + } |
| 99 | + |
| 100 | + //Use Excel automation to open and save the workbook, thereby running the calculation engine. |
| 101 | + var app = new Ex.Application(); |
| 102 | + //string execPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); |
| 103 | + Ex.Workbook book = app.Workbooks.Open(src.FullName); |
| 104 | + book.Save(); |
| 105 | + book.Close(); |
| 106 | + app.Quit(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public string[] GetTable(string testClass) |
| 111 | + { |
| 112 | + string[] tableQuery = new string[2]; |
| 113 | + |
| 114 | + var table = XDocument.Load("../../../Tables.xml").Descendants("Table"); |
| 115 | + |
| 116 | + Debug.WriteLine("TestClass: " + testClass); |
| 117 | + Debug.WriteLine("Table: " + table); |
| 118 | + |
| 119 | + var xElements = table as IList<XElement> ?? table.ToList(); |
| 120 | + var stateQuery = xElements |
| 121 | + .Where(t => (t.Element("TestClass")?.Value) == testClass) |
| 122 | + .Select(t => t.Element("State")?.Value).Single(); |
| 123 | + |
| 124 | + var sheetQuery = xElements |
| 125 | + .Where(t => (t.Element("TestClass")?.Value) == testClass) |
| 126 | + .Select(t => t.Element("Sheet")?.Value).Single(); |
| 127 | + |
| 128 | + tableQuery[0] = stateQuery; |
| 129 | + tableQuery[1] = sheetQuery; |
| 130 | + |
| 131 | + return tableQuery; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public class Datacollection |
| 136 | + { |
| 137 | + public int RowNumber { get; set; } |
| 138 | + public string ColName { get; set; } |
| 139 | + public string ColValue { get; set; } |
| 140 | + } |
| 141 | +} |
0 commit comments