Search in sources :

Example 1 with XlsxReadSheetHolder

use of com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder in project easyexcel by alibaba.

the class CellFormulaTagHandler method endElement.

@Override
public void endElement(XlsxReadContext xlsxReadContext, String name) {
    XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
    FormulaData formulaData = new FormulaData();
    formulaData.setFormulaValue(xlsxReadSheetHolder.getTempFormula().toString());
    xlsxReadSheetHolder.getTempCellData().setFormulaData(formulaData);
}
Also used : XlsxReadSheetHolder(com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder) FormulaData(com.alibaba.excel.metadata.data.FormulaData)

Example 2 with XlsxReadSheetHolder

use of com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder in project easyexcel by alibaba.

the class RowTagHandler method endElement.

@Override
public void endElement(XlsxReadContext xlsxReadContext, String name) {
    XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
    RowTypeEnum rowType = MapUtils.isEmpty(xlsxReadSheetHolder.getCellMap()) ? RowTypeEnum.EMPTY : RowTypeEnum.DATA;
    // It's possible that all of the cells in the row are empty
    if (rowType == RowTypeEnum.DATA) {
        boolean hasData = false;
        for (Cell cell : xlsxReadSheetHolder.getCellMap().values()) {
            if (!(cell instanceof ReadCellData)) {
                hasData = true;
                break;
            }
            ReadCellData<?> readCellData = (ReadCellData<?>) cell;
            if (readCellData.getType() != CellDataTypeEnum.EMPTY) {
                hasData = true;
                break;
            }
        }
        if (!hasData) {
            rowType = RowTypeEnum.EMPTY;
        }
    }
    xlsxReadContext.readRowHolder(new ReadRowHolder(xlsxReadSheetHolder.getRowIndex(), rowType, xlsxReadSheetHolder.getGlobalConfiguration(), xlsxReadSheetHolder.getCellMap()));
    xlsxReadContext.analysisEventProcessor().endRow(xlsxReadContext);
    xlsxReadSheetHolder.setColumnIndex(null);
    xlsxReadSheetHolder.setCellMap(new LinkedHashMap<>());
}
Also used : ReadCellData(com.alibaba.excel.metadata.data.ReadCellData) XlsxReadSheetHolder(com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder) ReadRowHolder(com.alibaba.excel.read.metadata.holder.ReadRowHolder) RowTypeEnum(com.alibaba.excel.enums.RowTypeEnum) Cell(com.alibaba.excel.metadata.Cell)

Example 3 with XlsxReadSheetHolder

use of com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder in project easyexcel by alibaba.

the class AnalysisContextImpl method currentSheet.

@Override
public void currentSheet(ReadSheet readSheet) {
    switch(readWorkbookHolder.getExcelType()) {
        case XLS:
            readSheetHolder = new XlsReadSheetHolder(readSheet, readWorkbookHolder);
            break;
        case XLSX:
            readSheetHolder = new XlsxReadSheetHolder(readSheet, readWorkbookHolder);
            break;
        case CSV:
            readSheetHolder = new CsvReadSheetHolder(readSheet, readWorkbookHolder);
            break;
        default:
            break;
    }
    currentReadHolder = readSheetHolder;
    if (readWorkbookHolder.getHasReadSheet().contains(readSheetHolder.getSheetNo())) {
        throw new ExcelAnalysisException("Cannot read sheet repeatedly.");
    }
    readWorkbookHolder.getHasReadSheet().add(readSheetHolder.getSheetNo());
    if (log.isDebugEnabled()) {
        log.debug("Began to read:{}", readSheetHolder);
    }
}
Also used : XlsxReadSheetHolder(com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder) CsvReadSheetHolder(com.alibaba.excel.read.metadata.holder.csv.CsvReadSheetHolder) XlsReadSheetHolder(com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Example 4 with XlsxReadSheetHolder

use of com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder in project easyexcel by alibaba.

the class CellFormulaTagHandler method startElement.

@Override
public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) {
    XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
    xlsxReadSheetHolder.setTempFormula(new StringBuilder());
}
Also used : XlsxReadSheetHolder(com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder)

Example 5 with XlsxReadSheetHolder

use of com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder in project easyexcel by alibaba.

the class CellTagHandler method endElement.

@Override
public void endElement(XlsxReadContext xlsxReadContext, String name) {
    XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
    ReadCellData<?> tempCellData = xlsxReadSheetHolder.getTempCellData();
    StringBuilder tempData = xlsxReadSheetHolder.getTempData();
    String tempDataString = tempData.toString();
    CellDataTypeEnum oldType = tempCellData.getType();
    switch(oldType) {
        case STRING:
            // In some cases, although cell type is a string, it may be an empty tag
            if (StringUtils.isEmpty(tempDataString)) {
                break;
            }
            String stringValue = xlsxReadContext.readWorkbookHolder().getReadCache().get(Integer.valueOf(tempDataString));
            tempCellData.setStringValue(stringValue);
            break;
        case DIRECT_STRING:
        case ERROR:
            tempCellData.setStringValue(tempDataString);
            tempCellData.setType(CellDataTypeEnum.STRING);
            break;
        case BOOLEAN:
            if (StringUtils.isEmpty(tempDataString)) {
                tempCellData.setType(CellDataTypeEnum.EMPTY);
                break;
            }
            tempCellData.setBooleanValue(BooleanUtils.valueOf(tempData.toString()));
            break;
        case NUMBER:
        case EMPTY:
            if (StringUtils.isEmpty(tempDataString)) {
                tempCellData.setType(CellDataTypeEnum.EMPTY);
                break;
            }
            tempCellData.setType(CellDataTypeEnum.NUMBER);
            tempCellData.setNumberValue(BigDecimal.valueOf(Double.parseDouble(tempDataString)));
            break;
        default:
            throw new IllegalStateException("Cannot set values now");
    }
    if (tempCellData.getStringValue() != null && xlsxReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
        tempCellData.setStringValue(tempCellData.getStringValue().trim());
    }
    tempCellData.checkEmpty();
    tempCellData.setRowIndex(xlsxReadSheetHolder.getRowIndex());
    tempCellData.setColumnIndex(xlsxReadSheetHolder.getColumnIndex());
    xlsxReadSheetHolder.getCellMap().put(xlsxReadSheetHolder.getColumnIndex(), tempCellData);
}
Also used : CellDataTypeEnum(com.alibaba.excel.enums.CellDataTypeEnum) XlsxReadSheetHolder(com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder)

Aggregations

XlsxReadSheetHolder (com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder)7 CellDataTypeEnum (com.alibaba.excel.enums.CellDataTypeEnum)2 Cell (com.alibaba.excel.metadata.Cell)2 ReadRowHolder (com.alibaba.excel.read.metadata.holder.ReadRowHolder)2 RowTypeEnum (com.alibaba.excel.enums.RowTypeEnum)1 ExcelAnalysisException (com.alibaba.excel.exception.ExcelAnalysisException)1 FormulaData (com.alibaba.excel.metadata.data.FormulaData)1 ReadCellData (com.alibaba.excel.metadata.data.ReadCellData)1 CsvReadSheetHolder (com.alibaba.excel.read.metadata.holder.csv.CsvReadSheetHolder)1 XlsReadSheetHolder (com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder)1 LinkedHashMap (java.util.LinkedHashMap)1