Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/4.0.0.rc3 #350

Merged
merged 17 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.github.liaochong</groupId>
<artifactId>myexcel</artifactId>
<version>4.0.0.RC2</version>
<version>4.0.0.RC3</version>
<packaging>jar</packaging>

<name>myexcel</name>
Expand All @@ -22,7 +22,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<poi.version>5.1.0</poi.version>
<poi.version>5.2.0</poi.version>
<jsoup.version>1.14.3</jsoup.version>
<lombok.version>1.18.22</lombok.version>
<beetl.version>3.9.0.RELEASE</beetl.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2019 liaochong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.liaochong.myexcel.core;

import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder;
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* 抽象HSSF读取处理器
*
* @author liaochong
* @version 1.0
*/
abstract class AbstractHSSFReadHandler implements HSSFListener {

protected BoundSheetRecord[] orderedBSRs;

protected final List<BoundSheetRecord> boundSheetRecords = new ArrayList<>();

protected int sheetIndex = -1;

protected String sheetName;

protected SaxExcelReader.ReadConfig<?> readConfig;

protected POIFSFileSystem fs;

protected void process() throws IOException {
HSSFRequest request = new HSSFRequest();
request.addListenerForAllRecords(new EventWorkbookBuilder.SheetRecordCollectingListener(this));
new HSSFEventFactory().processWorkbookEvents(request, fs);
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/github/liaochong/myexcel/core/CiConsumer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.liaochong.myexcel.core;

/**
* @author liaochong
* @version 1.0
*/
@FunctionalInterface
interface CiConsumer<T, F, U> {

void accept(T t, F f, U u);
}
11 changes: 11 additions & 0 deletions src/main/java/com/github/liaochong/myexcel/core/CiFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.liaochong.myexcel.core;

/**
* @author liaochong
* @version 1.0
*/
@FunctionalInterface
interface CiFunction<T, F, R, U> {

U apply(T t, F f, R r);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2019 liaochong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.liaochong.myexcel.core;

import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.MergeCellsRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.util.CellAddress;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
* @author liaochong
* @version 1.0
*/
class HSSFMergeReadHandler extends AbstractHSSFReadHandler {

private final Map<Integer, Map<CellAddress, CellAddress>> mergeCellIndexMapping;

public HSSFMergeReadHandler(File file,
SaxExcelReader.ReadConfig<?> readConfig,
Map<Integer, Map<CellAddress, CellAddress>> mergeCellIndexMapping) throws IOException {
this.readConfig = readConfig;
this.mergeCellIndexMapping = mergeCellIndexMapping;
this.fs = new POIFSFileSystem(new FileInputStream(file));
}


@Override
public void processRecord(Record record) {
switch (record.getSid()) {
case BoundSheetRecord.sid:
boundSheetRecords.add((BoundSheetRecord) record);
break;
case BOFRecord.sid:
BOFRecord br = (BOFRecord) record;
if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
sheetIndex++;
if (orderedBSRs == null) {
orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
}
sheetName = orderedBSRs[sheetIndex].getSheetname();
}
break;
case MergeCellsRecord.sid:
if (!isSelectedSheet()) {
return;
}
MergeCellsRecord mergeCellsRecord = (MergeCellsRecord) record;
int numAreas = mergeCellsRecord.getNumAreas();
Map<CellAddress, CellAddress> mergeCellMapping = new HashMap<>();
for (int i = 0; i < numAreas; i++) {
Iterator<CellAddress> iterator = mergeCellsRecord.getAreaAt(i).iterator();
CellAddress firstCellAddress = null;
while (iterator.hasNext()) {
CellAddress cellAddress = iterator.next();
if (firstCellAddress == null) {
firstCellAddress = cellAddress;
} else {
mergeCellMapping.put(cellAddress, firstCellAddress);
}
}
}
mergeCellIndexMapping.put(sheetIndex, mergeCellMapping);
break;
default:
break;
}
}

private boolean isSelectedSheet() {
if (readConfig.readAllSheet) {
return true;
}
if (!readConfig.sheetNames.isEmpty()) {
return readConfig.sheetNames.contains(sheetName);
}
return readConfig.sheetIndexs.contains(sheetIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
*/
package com.github.liaochong.myexcel.core;

import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder;
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener;
import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord;
import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.BlankRecord;
Expand All @@ -32,13 +26,11 @@
import org.apache.poi.hssf.record.NumberRecord;
import org.apache.poi.hssf.record.RKRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -47,42 +39,20 @@
* @author liaochong
* @version 1.0
*/
public class HSSFMetaDataSaxReadHandler implements HSSFListener {

private BoundSheetRecord[] orderedBSRs;

private final List<BoundSheetRecord> boundSheetRecords = new ArrayList<>();

private final POIFSFileSystem fs;
class HSSFMetaDataSaxReadHandler extends AbstractHSSFReadHandler {

private int lastRowNumber = -1;

/**
* For parsing Formulas
*/
private EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener;
private HSSFWorkbook stubWorkbook;

/**
* So we known which sheet we're on
*/
private int sheetIndex = -1;

private final WorkbookMetaData workbookMetaData;

public HSSFMetaDataSaxReadHandler(File file, WorkbookMetaData workbookMetaData) throws IOException {
this.fs = new POIFSFileSystem(new FileInputStream(file));
this.workbookMetaData = workbookMetaData;
}

@Override
public void process() throws IOException {
MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
FormatTrackingHSSFListener formatListener = new FormatTrackingHSSFListener(listener);

HSSFRequest request = new HSSFRequest();
workbookBuildingListener = new EventWorkbookBuilder.SheetRecordCollectingListener(formatListener);
request.addListenerForAllRecords(workbookBuildingListener);
new HSSFEventFactory().processWorkbookEvents(request, fs);
super.process();
// 处理最后一个sheet
if (lastRowNumber > -1) {
workbookMetaData.getSheetMetaDataList().get(sheetIndex).setLastRowNum(lastRowNumber + 1);
Expand All @@ -99,9 +69,6 @@ public void processRecord(Record record) {
case BOFRecord.sid:
BOFRecord br = (BOFRecord) record;
if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
if (workbookBuildingListener != null && stubWorkbook == null) {
stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
}
List<SheetMetaData> sheetMetaDataList = workbookMetaData.getSheetMetaDataList();
if (lastRowNumber > -1) {
sheetMetaDataList.get(sheetIndex).setLastRowNum(lastRowNumber + 1);
Expand All @@ -118,46 +85,36 @@ public void processRecord(Record record) {
}
break;
case BlankRecord.sid:
BlankRecord brec = (BlankRecord) record;
thisRow = brec.getRow();
thisRow = ((BlankRecord) record).getRow();
break;
case BoolErrRecord.sid:
BoolErrRecord berec = (BoolErrRecord) record;
thisRow = berec.getRow();
thisRow = ((BoolErrRecord) record).getRow();
break;
case FormulaRecord.sid:
FormulaRecord frec = (FormulaRecord) record;
thisRow = frec.getRow();
thisRow = ((FormulaRecord) record).getRow();
break;
case LabelRecord.sid:
LabelRecord lrec = (LabelRecord) record;
thisRow = lrec.getRow();
thisRow = ((LabelRecord) record).getRow();
break;
case LabelSSTRecord.sid:
LabelSSTRecord lsrec = (LabelSSTRecord) record;
thisRow = lsrec.getRow();
thisRow = ((LabelSSTRecord) record).getRow();
break;
case NoteRecord.sid:
NoteRecord nrec = (NoteRecord) record;
thisRow = nrec.getRow();
thisRow = ((NoteRecord) record).getRow();
break;
case NumberRecord.sid:
NumberRecord numrec = (NumberRecord) record;
thisRow = numrec.getRow();
thisRow = ((NumberRecord) record).getRow();
break;
case RKRecord.sid:
RKRecord rkrec = (RKRecord) record;
thisRow = rkrec.getRow();
thisRow = ((RKRecord) record).getRow();
break;
default:
break;
}

if (record instanceof LastCellOfRowDummyRecord) {
LastCellOfRowDummyRecord lc = (LastCellOfRowDummyRecord) record;
thisRow = lc.getRow();
}

// Handle new row
if (thisRow != -1 && thisRow != lastRowNumber) {
lastRowNumber = thisRow;
Expand Down
Loading