Search in sources :

Example 1 with CellExtra

use of com.alibaba.excel.metadata.CellExtra in project easyexcel by alibaba.

the class HyperlinkRecordHandler method processRecord.

@Override
public void processRecord(XlsReadContext xlsReadContext, Record record) {
    HyperlinkRecord hr = (HyperlinkRecord) record;
    CellExtra cellExtra = new CellExtra(CellExtraTypeEnum.HYPERLINK, hr.getAddress(), hr.getFirstRow(), hr.getLastRow(), hr.getFirstColumn(), hr.getLastColumn());
    xlsReadContext.xlsReadSheetHolder().setCellExtra(cellExtra);
    xlsReadContext.analysisEventProcessor().extra(xlsReadContext);
}
Also used : HyperlinkRecord(org.apache.poi.hssf.record.HyperlinkRecord) CellExtra(com.alibaba.excel.metadata.CellExtra)

Example 2 with CellExtra

use of com.alibaba.excel.metadata.CellExtra in project easyexcel by alibaba.

the class MergeCellTagHandler method startElement.

@Override
public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) {
    String ref = attributes.getValue(ExcelXmlConstants.ATTRIBUTE_REF);
    if (StringUtils.isEmpty(ref)) {
        return;
    }
    CellExtra cellExtra = new CellExtra(CellExtraTypeEnum.MERGE, null, ref);
    xlsxReadContext.readSheetHolder().setCellExtra(cellExtra);
    xlsxReadContext.analysisEventProcessor().extra(xlsxReadContext);
}
Also used : CellExtra(com.alibaba.excel.metadata.CellExtra)

Example 3 with CellExtra

use of com.alibaba.excel.metadata.CellExtra in project pancm_project by xuwujing.

the class EasyExcelTest method simpleRead.

/**
 * 最简单的读
 * <p>
 * 1. 创建excel对应的实体对象 参照{@link DemoData}
 * <p>
 * 2. 由于默认一行行的读取excel,所以需要创建excel一行一行的回调监听器,参照{@link DemoDataListener}
 * <p>
 * 3. 直接读即可
 */
