public string GetElement(object col, int row) { ExcelFileException.ThrowIfStringNull((string)col); if ((int)col < 0 | row < 0) { throw new ExcelFileException($"Столбцы могут быть только положительные."); } string res = null; switch (col.GetType().ToString()) { case "System.String": { if (IsExistColumn((string)col)) { res = data.Rows[row][(string)col].ToString(); } break; } case "System.Int32": { res = data.Rows[row][(int)col].ToString(); break; } default: throw new Exception($"Форма колонки не правильно задан."); } return(res); }
public ExcelFile(string fullpath) { FileInfo file = new FileInfo(fullpath); ExcelFileException.ThrowIfFileNotExsist(fullpath, file); extension = file.Extension; this.fullpath = file.FullName; filename = file.Name; State = Status.Initialized; }
public bool IsExistSheet(string name) { int i = 0; ExcelFileException.ThrowIfStringNull(name); foreach (string sheet in sheetnames.Split(';')) { if (sheet == name) { i++; } } return(i > 0 ? true : false); }
public bool IsExistColumn(string name) { int i = 0; ExcelFileException.ThrowIfStringNull(name); foreach (string colname in columns.Split(';')) { if (colname == name) { i++; } } return(i > 0 ? true : false); }
public static DataTable ReadData(ExcelFile excel, string sheetname) { sheetname += "$"; ExcelFileException.ThrowIfSheetNotExist(excel, sheetname); var res = new DataTable(); using (var connection = new OleDbConnection(excel.ConnectionStr)) { connection.Open(); if (connection.State == ConnectionState.Open) { var cmd = new OleDbCommand() { Connection = connection, CommandText = $"SELECT *FROM [{sheetname}]" }; OleDbDataReader reader = cmd.ExecuteReader(); res.Load(reader); reader.Close(); connection.Close(); } } ReadDataFinished?.Invoke(null, new ReadDataEventArgs(sheetname, excel.FileName, res)); return(res); }