use of org.apache.poi.hssf.record.LabelRecord in project hutool by looly.
the class Excel03SaxReader method processCellValue.
// ---------------------------------------------------------------------------------------------- Private method start
/**
* 处理单元格值
*
* @param record 单元格
*/
private void processCellValue(Record record) {
Object value = null;
switch(record.getSid()) {
case BlankRecord.sid:
// 空白记录
BlankRecord brec = (BlankRecord) record;
rowCellList.add(brec.getColumn(), StrUtil.EMPTY);
break;
case // 单元格为布尔类型
BoolErrRecord.sid:
BoolErrRecord berec = (BoolErrRecord) record;
rowCellList.add(berec.getColumn(), berec.getBooleanValue());
break;
case // 单元格为公式类型
FormulaRecord.sid:
FormulaRecord frec = (FormulaRecord) record;
if (isOutputFormulaValues) {
if (Double.isNaN(frec.getValue())) {
// Formula result is a string
// This is stored in the next record
isOutputNextStringRecord = true;
} else {
value = formatListener.formatNumberDateCell(frec);
}
} else {
value = '"' + HSSFFormulaParser.toFormulaString(stubWorkbook, frec.getParsedExpression()) + '"';
}
rowCellList.add(frec.getColumn(), value);
break;
case // 单元格中公式的字符串
StringRecord.sid:
if (isOutputNextStringRecord) {
// String for formula
StringRecord srec = (StringRecord) record;
value = srec.getString();
isOutputNextStringRecord = false;
}
break;
case LabelRecord.sid:
LabelRecord lrec = (LabelRecord) record;
this.rowCellList.add(lrec.getColumn(), value);
break;
case // 单元格为字符串类型
LabelSSTRecord.sid:
LabelSSTRecord lsrec = (LabelSSTRecord) record;
if (sstRecord == null) {
rowCellList.add(lsrec.getColumn(), StrUtil.EMPTY);
} else {
value = sstRecord.getString(lsrec.getSSTIndex()).toString();
rowCellList.add(lsrec.getColumn(), value);
}
break;
case // 单元格为数字类型
NumberRecord.sid:
NumberRecord numrec = (NumberRecord) record;
final String formatString = formatListener.getFormatString(numrec);
if (formatString.contains(StrUtil.DOT)) {
// 浮点数
value = numrec.getValue();
} else if (formatString.contains(StrUtil.SLASH) || formatString.contains(StrUtil.COLON)) {
// 日期
value = formatListener.formatNumberDateCell(numrec);
} else {
// 整型
value = (long) numrec.getValue();
}
// 向容器加入列值
rowCellList.add(numrec.getColumn(), value);
break;
default:
break;
}
}
use of org.apache.poi.hssf.record.LabelRecord 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.record.LabelRecord 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.record.LabelRecord in project poi by apache.
the class HSSFWorkbook method convertLabelRecords.
/**
* This is basically a kludge to deal with the now obsolete Label records. If
* you have to read in a sheet that contains Label records, be aware that the rest
* of the API doesn't deal with them, the low level structure only provides read-only
* semi-immutable structures (the sets are there for interface conformance with NO
* Implementation). In short, you need to call this function passing it a reference
* to the Workbook object. All labels will be converted to LabelSST records and their
* contained strings will be written to the Shared String table (SSTRecord) within
* the Workbook.
*
* @param records a collection of sheet's records.
* @param offset the offset to search at
* @see org.apache.poi.hssf.record.LabelRecord
* @see org.apache.poi.hssf.record.LabelSSTRecord
* @see org.apache.poi.hssf.record.SSTRecord
*/
private void convertLabelRecords(List<Record> records, int offset) {
if (log.check(POILogger.DEBUG)) {
log.log(POILogger.DEBUG, "convertLabelRecords called");
}
for (int k = offset; k < records.size(); k++) {
Record rec = records.get(k);
if (rec.getSid() == LabelRecord.sid) {
LabelRecord oldrec = (LabelRecord) rec;
records.remove(k);
LabelSSTRecord newrec = new LabelSSTRecord();
int stringid = workbook.addSSTString(new UnicodeString(oldrec.getValue()));
newrec.setRow(oldrec.getRow());
newrec.setColumn(oldrec.getColumn());
newrec.setXFIndex(oldrec.getXFIndex());
newrec.setSSTIndex(stringid);
records.add(k, newrec);
}
}
if (log.check(POILogger.DEBUG)) {
log.log(POILogger.DEBUG, "convertLabelRecords exit");
}
}
Aggregations