@Test
public void simpleRead() {
    // 写法1:JDK8+ ,不用额外写一个DemoDataListener
    // since: 3.0.0-beta1
    String fileName = "/home" + "demo" + File.separator + "demo.xlsx";
    // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
    // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
    // EasyExcel.read(fileName, DemoData.class, new PageReadListener<DemoData>(dataList -> {
    // for (DemoData demoData : dataList) {
    // log.info("读取到一条数据{}", JSON.toJSONString(demoData));
    // }
    // })).sheet().doRead();
    // 写法2:
    // 匿名内部类 不用额外写一个DemoDataListener
    fileName = "/home" + "demo" + File.separator + "demo.xlsx";
    // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
    EasyExcel.read(fileName, DemoData.class, new ReadListener<DemoData>() {

        /**
         * 单次缓存的数据量
         */
        public static final int BATCH_COUNT = 100;

        /**
         *临时存储
         */
        private List<DemoData> cachedDataList = new ArrayList<>();

        /**
         * All listeners receive this method when any one Listener does an error report. If an exception is thrown here, the
         * entire read will terminate.
         *
         * @param exception
         * @param context
         * @throws Exception
         */
        @Override
        public void onException(Exception exception, AnalysisContext context) throws Exception {
        }

        /**
         * When analysis one head row trigger invoke function.
         *
         * @param headMap
         * @param context
         */
        @Override
        public void invokeHead(Map<Integer, CellData> headMap, AnalysisContext context) {
        }

        @Override
        public void invoke(DemoData data, AnalysisContext context) {
            cachedDataList.add(data);
            if (cachedDataList.size() >= BATCH_COUNT) {
                saveData();
                // 存储完成清理 list
                cachedDataList = new ArrayList<>();
            }
        }

        /**
         * The current method is called when extra information is returned
         *
         * @param extra   extra information
         * @param context
         */
        @Override
        public void extra(CellExtra extra, AnalysisContext context) {
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext context) {
            saveData();
        }

        /**
         * Verify that there is another piece of data.You can stop the read by returning false
         *
         * @param context
         * @return
         */
        @Override
        public boolean hasNext(AnalysisContext context) {
            return false;
        }

        /**
         * 加上存储数据库
         */
        private void saveData() {
            log.info("{}条数据,开始存储数据库!", cachedDataList.size());
            log.info("存储数据库成功!");
        }
    }).sheet().doRead();
    // 有个很重要的点 DemoDataListener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去
    // 写法3:
    fileName = "/home" + "demo" + File.separator + "demo.xlsx";
    // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
    EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead();
    // 写法4:
    fileName = "/home" + "demo" + File.separator + "demo.xlsx";
    // 一个文件一个reader
    ExcelReader excelReader = null;
    try {
        excelReader = EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).build();
        // 构建一个sheet 这里可以指定名字或者no
        ReadSheet readSheet = EasyExcel.readSheet(0).build();
        // 读取一个sheet
        excelReader.read(readSheet);
    } finally {
        if (excelReader != null) {
            // 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的
            excelReader.finish();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) AnalysisContext(com.alibaba.excel.context.AnalysisContext) ReadSheet(com.alibaba.excel.read.metadata.ReadSheet) CellData(com.alibaba.excel.metadata.CellData) ExcelReader(com.alibaba.excel.ExcelReader) CellExtra(com.alibaba.excel.metadata.CellExtra) Test(org.junit.Test)

Example 4 with CellExtra

use of com.alibaba.excel.metadata.CellExtra in project easyexcel by alibaba.

the class NoteRecordHandler method processRecord.

@Override
public void processRecord(XlsReadContext xlsReadContext, Record record) {
    NoteRecord nr = (NoteRecord) record;
    String text = xlsReadContext.xlsReadSheetHolder().getObjectCacheMap().get(nr.getShapeId());
    CellExtra cellExtra = new CellExtra(CellExtraTypeEnum.COMMENT, text, nr.getRow(), nr.getColumn());
    xlsReadContext.xlsReadSheetHolder().setCellExtra(cellExtra);
    xlsReadContext.analysisEventProcessor().extra(xlsReadContext);
}
Also used : NoteRecord(org.apache.poi.hssf.record.NoteRecord) CellExtra(com.alibaba.excel.metadata.CellExtra)

Example 5 with CellExtra

use of com.alibaba.excel.metadata.CellExtra in project easyexcel by alibaba.

the class MergeCellsRecordHandler method processRecord.

@Override
public void processRecord(XlsReadContext xlsReadContext, Record record) {
    MergeCellsRecord mcr = (MergeCellsRecord) record;
    for (int i = 0; i < mcr.getNumAreas(); i++) {
        CellRangeAddress cellRangeAddress = mcr.getAreaAt(i);
        CellExtra cellExtra = new CellExtra(CellExtraTypeEnum.MERGE, null, cellRangeAddress.getFirstRow(), cellRangeAddress.getLastRow(), cellRangeAddress.getFirstColumn(), cellRangeAddress.getLastColumn());
        xlsReadContext.xlsReadSheetHolder().setCellExtra(cellExtra);
        xlsReadContext.analysisEventProcessor().extra(xlsReadContext);
    }
}
Also used : MergeCellsRecord(org.apache.poi.hssf.record.MergeCellsRecord) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) CellExtra(com.alibaba.excel.metadata.CellExtra)

Aggregations

CellExtra (com.alibaba.excel.metadata.CellExtra)7 ExcelReader (com.alibaba.excel.ExcelReader)1 AnalysisContext (com.alibaba.excel.context.AnalysisContext)1 CellData (com.alibaba.excel.metadata.CellData)1 ReadSheet (com.alibaba.excel.read.metadata.ReadSheet)1 ArrayList (java.util.ArrayList)1 HyperlinkRecord (org.apache.poi.hssf.record.HyperlinkRecord)1 MergeCellsRecord (org.apache.poi.hssf.record.MergeCellsRecord)1 NoteRecord (org.apache.poi.hssf.record.NoteRecord)1 CellAddress (org.apache.poi.ss.util.CellAddress)1 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)1 CommentsTable (org.apache.poi.xssf.model.CommentsTable)1 XSSFComment (org.apache.poi.xssf.usermodel.XSSFComment)1 Test (org.junit.Test)1