use of org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord in project poi by apache.
the class TestMissingRecordAwareHSSFListener method testStringRecordHandling.
public void testStringRecordHandling() {
readRecords("53588.xls");
Record[] rr = r;
int missingCount = 0;
int lastCount = 0;
for (Record record : rr) {
if (record instanceof MissingCellDummyRecord) {
missingCount++;
}
if (record instanceof LastCellOfRowDummyRecord) {
lastCount++;
}
}
assertEquals(1, missingCount);
assertEquals(1, lastCount);
}
use of org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord in project jeesuite-libs by vakinge.
the class XLS2CSV method processRecord.
/**
* Main HSSFListener method, processes events, and outputs the
* CSV as the file is processed.
*/
public void processRecord(Record record) {
// 超过10行空白就不处理了
if (blankRowNum == 10)
return;
int thisRow = -1;
int thisColumn = -1;
String thisStr = null;
switch(record.getSid()) {
case BoundSheetRecord.sid:
boundSheetRecords.add((BoundSheetRecord) record);
break;
case BOFRecord.sid:
BOFRecord br = (BOFRecord) record;
if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
// Create sub workbook if required
if (workbookBuildingListener != null && stubWorkbook == null) {
stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
}
// Output the worksheet name
// Works by ordering the BSRs by the location of
// their BOFRecords, and then knowing that we
// process BOFRecords in byte offset order
sheetIndex++;
if (orderedBSRs == null) {
orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
}
// sheetName
// System.out.println(orderedBSRs[sheetIndex].getSheetname() + " [" + (sheetIndex+1) + "]:" );
String sheetname = orderedBSRs[sheetIndex].getSheetname();
results.add(ExcelValidator.SHEET_NAME_PREFIX + sheetname);
}
break;
case SSTRecord.sid:
sstRecord = (SSTRecord) record;
break;
case BlankRecord.sid:
BlankRecord brec = (BlankRecord) record;
thisRow = brec.getRow();
thisColumn = brec.getColumn();
thisStr = "";
break;
case BoolErrRecord.sid:
BoolErrRecord berec = (BoolErrRecord) record;
thisRow = berec.getRow();
thisColumn = berec.getColumn();
thisStr = "";
break;
case FormulaRecord.sid:
FormulaRecord frec = (FormulaRecord) record;
thisRow = frec.getRow();
thisColumn = frec.getColumn();
if (outputFormulaValues) {
if (Double.isNaN(frec.getValue())) {
// Formula result is a string
// This is stored in the next record
outputNextStringRecord = true;
nextRow = frec.getRow();
nextColumn = frec.getColumn();
} else {
thisStr = formatListener.formatNumberDateCell(frec);
}
} else {
thisStr = HSSFFormulaParser.toFormulaString(stubWorkbook, frec.getParsedExpression());
}
break;
case StringRecord.sid:
if (outputNextStringRecord) {
// String for formula
StringRecord srec = (StringRecord) record;
thisStr = srec.getString();
thisRow = nextRow;
thisColumn = nextColumn;
outputNextStringRecord = false;
}
break;
case LabelRecord.sid:
LabelRecord lrec = (LabelRecord) record;
thisRow = lrec.getRow();
thisColumn = lrec.getColumn();
thisStr = lrec.getValue();
break;
case LabelSSTRecord.sid:
LabelSSTRecord lsrec = (LabelSSTRecord) record;
thisRow = lsrec.getRow();
thisColumn = lsrec.getColumn();
if (sstRecord == null) {
thisStr = "(No SST Record, can't identify string)";
} else {
thisStr = sstRecord.getString(lsrec.getSSTIndex()).toString();
}
break;
case NoteRecord.sid:
NoteRecord nrec = (NoteRecord) record;
thisRow = nrec.getRow();
thisColumn = nrec.getColumn();
// TODO: Find object to match nrec.getShapeId()
thisStr = "(TODO)";
break;
case NumberRecord.sid:
NumberRecord numrec = (NumberRecord) record;
thisRow = numrec.getRow();
thisColumn = numrec.getColumn();
// Format
thisStr = formatListener.formatNumberDateCell(numrec);
break;
case RKRecord.sid:
RKRecord rkrec = (RKRecord) record;
thisRow = rkrec.getRow();
thisColumn = rkrec.getColumn();
thisStr = "(TODO)";
break;
default:
break;
}
// Handle new row
if (thisRow != -1 && thisRow != lastRowNumber) {
lastColumnNumber = -1;
}
// Handle missing column
if (record instanceof MissingCellDummyRecord) {
MissingCellDummyRecord mc = (MissingCellDummyRecord) record;
thisRow = mc.getRow();
thisColumn = mc.getColumn();
thisStr = "";
}
if (thisStr != null) {
if (thisColumn > 0) {
_resultRowTmp.append(ExcelValidator.FIELD_SPLIT);
}
_resultRowTmp.append(thisStr);
}
// Update column and row count
if (thisRow > -1)
lastRowNumber = thisRow;
if (thisColumn > -1)
lastColumnNumber = thisColumn;
// Handle end of row
if (record instanceof LastCellOfRowDummyRecord) {
// Print out any missing commas if needed
if (minColumns > 0) {
// Columns are 0 based
if (lastColumnNumber == -1) {
lastColumnNumber = 0;
}
for (int i = lastColumnNumber; i < (minColumns); i++) {
_resultRowTmp.append(',');
}
}
// We're onto a new row
lastColumnNumber = -1;
// End the row
if (!ExcelValidator.isBlankCSVRow(_resultRowTmp.toString())) {
results.add(_resultRowTmp.toString());
} else {
blankRowNum = blankRowNum + 1;
}
_resultRowTmp.setLength(0);
}
}
use of org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord in project h2o-2 by h2oai.
the class XlsParser method processRecord.
@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) {
Log.warn(Sys.EXCEL, "[ExcelParser] Missing SST record");
} else {
curCol = lsrec.getColumn();
curStr = _str.setTo(_sstRecord.getString(lsrec.getSSTIndex()).toString());
}
break;
case NoteRecord.sid:
Log.warn(Sys.EXCEL, "Warning cell notes are unsupported");
break;
case NumberRecord.sid:
NumberRecord numrec = (NumberRecord) record;
curCol = numrec.getColumn();
curNum = numrec.getValue();
break;
case RKRecord.sid:
Log.warn(Sys.EXCEL, "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);
_dout.setColumnNames(arr);
} else {
_dout.newLine();
curCol = -1;
}
}
if (curCol == -1)
return;
if (_firstRow) {
_columnNames.add(curStr == null ? ("C" + (curCol + 1)) : curStr.toString());
} else {
if (curStr == null)
if (Double.isNaN(curNum))
_dout.addInvalidCol(curCol);
else
_dout.addNumCol(curCol, curNum);
else
_dout.addStrCol(curCol, curStr);
}
}
use of org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord in project poi by apache.
the class XLS2CSVmra method processRecord.
/**
* Main HSSFListener method, processes events, and outputs the
* CSV as the file is processed.
*/
@Override
public void processRecord(Record record) {
int thisRow = -1;
int thisColumn = -1;
String thisStr = null;
switch(record.getSid()) {
case BoundSheetRecord.sid:
boundSheetRecords.add((BoundSheetRecord) record);
break;
case BOFRecord.sid:
BOFRecord br = (BOFRecord) record;
if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
// Create sub workbook if required
if (workbookBuildingListener != null && stubWorkbook == null) {
stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
}
// Output the worksheet name
// Works by ordering the BSRs by the location of
// their BOFRecords, and then knowing that we
// process BOFRecords in byte offset order
sheetIndex++;
if (orderedBSRs == null) {
orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
}
output.println();
output.println(orderedBSRs[sheetIndex].getSheetname() + " [" + (sheetIndex + 1) + "]:");
}
break;
case SSTRecord.sid:
sstRecord = (SSTRecord) record;
break;
case BlankRecord.sid:
BlankRecord brec = (BlankRecord) record;
thisRow = brec.getRow();
thisColumn = brec.getColumn();
thisStr = "";
break;
case BoolErrRecord.sid:
BoolErrRecord berec = (BoolErrRecord) record;
thisRow = berec.getRow();
thisColumn = berec.getColumn();
thisStr = "";
break;
case FormulaRecord.sid:
FormulaRecord frec = (FormulaRecord) record;
thisRow = frec.getRow();
thisColumn = frec.getColumn();
if (outputFormulaValues) {
if (Double.isNaN(frec.getValue())) {
// Formula result is a string
// This is stored in the next record
outputNextStringRecord = true;
nextRow = frec.getRow();
nextColumn = frec.getColumn();
} else {
thisStr = formatListener.formatNumberDateCell(frec);
}
} else {
thisStr = '"' + HSSFFormulaParser.toFormulaString(stubWorkbook, frec.getParsedExpression()) + '"';
}
break;
case StringRecord.sid:
if (outputNextStringRecord) {
// String for formula
StringRecord srec = (StringRecord) record;
thisStr = srec.getString();
thisRow = nextRow;
thisColumn = nextColumn;
outputNextStringRecord = false;
}
break;
case LabelRecord.sid:
LabelRecord lrec = (LabelRecord) record;
thisRow = lrec.getRow();
thisColumn = lrec.getColumn();
thisStr = '"' + lrec.getValue() + '"';
break;
case LabelSSTRecord.sid:
LabelSSTRecord lsrec = (LabelSSTRecord) record;
thisRow = lsrec.getRow();
thisColumn = lsrec.getColumn();
if (sstRecord == null) {
thisStr = '"' + "(No SST Record, can't identify string)" + '"';
} else {
thisStr = '"' + sstRecord.getString(lsrec.getSSTIndex()).toString() + '"';
}
break;
case NoteRecord.sid:
NoteRecord nrec = (NoteRecord) record;
thisRow = nrec.getRow();
thisColumn = nrec.getColumn();
// TODO: Find object to match nrec.getShapeId()
thisStr = '"' + "(TODO)" + '"';
break;
case NumberRecord.sid:
NumberRecord numrec = (NumberRecord) record;
thisRow = numrec.getRow();
thisColumn = numrec.getColumn();
// Format
thisStr = formatListener.formatNumberDateCell(numrec);
break;
case RKRecord.sid:
RKRecord rkrec = (RKRecord) record;
thisRow = rkrec.getRow();
thisColumn = rkrec.getColumn();
thisStr = '"' + "(TODO)" + '"';
break;
default:
break;
}
// Handle new row
if (thisRow != -1 && thisRow != lastRowNumber) {
lastColumnNumber = -1;
}
// Handle missing column
if (record instanceof MissingCellDummyRecord) {
MissingCellDummyRecord mc = (MissingCellDummyRecord) record;
thisRow = mc.getRow();
thisColumn = mc.getColumn();
thisStr = "";
}
// If we got something to print out, do so
if (thisStr != null) {
if (thisColumn > 0) {
output.print(',');
}
output.print(thisStr);
}
// Update column and row count
if (thisRow > -1)
lastRowNumber = thisRow;
if (thisColumn > -1)
lastColumnNumber = thisColumn;
// Handle end of row
if (record instanceof LastCellOfRowDummyRecord) {
// Print out any missing commas if needed
if (minColumns > 0) {
// Columns are 0 based
if (lastColumnNumber == -1) {
lastColumnNumber = 0;
}
for (int i = lastColumnNumber; i < (minColumns); i++) {
output.print(',');
}
}
// We're onto a new row
lastColumnNumber = -1;
// End the row
output.println();
}
}
use of org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord in project poi by apache.
the class MissingRecordAwareHSSFListener method processRecord.
public void processRecord(Record record) {
int thisRow;
int thisColumn;
CellValueRecordInterface[] expandedRecords = null;
if (record instanceof CellValueRecordInterface) {
CellValueRecordInterface valueRec = (CellValueRecordInterface) record;
thisRow = valueRec.getRow();
thisColumn = valueRec.getColumn();
} else {
if (record instanceof StringRecord) {
//it contains only cashed result of the previous FormulaRecord evaluation
childListener.processRecord(record);
return;
}
thisRow = -1;
thisColumn = -1;
switch(record.getSid()) {
// the workbook
case BOFRecord.sid:
BOFRecord bof = (BOFRecord) record;
if (bof.getType() == BOFRecord.TYPE_WORKBOOK || bof.getType() == BOFRecord.TYPE_WORKSHEET) {
// Reset the row and column counts - new workbook / worksheet
resetCounts();
}
break;
case RowRecord.sid:
RowRecord rowrec = (RowRecord) record;
// If there's a jump in rows, fire off missing row records
if (lastRowRow + 1 < rowrec.getRowNumber()) {
for (int i = (lastRowRow + 1); i < rowrec.getRowNumber(); i++) {
MissingRowDummyRecord dr = new MissingRowDummyRecord(i);
childListener.processRecord(dr);
}
}
// Record this as the last row we saw
lastRowRow = rowrec.getRowNumber();
lastCellColumn = -1;
break;
case SharedFormulaRecord.sid:
// SharedFormulaRecord occurs after the first FormulaRecord of the cell range.
// There are probably (but not always) more cell records after this
// - so don't fire off the LastCellOfRowDummyRecord yet
childListener.processRecord(record);
return;
case MulBlankRecord.sid:
// These appear in the middle of the cell records, to
// specify that the next bunch are empty but styled
// Expand this out into multiple blank cells
MulBlankRecord mbr = (MulBlankRecord) record;
expandedRecords = RecordFactory.convertBlankRecords(mbr);
break;
case MulRKRecord.sid:
// This is multiple consecutive number cells in one record
// Exand this out into multiple regular number cells
MulRKRecord mrk = (MulRKRecord) record;
expandedRecords = RecordFactory.convertRKRecords(mrk);
break;
case NoteRecord.sid:
NoteRecord nrec = (NoteRecord) record;
thisRow = nrec.getRow();
thisColumn = nrec.getColumn();
break;
default:
break;
}
}
// First part of expanded record handling
if (expandedRecords != null && expandedRecords.length > 0) {
thisRow = expandedRecords[0].getRow();
thisColumn = expandedRecords[0].getColumn();
}
// dummy end-of-row records
if (thisRow != lastCellRow && thisRow > 0) {
if (lastCellRow == -1)
lastCellRow = 0;
for (int i = lastCellRow; i < thisRow; i++) {
int cols = -1;
if (i == lastCellRow) {
cols = lastCellColumn;
}
childListener.processRecord(new LastCellOfRowDummyRecord(i, cols));
}
}
// final dummy end-of-row record
if (lastCellRow != -1 && lastCellColumn != -1 && thisRow == -1) {
childListener.processRecord(new LastCellOfRowDummyRecord(lastCellRow, lastCellColumn));
lastCellRow = -1;
lastCellColumn = -1;
}
// the column counter
if (thisRow != lastCellRow) {
lastCellColumn = -1;
}
// the dummy cell records
if (lastCellColumn != thisColumn - 1) {
for (int i = lastCellColumn + 1; i < thisColumn; i++) {
childListener.processRecord(new MissingCellDummyRecord(thisRow, i));
}
}
// Next part of expanded record handling
if (expandedRecords != null && expandedRecords.length > 0) {
thisColumn = expandedRecords[expandedRecords.length - 1].getColumn();
}
// Update cell and row counts as needed
if (thisColumn != -1) {
lastCellColumn = thisColumn;
lastCellRow = thisRow;
}
// Pass along the record(s)
if (expandedRecords != null && expandedRecords.length > 0) {
for (CellValueRecordInterface r : expandedRecords) {
childListener.processRecord((Record) r);
}
} else {
childListener.processRecord(record);
}
}
Aggregations