예제 #1
0
    @Override
    public void processRecord(final Record record) {
      switch (record.getSid()) {
        case BOFRecord.sid:
          // 开始解析到workboot sheet 等
          BOFRecord bof = (BOFRecord) record;
          if (bof.getType() == bof.TYPE_WORKBOOK) {
            // workbook
          } else if (bof.getType() == bof.TYPE_WORKSHEET) {
            // sheet
          }
          break;
        case BoundSheetRecord.sid:
          // 开始解析BundleSheet
          BoundSheetRecord bsr = (BoundSheetRecord) record;
          // bsr.getSheetname() 得到sheet name
          break;
        case RowRecord.sid:
          // 开始解析行
          RowRecord rowrec = (RowRecord) record;
          break;
        case NumberRecord.sid:
          // 解析一个Number类型的单元格值
          NumberRecord numrec = (NumberRecord) record;
          // numrec.getRow()  numrec.getColumn()   numrec.getValue()

          // 非第一行 第一列
          if (numrec.getRow() > 0 && numrec.getColumn() == 0) {
            current = new ExcelData();
            current.setId(Double.valueOf(numrec.getValue()).longValue());
          }
          break;
        case SSTRecord.sid:
          // SSTRecords存储了在Excel中使用的所有唯一String的数组
          sstrec = (SSTRecord) record;
          break;
        case LabelSSTRecord.sid:
          // 解析一个String类型的单元格值(存储在SSTRecord)
          LabelSSTRecord lrec = (LabelSSTRecord) record;

          if (lrec.getRow() > 0 && lrec.getColumn() == 1) {
            current.setContent(sstrec.getString(lrec.getSSTIndex()).getString());
            dataList.add(current);

            totalSize++;

            // 最后一个单元格时 判断是否该写了
            if (totalSize % batchSize == 0) {
              doBatchSave(dataList);
              dataList.clear();
            }
          }
          break;
      }
    }
예제 #2
0
파일: XlsParser.java 프로젝트: pwaila/h2o
  @Override
  public void processRecord(Record record) {
    int curCol = -1;
    double curNum = Double.NaN;
    ValueString curStr = null;

    switch (record.getSid()) {
      case BoundSheetRecord.sid:
      case BOFRecord.sid:
        // we just run together multiple sheets
        break;
      case SSTRecord.sid:
        _sstRecord = (SSTRecord) record;
        break;
      case BlankRecord.sid:
        BlankRecord brec = (BlankRecord) record;

        curCol = brec.getColumn();
        curStr = _str.setTo("");
        break;
      case BoolErrRecord.sid:
        BoolErrRecord berec = (BoolErrRecord) record;

        curCol = berec.getColumn();
        curStr = _str.setTo("");
        break;

      case FormulaRecord.sid:
        FormulaRecord frec = (FormulaRecord) record;

        curCol = frec.getColumn();
        curNum = frec.getValue();

        if (Double.isNaN(curNum)) {
          // Formula result is a string
          // This is stored in the next record
          _outputNextStringRecord = true;
          _nextCol = frec.getColumn();
        }
        break;
      case StringRecord.sid:
        if (_outputNextStringRecord) {
          // String for formula
          StringRecord srec = (StringRecord) record;
          curStr = _str.setTo(srec.getString());
          curCol = _nextCol;
          _outputNextStringRecord = false;
        }
        break;
      case LabelRecord.sid:
        LabelRecord lrec = (LabelRecord) record;

        curCol = lrec.getColumn();
        curStr = _str.setTo(lrec.getValue());
        break;
      case LabelSSTRecord.sid:
        LabelSSTRecord lsrec = (LabelSSTRecord) record;
        if (_sstRecord == null) {
          System.err.println("[ExcelParser] Missing SST record");
        } else {
          curCol = lsrec.getColumn();
          curStr = _str.setTo(_sstRecord.getString(lsrec.getSSTIndex()).toString());
        }
        break;
      case NoteRecord.sid:
        System.err.println("[ExcelParser] Warning cell notes are unsupported");
        break;
      case NumberRecord.sid:
        NumberRecord numrec = (NumberRecord) record;
        curCol = numrec.getColumn();
        curNum = numrec.getValue();
        break;
      case RKRecord.sid:
        System.err.println("[ExcelParser] Warning RK records are unsupported");
        break;
      default:
        break;
    }

    // Handle missing column
    if (record instanceof MissingCellDummyRecord) {
      MissingCellDummyRecord mc = (MissingCellDummyRecord) record;
      curCol = mc.getColumn();
      curNum = Double.NaN;
    }

    // Handle end of row
    if (record instanceof LastCellOfRowDummyRecord) {
      if (_firstRow) {
        _firstRow = false;
        String[] arr = new String[_columnNames.size()];
        arr = _columnNames.toArray(arr);
        _callback.setColumnNames(arr);
      }
      _callback.newLine();
    }

    if (curCol == -1) return;

    if (_firstRow) {
      _columnNames.add(curStr == null ? "" : curStr.toString());
    } else {
      if (curStr == null)
        if (Double.isNaN(curNum)) _callback.addInvalidCol(curCol);
        else _callback.addCol(curCol, curNum);
      else _callback.addStrCol(curCol, curStr);
    }
  }