Jiaju Zhuang
5 years ago
20 changed files with 354 additions and 43 deletions
@ -0,0 +1,60 @@
|
||||
package com.alibaba.excel.analysis.v03; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
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.record.BoundSheetRecord; |
||||
import org.apache.poi.hssf.record.Record; |
||||
import org.apache.poi.poifs.filesystem.POIFSFileSystem; |
||||
|
||||
import com.alibaba.excel.analysis.v03.handlers.BofRecordHandler; |
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.exception.ExcelAnalysisException; |
||||
import com.alibaba.excel.read.metadata.ReadSheet; |
||||
|
||||
/** |
||||
* In some cases, you need to know the number of sheets in advance and only read the file once in advance. |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class XlsListSheetListener implements HSSFListener { |
||||
private POIFSFileSystem poifsFileSystem; |
||||
private List<ReadSheet> sheetList; |
||||
private XlsRecordHandler bofRecordHandler; |
||||
|
||||
public XlsListSheetListener(AnalysisContext analysisContext, POIFSFileSystem poifsFileSystem) { |
||||
this.poifsFileSystem = poifsFileSystem; |
||||
sheetList = new ArrayList<ReadSheet>(); |
||||
bofRecordHandler = new BofRecordHandler(analysisContext, sheetList, false); |
||||
bofRecordHandler.init(); |
||||
} |
||||
|
||||
@Override |
||||
public void processRecord(Record record) { |
||||
bofRecordHandler.processRecord(record); |
||||
} |
||||
|
||||
public List<ReadSheet> getSheetList() { |
||||
MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this); |
||||
HSSFListener formatListener = new FormatTrackingHSSFListener(listener); |
||||
HSSFEventFactory factory = new HSSFEventFactory(); |
||||
HSSFRequest request = new HSSFRequest(); |
||||
EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener = |
||||
new EventWorkbookBuilder.SheetRecordCollectingListener(formatListener); |
||||
request.addListenerForAllRecords(workbookBuildingListener); |
||||
|
||||
try { |
||||
factory.processWorkbookEvents(request, poifsFileSystem); |
||||
} catch (IOException e) { |
||||
throw new ExcelAnalysisException(e); |
||||
} |
||||
return sheetList; |
||||
} |
||||
} |
@ -0,0 +1,11 @@
|
||||
package com.alibaba.easyexcel.test.core.multiplesheets; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Data |
||||
public class MultipleSheetsData { |
||||
private String title; |
||||
} |
@ -0,0 +1,56 @@
|
||||
package com.alibaba.easyexcel.test.core.multiplesheets; |
||||
|
||||
import java.io.File; |
||||
import java.util.List; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.BeforeClass; |
||||
import org.junit.FixMethodOrder; |
||||
import org.junit.Test; |
||||
import org.junit.runners.MethodSorters; |
||||
|
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcel; |
||||
import com.alibaba.excel.ExcelReader; |
||||
import com.alibaba.excel.read.metadata.ReadSheet; |
||||
|
||||
/** |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
||||
public class MultipleSheetsDataTest { |
||||
|
||||
private static File file07; |
||||
private static File file03; |
||||
|
||||
@BeforeClass |
||||
public static void init() { |
||||
file07 = TestFileUtil.readFile("multiplesheets" + File.separator + "multiplesheets.xlsx"); |
||||
file03 = TestFileUtil.readFile("multiplesheets" + File.separator + "multiplesheets.xls"); |
||||
} |
||||
|
||||
@Test |
||||
public void t01Read07() { |
||||
read(file07); |
||||
} |
||||
|
||||
@Test |
||||
public void t020Read03() { |
||||
read(file03); |
||||
} |
||||
|
||||
private void read(File file) { |
||||
MultipleSheetsListener multipleSheetsListener = new MultipleSheetsListener(); |
||||
ExcelReader excelReader = EasyExcel.read(file, MultipleSheetsData.class, multipleSheetsListener).build(); |
||||
List<ReadSheet> sheets = excelReader.excelExecutor().sheetList(); |
||||
int count = 1; |
||||
for (ReadSheet readSheet : sheets) { |
||||
excelReader.read(readSheet); |
||||
Assert.assertEquals((long)multipleSheetsListener.getList().size(), (long)count); |
||||
count++; |
||||
} |
||||
excelReader.finish(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,41 @@
|
||||
package com.alibaba.easyexcel.test.core.multiplesheets; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.Assert; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.easyexcel.test.core.parameter.ParameterData; |
||||
import com.alibaba.easyexcel.test.core.parameter.ParameterDataListener; |
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class MultipleSheetsListener extends AnalysisEventListener<MultipleSheetsData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(MultipleSheetsListener.class); |
||||
List<MultipleSheetsData> list = new ArrayList<MultipleSheetsData>(); |
||||
|
||||
@Override |
||||
public void invoke(MultipleSheetsData data, AnalysisContext context) { |
||||
list.add(data); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
Assert.assertEquals(list.get(0).getTitle(), "表1数据"); |
||||
LOGGER.debug("All row:{}", JSON.toJSONString(list)); |
||||
} |
||||
|
||||
public List<MultipleSheetsData> getList() { |
||||
return list; |
||||
} |
||||
|
||||
public void setList(List<MultipleSheetsData> list) { |
||||
this.list = list; |
||||
} |
||||
} |
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue