private int findFirstDataCellOffset(int startOffset) { XlsBiffRecord record = this.m_stream.ReadAt(startOffset); while (!(record is XlsBiffDbCell)) { if (this.m_stream.Position >= this.m_stream.Size) { return(-1); } if (record is XlsBiffEOF) { return(-1); } record = this.m_stream.Read(); } XlsBiffDbCell cell = (XlsBiffDbCell)record; XlsBiffRow row = null; int rowAddress = cell.RowAddress; do { row = this.m_stream.ReadAt(rowAddress) as XlsBiffRow; if (row == null) { return(rowAddress); } rowAddress += row.Size; }while (row != null); return(rowAddress); }
private int findFirstDataCellOffset(int startOffset) { //seek to the first dbcell record var record = m_stream.ReadAt(startOffset); while (!(record is XlsBiffDbCell)) { if (m_stream.Position >= m_stream.Size) { return(-1); } if (record is XlsBiffEOF) { return(-1); } try { record = m_stream.Read(); } catch { return(-2); } } XlsBiffDbCell startCell = (XlsBiffDbCell)record; XlsBiffRow row = null; int offs = startCell.RowAddress; do { row = m_stream.ReadAt(offs) as XlsBiffRow; if (row == null) { break; } offs += row.Size; } while (null != row); return(offs); }
private int findFirstDataCellOffset(int startOffset) { XlsBiffDbCell startCell = (XlsBiffDbCell)m_stream.ReadAt(startOffset); XlsBiffRow row = null; int offs = startCell.RowAddress; do { row = m_stream.ReadAt(offs) as XlsBiffRow; if (row == null) { break; } offs += row.Size; } while (null != row); return(offs); }
/// <summary> /// private method, reads sheet data /// </summary> /// <param name="sheet">Sheet object, whose data to read</param> /// <returns>True if sheet was read successfully, otherwise False</returns> private bool ReadWorksheet(XlsWorksheet sheet) { m_stream.Seek((int)sheet.DataOffset, SeekOrigin.Begin); XlsBiffBOF bof = m_stream.Read() as XlsBiffBOF; if (bof == null || bof.Type != BIFFTYPE.Worksheet) { return(false); } XlsBiffIndex idx = m_stream.Read() as XlsBiffIndex; bool isV8 = (m_version >= 0x600); if (idx != null) { idx.IsV8 = isV8; DataTable dt = new DataTable(sheet.Name); XlsBiffRecord trec; XlsBiffDimensions dims = null; do { trec = m_stream.Read(); if (trec.ID == BIFFRECORDTYPE.DIMENSIONS) { dims = (XlsBiffDimensions)trec; break; } }while (trec.ID != BIFFRECORDTYPE.ROW); int maxCol = 256; if (dims != null) { dims.IsV8 = isV8; maxCol = dims.LastColumn; sheet.Dimensions = dims; } for (int i = 0; i < maxCol; i++) { dt.Columns.Add("Column" + (i + 1).ToString(), typeof(string)); } sheet.Data = dt; uint maxRow = idx.LastExistingRow; if (idx.LastExistingRow <= idx.FirstExistingRow) { return(true); } dt.BeginLoadData(); for (int i = 0; i <= maxRow; i++) { dt.Rows.Add(dt.NewRow()); } uint[] dbCellAddrs = idx.DbCellAddresses; for (int i = 0; i < dbCellAddrs.Length; i++) { XlsBiffDbCell dbCell = (XlsBiffDbCell)m_stream.ReadAt((int)dbCellAddrs[i]); XlsBiffRow row = null; int offs = (int)dbCell.RowAddress; do { row = m_stream.ReadAt(offs) as XlsBiffRow; if (row == null) { break; } offs += row.Size; }while (row != null); while (true) { XlsBiffRecord rec = m_stream.ReadAt(offs); offs += rec.Size; if (rec is XlsBiffDbCell) { break; } if (rec is XlsBiffEOF) { break; } XlsBiffBlankCell cell = rec as XlsBiffBlankCell; if (cell == null) { continue; } if (cell.ColumnIndex >= maxCol) { continue; } if (cell.RowIndex > maxRow) { continue; } switch (cell.ID) { case BIFFRECORDTYPE.INTEGER: case BIFFRECORDTYPE.INTEGER_OLD: dt.Rows[cell.RowIndex][cell.ColumnIndex] = ((XlsBiffIntegerCell)cell).Value.ToString(); break; case BIFFRECORDTYPE.NUMBER: case BIFFRECORDTYPE.NUMBER_OLD: dt.Rows[cell.RowIndex][cell.ColumnIndex] = FormatNumber(((XlsBiffNumberCell)cell).Value); break; case BIFFRECORDTYPE.LABEL: case BIFFRECORDTYPE.LABEL_OLD: case BIFFRECORDTYPE.RSTRING: dt.Rows[cell.RowIndex][cell.ColumnIndex] = ((XlsBiffLabelCell)cell).Value; break; case BIFFRECORDTYPE.LABELSST: { string tmp = m_globals.SST.GetString(((XlsBiffLabelSSTCell)cell).SSTIndex); dt.Rows[cell.RowIndex][cell.ColumnIndex] = tmp; } break; case BIFFRECORDTYPE.RK: dt.Rows[cell.RowIndex][cell.ColumnIndex] = FormatNumber(((XlsBiffRKCell)cell).Value); break; case BIFFRECORDTYPE.MULRK: for (ushort j = cell.ColumnIndex; j <= ((XlsBiffMulRKCell)cell).LastColumnIndex; j++) { dt.Rows[cell.RowIndex][j] = FormatNumber(((XlsBiffMulRKCell)cell).GetValue(j)); } break; case BIFFRECORDTYPE.BLANK: case BIFFRECORDTYPE.BLANK_OLD: case BIFFRECORDTYPE.MULBLANK: // Skip blank cells break; case BIFFRECORDTYPE.FORMULA: case BIFFRECORDTYPE.FORMULA_OLD: ((XlsBiffFormulaCell)cell).UseEncoding = m_encoding; object val = ((XlsBiffFormulaCell)cell).Value; if (val == null) { val = string.Empty; } else if (val is FORMULAERROR) { val = "#" + ((FORMULAERROR)val).ToString(); } else if (val is double) { val = FormatNumber((double)val); } dt.Rows[cell.RowIndex][cell.ColumnIndex] = val.ToString(); break; default: break; } } } dt.EndLoadData(); } else { return(false); } return(true); }
private bool ReadWorksheet(XlsWorksheet sheet) { m_stream.Seek((int)sheet.DataOffset, SeekOrigin.Begin); XlsBiffBOF bof = m_stream.Read() as XlsBiffBOF; if (bof == null || bof.Type != BIFFTYPE.Worksheet) { return(false); } XlsBiffIndex idx = m_stream.Read() as XlsBiffIndex; if (null == idx) { return(false); } idx.IsV8 = IsV8(); DataTable dt = new DataTable(sheet.Name); XlsBiffRecord trec; XlsBiffDimensions dims = null; do { trec = m_stream.Read(); if (trec.ID == BIFFRECORDTYPE.DIMENSIONS) { dims = (XlsBiffDimensions)trec; break; } } while (trec != null && trec.ID != BIFFRECORDTYPE.ROW); int maxCol = 256; if (dims != null) { dims.IsV8 = IsV8(); maxCol = dims.LastColumn - 1; sheet.Dimensions = dims; } InitializeColumns(ref dt, maxCol); sheet.Data = dt; uint maxRow = idx.LastExistingRow; if (idx.LastExistingRow <= idx.FirstExistingRow) { return(true); } dt.BeginLoadData(); for (int i = 0; i < maxRow; i++) { dt.Rows.Add(dt.NewRow()); } uint[] dbCellAddrs = idx.DbCellAddresses; for (int i = 0; i < dbCellAddrs.Length; i++) { XlsBiffDbCell dbCell = (XlsBiffDbCell)m_stream.ReadAt((int)dbCellAddrs[i]); XlsBiffRow row = null; int offs = dbCell.RowAddress; do { row = m_stream.ReadAt(offs) as XlsBiffRow; if (row == null) { break; } offs += row.Size; } while (null != row); while (true) { XlsBiffRecord rec = m_stream.ReadAt(offs); offs += rec.Size; if (rec is XlsBiffDbCell) { break; } if (rec is XlsBiffEOF) { break; } XlsBiffBlankCell cell = rec as XlsBiffBlankCell; if (cell == null) { continue; } if (cell.ColumnIndex >= maxCol) { continue; } if (cell.RowIndex > maxRow) { continue; } string _sValue; double _dValue; switch (cell.ID) { case BIFFRECORDTYPE.INTEGER: case BIFFRECORDTYPE.INTEGER_OLD: dt.Rows[cell.RowIndex][cell.ColumnIndex] = ((XlsBiffIntegerCell)cell).Value.ToString(); break; case BIFFRECORDTYPE.NUMBER: case BIFFRECORDTYPE.NUMBER_OLD: _dValue = ((XlsBiffNumberCell)cell).Value; if ((_sValue = TryConvertOADate(_dValue, cell.XFormat)) != null) { dt.Rows[cell.RowIndex][cell.ColumnIndex] = _sValue; } else { dt.Rows[cell.RowIndex][cell.ColumnIndex] = _dValue; } break; case BIFFRECORDTYPE.LABEL: case BIFFRECORDTYPE.LABEL_OLD: case BIFFRECORDTYPE.RSTRING: dt.Rows[cell.RowIndex][cell.ColumnIndex] = ((XlsBiffLabelCell)cell).Value; break; case BIFFRECORDTYPE.LABELSST: string tmp = m_globals.SST.GetString(((XlsBiffLabelSSTCell)cell).SSTIndex); dt.Rows[cell.RowIndex][cell.ColumnIndex] = tmp; break; case BIFFRECORDTYPE.RK: _dValue = ((XlsBiffRKCell)cell).Value; if ((_sValue = TryConvertOADate(_dValue, cell.XFormat)) != null) { dt.Rows[cell.RowIndex][cell.ColumnIndex] = _sValue; } else { dt.Rows[cell.RowIndex][cell.ColumnIndex] = _dValue; } break; case BIFFRECORDTYPE.MULRK: XlsBiffMulRKCell _rkCell = (XlsBiffMulRKCell)cell; for (ushort j = cell.ColumnIndex; j <= _rkCell.LastColumnIndex; j++) { dt.Rows[cell.RowIndex][j] = _rkCell.GetValue(j); } break; case BIFFRECORDTYPE.BLANK: case BIFFRECORDTYPE.BLANK_OLD: case BIFFRECORDTYPE.MULBLANK: // Skip blank cells break; case BIFFRECORDTYPE.FORMULA: case BIFFRECORDTYPE.FORMULA_OLD: object _oValue = ((XlsBiffFormulaCell)cell).Value; if (null != _oValue && _oValue is FORMULAERROR) { _oValue = null; } if (null != _oValue && (_sValue = TryConvertOADate(_oValue, cell.XFormat)) != null) { dt.Rows[cell.RowIndex][cell.ColumnIndex] = _sValue; } else { dt.Rows[cell.RowIndex][cell.ColumnIndex] = _oValue; } break; default: break; } } } dt.EndLoadData(); if (m_PromoteToColumns) { RemapColumnsNames(ref dt, dt.Rows[0].ItemArray); dt.Rows.RemoveAt(0); dt.AcceptChanges(); } return(true); }