/// <summary> /// Generate a row based on the input data /// </summary> /// <param name="sheet"></param> /// <param name="rowId"></param> /// <param name="data"></param> public static void GenerateRow(ISheet sheet, int rowId, RowData data) { ICell tmpCell; IRow tmpRow = sheet.CreateRow(rowId); // For every string data piece - create a corresponding cell foreach (KeyValuePair <int, string> stringCell in data.stringDict) { tmpCell = tmpRow.CreateCell(stringCell.Key); tmpCell.SetCellType(CellType.String); tmpCell.SetCellValue(stringCell.Value); } // For every numeric data piece - create a corresponding cell foreach (KeyValuePair <int, long> numericCell in data.numericDict) { tmpCell = tmpRow.CreateCell(numericCell.Key); tmpCell.SetCellType(CellType.Numeric); tmpCell.SetCellValue(numericCell.Value); } }
/// <summary> /// /// </summary> /// <param name="sheet"></param> /// <param name="rowId"></param> /// <param name="data"></param> public static void GenerateRowV2(ISheet sheet, int rowId, RowData data) { ICell tmpCell; IRow tmpRow = sheet.CreateRow(rowId); //*********NEEDS TO USE THE LIST OF CellData and CellCoordinates Dictionary <int, string> stringCells = data.stringDict; Dictionary <int, long> numericCells = data.numericDict; for (int i = 0; i < data.numericDict.Count; i++) { tmpCell = tmpRow.CreateCell(i); tmpCell.SetCellType(CellType.String); tmpCell.SetCellValue(stringCells[i]); } for (int i = 0; i < data.numericDict.Count; i++) { tmpCell = tmpRow.CreateCell(i); tmpCell.SetCellType(CellType.Numeric); tmpCell.SetCellValue(numericCells[i]); } }
public static RowData[] ReadWorkBook(string path, CellMap[] maps) { try { FileStream file = File.OpenRead(path); } catch (Exception) { Console.WriteLine("File is open, you must close it."); return(null); } // list of all data to keep List <RowData> dataList = new List <RowData>(); // row data RowData rowData; IRow tmpRow; IWorkbook workbook = new XSSFWorkbook(path); ISheet sheet = workbook.GetSheetAt(0); // for every row (contact) in the sheet for (int i = sheet.FirstRowNum; i < sheet.LastRowNum + 1; i++) { rowData = new RowData(); // temporary row handler tmpRow = sheet.GetRow(i); ICell cell; if (tmpRow == null) { Console.WriteLine("Row is null"); continue; } // for every mapping, add the data for this row for (int j = 0; j < maps.Length; j++) { cell = tmpRow.GetCell(maps[j].ImportedCellId); if (cell == null) { Console.WriteLine("Cell is null"); continue; } switch (cell.CellType) { case CellType.String: rowData.AddString(maps[j].ConversionCellId, tmpRow.GetCell(maps[j].ImportedCellId).StringCellValue); Console.WriteLine("Value: String"); break; case CellType.Numeric: rowData.AddNumber(maps[j].ConversionCellId, tmpRow.GetCell(maps[j].ImportedCellId).NumericCellValue); Console.WriteLine("Value: Numeric"); break; default: rowData.AddString(maps[j].ConversionCellId, "Exception"); Console.WriteLine("Value: Exception"); break; } } dataList.Add(rowData); } return(dataList.ToArray()); }