/// <summary> /// Gets the file csv. /// </summary> /// <param name="pathToFile">The path to file.</param> /// <returns>The csv file in rows file format</returns> public RowsFile GetFile(string pathToFile) { if (string.IsNullOrWhiteSpace(pathToFile)) { throw new ArgumentNullException("pathToFile is null"); } if (File.Exists(pathToFile) == false) { return(new RowsFile()); } RowsFile rowsFile = new RowsFile(); using (var reader = new StreamReader(File.OpenRead(pathToFile))) { bool isHeader = true; while (reader.EndOfStream == false) { string line = reader.ReadLine(); string[] values = line.Split(';'); if (isHeader) { rowsFile.Headers.ColumnList = values.ToList(); } else { rowsFile.Rows.Add(new Row() { ColumnList = values.ToList() }); } isHeader = false; } } return(rowsFile); }
/// <summary> /// Gets the file xls. /// </summary> /// <param name="pathToFile">The path to file.</param> /// <returns>The xls file in rows file format</returns> public RowsFile GetFile(string pathToFile) { if (string.IsNullOrWhiteSpace(pathToFile)) { throw new ArgumentNullException("pathToFile is null"); } if (File.Exists(pathToFile) == false) { return(new RowsFile()); } RowsFile rowsFile = new RowsFile(); int rowIndex; int columnIndex; int rowsCount = 0; int columnsCount = 0; Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(Path.Combine(Environment.CurrentDirectory, pathToFile), Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, "\t", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.Item[1]; Excel.Range range = xlWorkSheet.UsedRange; rowsCount = range.Rows.Count; columnsCount = range.Columns.Count; for (rowIndex = 1; rowIndex <= rowsCount; rowIndex++) { List <string> list = new List <string>(); for (columnIndex = 1; columnIndex <= columnsCount; columnIndex++) { list.Add(range.Cells[rowIndex, columnIndex].Value2.ToString()); } if (rowsFile.Headers.ColumnList.Any() == false) { rowsFile.Headers.ColumnList = list; } else { rowsFile.Rows.Add(new Row() { ColumnList = list }); } } xlWorkBook.Close(true, null, null); xlApp.Quit(); Marshal.ReleaseComObject(xlWorkSheet); Marshal.ReleaseComObject(xlWorkBook); Marshal.ReleaseComObject(xlApp); return(rowsFile); }