/// <summary> /// Sets the cell type. The SetValue flag indicates whether to bother about /// trying to preserve the current value in the new record if one is Created. /// The SetCellValue method will call this method with false in SetValue /// since it will overWrite the cell value later /// </summary> /// <param name="cellType">Type of the cell.</param> /// <param name="setValue">if set to <c>true</c> [set value].</param> /// <param name="row">The row.</param> /// <param name="col">The col.</param> /// <param name="styleIndex">Index of the style.</param> private void SetCellType(NPOI.SS.UserModel.CellType cellType, bool setValue, int row, int col, short styleIndex) { if (cellType > NPOI.SS.UserModel.CellType.ERROR) { throw new Exception("I have no idea what type that Is!"); } switch (cellType) { case NPOI.SS.UserModel.CellType.FORMULA: FormulaRecordAggregate frec = null; if (cellType != this.cellType) { frec = sheet.Sheet.RowsAggregate.CreateFormula(row, col); } else { frec = (FormulaRecordAggregate)record; } frec.Column = col; if (setValue) { frec.FormulaRecord.Value = NumericCellValue; } frec.XFIndex = styleIndex; frec.Row = row; record = frec; break; case NPOI.SS.UserModel.CellType.NUMERIC: NumberRecord nrec = null; if (cellType != this.cellType) { nrec = new NumberRecord(); } else { nrec = (NumberRecord)record; } nrec.Column = col; if (setValue) { nrec.Value = NumericCellValue; } nrec.XFIndex = styleIndex; nrec.Row = row; record = nrec; break; case NPOI.SS.UserModel.CellType.STRING: LabelSSTRecord lrec = null; if (cellType != this.cellType) { lrec = new LabelSSTRecord(); } else { lrec = (LabelSSTRecord)record; } lrec.Column = col; lrec.Row = row; lrec.XFIndex = styleIndex; if (setValue) { String str = ConvertCellValueToString(); int sstIndex = book.Workbook.AddSSTString(new UnicodeString(str)); lrec.SSTIndex = (sstIndex); UnicodeString us = book.Workbook.GetSSTString(sstIndex); stringValue = new HSSFRichTextString(); stringValue.UnicodeString = us; } record = lrec; break; case NPOI.SS.UserModel.CellType.BLANK: BlankRecord brec = null; if (cellType != this.cellType) { brec = new BlankRecord(); } else { brec = (BlankRecord)record; } brec.Column = col; // During construction the cellStyle may be null for a Blank cell. brec.XFIndex = styleIndex; brec.Row = row; record = brec; break; case NPOI.SS.UserModel.CellType.BOOLEAN: BoolErrRecord boolRec = null; if (cellType != this.cellType) { boolRec = new BoolErrRecord(); } else { boolRec = (BoolErrRecord)record; } boolRec.Column = col; if (setValue) { boolRec.SetValue(ConvertCellValueToBoolean()); } boolRec.XFIndex = styleIndex; boolRec.Row = row; record = boolRec; break; case NPOI.SS.UserModel.CellType.ERROR: BoolErrRecord errRec = null; if (cellType != this.cellType) { errRec = new BoolErrRecord(); } else { errRec = (BoolErrRecord)record; } errRec.Column = col; if (setValue) { errRec.SetValue((byte)HSSFErrorConstants.ERROR_VALUE); } errRec.XFIndex = styleIndex; errRec.Row = row; record = errRec; break; } if (cellType != this.cellType && this.cellType != NPOI.SS.UserModel.CellType.Unknown) // Special Value to indicate an Uninitialized Cell { sheet.Sheet.ReplaceValueRecord(record); } this.cellType = cellType; }
public object GetObject(int rowIndex, int columnIndex) { if (rowIndex < 0 || (rowIndex + 1) > this.maxRow || columnIndex < 0 || (columnIndex + 1) > this.maxCol) { return(null); } HSSFCell cell = GetCell(rowIndex, columnIndex); if (cell == null) { return(null); } NPOI.SS.UserModel.CellType c_type = cell.CellType; switch (c_type) { case NPOI.SS.UserModel.CellType.Boolean: return(cell.BooleanCellValue); case NPOI.SS.UserModel.CellType.Error: return(cell.ErrorCellValue); case NPOI.SS.UserModel.CellType.Formula: return(cell.CellFormula); case NPOI.SS.UserModel.CellType.Numeric: return(cell.NumericCellValue); } return(cell.StringCellValue); }
private NPOI.SS.UserModel.CellType GetColumnCellType(Type type) { NPOI.SS.UserModel.CellType cellType = NPOI.SS.UserModel.CellType.String; switch (type.Name.ToLowerInvariant()) { case "string": cellType = NPOI.SS.UserModel.CellType.String; break; case "datetime": cellType = NPOI.SS.UserModel.CellType.Numeric; break; case "int": case "int32": case "int64": case "decimal": case "long": case "double": cellType = NPOI.SS.UserModel.CellType.Numeric; break; case "boolean": case "bool": cellType = NPOI.SS.UserModel.CellType.Boolean; break; } return(cellType); }
/// <summary> /// Creates new Cell - Should only be called by HSSFRow. This Creates a cell /// from scratch. /// </summary> /// <param name="book">Workbook record of the workbook containing this cell</param> /// <param name="sheet">Sheet record of the sheet containing this cell</param> /// <param name="row">the row of this cell</param> /// <param name="col">the column for this cell</param> /// <param name="type">NPOI.SS.UserModel.CellType.NUMERIC, NPOI.SS.UserModel.CellType.STRING, NPOI.SS.UserModel.CellType.FORMULA, NPOI.SS.UserModel.CellType.BLANK, /// NPOI.SS.UserModel.CellType.BOOLEAN, NPOI.SS.UserModel.CellType.ERROR</param> public HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col, NPOI.SS.UserModel.CellType type) { CheckBounds(col); cellType = NPOI.SS.UserModel.CellType.Unknown; // Force 'SetCellType' to Create a first Record stringValue = null; this.book = book; this.sheet = sheet; short xfindex = sheet.Sheet.GetXFIndexForColAt(col); SetCellType(type, false, row, col, xfindex); }
/// <summary> /// Creates an Cell from a CellValueRecordInterface. HSSFSheet uses this when /// Reading in cells from an existing sheet. /// </summary> /// <param name="book">Workbook record of the workbook containing this cell</param> /// <param name="sheet">Sheet record of the sheet containing this cell</param> /// <param name="row">The row.</param> /// <param name="cval">the Cell Value Record we wish to represent</param> public HSSFCell(HSSFWorkbook book, HSSFSheet sheet, CellValueRecordInterface cval) { record = cval; cellType = DetermineType(cval); stringValue = null; this.book = book; this.sheet = sheet; switch (cellType) { case NPOI.SS.UserModel.CellType.STRING: stringValue = new HSSFRichTextString(book.Workbook, (LabelSSTRecord)cval); break; case NPOI.SS.UserModel.CellType.BLANK: break; case NPOI.SS.UserModel.CellType.FORMULA: stringValue = new HSSFRichTextString(((FormulaRecordAggregate)cval).StringValue); break; } ExtendedFormatRecord xf = book.Workbook.GetExFormatAt(cval.XFIndex); CellStyle = new HSSFCellStyle((short)cval.XFIndex, xf, book); }