private void FastExcelReadDemo(FileInfo inputFile) { Console.WriteLine(); Console.WriteLine("DEMO READ 1"); Stopwatch stopwatch = Stopwatch.StartNew(); // Open excel file using read only is much faster, but you cannot perfrom any writes using (FastExcel.FastExcel fastExcel = new FastExcel.FastExcel(inputFile, true)) { Console.WriteLine("Reading data (Read Only Access) still needs enumerating..."); Worksheet worksheet = fastExcel.Read("sheet1", 1); } Console.WriteLine(string.Format("Reading data took {0} seconds", stopwatch.Elapsed.TotalSeconds)); }
public List <WindData> ReadFromExcel(string path) { // Get the input file paths FileInfo inputFile = new FileInfo(path); //Create a worksheet FastExcel.Worksheet worksheet = null; // Create an instance of Fast Excel using (FastExcel.FastExcel fastExcel = new FastExcel.FastExcel(inputFile, true)) { // Read the rows using worksheet name worksheet = fastExcel.Read("Sheet1"); worksheet = fastExcel.Read(1); list = worksheet.Rows.ToArray().ToList(); foreach (var item in list) { windData = new WindData(); foreach (var cellItem in item.Cells) { if (item.RowNumber != 1) { switch (cellItem.ColumnNumber) { case 1: windData.year = cellItem.Value.ToString(); break; case 2: windData.month = cellItem.Value.ToString(); break; case 3: windData.day = cellItem.Value.ToString(); break; case 4: windData.timeUTC = cellItem.Value.ToString(); break; case 5: windData.windSpeed = cellItem.Value.ToString(); break; default: break; } } } if (item.RowNumber != 1) { listData.Add(FixDirection(windData)); } } } return(listData); }
/// <summary> /// Loads, parses and validates redirects /// </summary> public void Load() { inputFile = new FileInfo(FileName); Worksheet worksheet = null; using (FastExcel.FastExcel fastExcel = new FastExcel.FastExcel(inputFile, true)) { File = fastExcel.Read(1); Redirects = File.Rows.Select(Parse).ToList(); } Validate(); }
private void FastExcelReadDemo2(FileInfo inputFile) { Console.WriteLine(); Console.WriteLine("DEMO READ 2"); Stopwatch stopwatch = Stopwatch.StartNew(); // Open excel file using read/write is slower, but you can also perform writes using (FastExcel.FastExcel fastExcel = new FastExcel.FastExcel(inputFile, false)) { Console.WriteLine("Reading data (Read/Write Access) still needs enumerating..."); Worksheet worksheet = fastExcel.Read("sheet1", 1); } Console.WriteLine(string.Format("Reading data took {0} seconds", stopwatch.Elapsed.TotalSeconds)); }
private void FastExcelReadDemo(FileInfo inputFile) { Console.WriteLine(); Console.WriteLine("DEMO READ 1"); Stopwatch stopwatch = Stopwatch.StartNew(); // Open excel file using read only is much faster, but you cannot perfrom any writes using (FastExcel.FastExcel fastExcel = new FastExcel.FastExcel(inputFile, true)) { Console.WriteLine("Reading data (Read Only Access) still needs enumerating..."); Worksheet worksheet = fastExcel.Read("sheet1", 1); } Console.WriteLine(string.Format("Reading data took {0} seconds", stopwatch.Elapsed.TotalSeconds)); }
private void readPlanillasExcelFile() { // Get the input file paths FileInfo inputFile = new FileInfo(@"D:\SGH_Sistemas\ShiolExcelService\ShiolWinSvc\FastExcel\FastExcelDemo\bin\Debug\SHIOL 1-31 FINAL.XLSX"); //Create a worksheet Worksheet worksheet = null; // Create an instance of Fast Excel using (FastExcel.FastExcel fastExcel = new FastExcel.FastExcel(inputFile, true)) { // Read the rows using worksheet name worksheet = fastExcel.Read("RH"); List <Field> fields = new List <Field>(); Boolean header = false; foreach (Row row in worksheet.Rows) { if (!header) { int index = 0; foreach (Cell cell in row.Cells) { fields.Add(new Field(cell.Value.ToString(), index)); } header = true; continue; } foreach (Cell cell in row.Cells) { Console.WriteLine(fields[cell.ColumnNumber - 1].Name + " -- " + cell.Value.ToString() + " -- " + cell.GetType()); } Console.ReadKey(); } // Read the rows using the worksheet index // Worksheet indexes are start at 1 not 0 // This method is slightly faster to find the underlying file (so slight you probably wouldn't notice) //worksheet = fastExcel.Read(1); } }
private void FastExcelReadDemo2(FileInfo inputFile) { Console.WriteLine(); Console.WriteLine("DEMO READ 2"); Stopwatch stopwatch = Stopwatch.StartNew(); // Open excel file using read/write is slower, but you can also perform writes using (FastExcel.FastExcel fastExcel = new FastExcel.FastExcel(inputFile, false)) { Console.WriteLine("Reading data (Read/Write Access) still needs enumerating..."); Worksheet worksheet = fastExcel.Read("sheet1", 1); } Console.WriteLine(string.Format("Reading data took {0} seconds", stopwatch.Elapsed.TotalSeconds)); }
/// <summary> /// Read Data From Excel File /// </summary> protected void ReadData() { FileInfo inputFile = new FileInfo(this.FileFullPath); // Test if File Exit if (!inputFile.Exists) { throw new GwinFileNotExistException("The file " + this.FileFullPath + " not exist"); } using (FastExcel.FastExcel fastExcel = new FastExcel.FastExcel(inputFile, true)) { Console.WriteLine("Reading data (Read Only Access) still needs enumerating..."); Worksheet worksheet = fastExcel.Read(1, 1); // Cleat List of Entities ListEntities.Clear(); // Read Rows worksheet.Read(); Row[] rows = worksheet.Rows.ToArray(); // Read PropertyInfo PropertyInfo[] Properties = typeof(T).GetProperties(); // Create Entity for each Row for (int i = 1; i < rows.Count(); i++) { Row row = rows[i]; // Read Values, object[] values = new object[Properties.Count()]; foreach (Cell cell in row.Cells) { // Excel Cell with empty Data Not exist in Cells if (cell.ColumnNumber <= Properties.Count()) { values[cell.ColumnNumber - 1] = cell.Value; } } // Create Entity Instance T Entity = Activator.CreateInstance <T>(); // Write Value in Entity int index_column = 0; foreach (PropertyInfo item in Properties) { object value = values[index_column]; try { if (value != null) { // Fix DateTime convert from Excel to C# if (item.PropertyType == typeof(DateTime)) { value = FromExcelSerialDate(Convert.ToInt32(value)); } item.SetValue(Entity, Convert.ChangeType(value, item.PropertyType)); } } catch (Exception e) { string message = "Can not read the value " + value + "as " + item.PropertyType; Entity.AddError(BaseEntity.CategoryError.ExcelFileError, message); } index_column++; } ListEntities.Add(Entity); } } }