diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteLargeTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteLargeTest.java index d82179ce..846ebdb7 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteLargeTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteLargeTest.java @@ -1,5 +1,8 @@ package com.alibaba.easyexcel.test.temp; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -15,6 +18,18 @@ import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; import lombok.extern.slf4j.Slf4j; +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.BOFRecord; +import org.apache.poi.hssf.record.BoundSheetRecord; +import org.apache.poi.hssf.record.Record; +import org.apache.poi.hssf.record.SSTRecord; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.IndexedColors; import org.junit.jupiter.api.Test; @@ -83,6 +98,76 @@ public class WriteLargeTest { } + @Test + public void read2() throws Exception { + // 使用输入的文件创建一个新的文件输入流 + //FileInputStream fin = new FileInputStream("/Users/zhuangjiaju/Downloads/1e9e0578a9634abbbbd9b67f338f142a + // .xls"); + // 创建一个新的org.apache.poi.poifs.filesystem.Filesystem + POIFSFileSystem poifs = new POIFSFileSystem( + new File("/Users/zhuangjiaju/Downloads/1e9e0578a9634abbbbd9b67f338f142a.xls")); + // 在InputStream中获取Workbook流 + InputStream din = poifs.createDocumentInputStream("Workbook"); + // 构造出HSSFRequest对象 + HSSFRequest req = new HSSFRequest(); + // 注册全部的监听器 + req.addListenerForAllRecords(new EventExample()); + // 创建事件工厂 + HSSFEventFactory factory = new HSSFEventFactory(); + // 根据文档输入流处理我们监听的事件 + factory.processEvents(req, din); + // 关闭文件输入流 + //fin.close(); + // 关闭文档输入流 + din.close(); + System.out.println("读取结束"); + } + + @Test + public void read3() throws Exception { + HSSFWorkbook hwb = new HSSFWorkbook( + new FileInputStream("/Users/zhuangjiaju/Downloads/1e9e0578a9634abbbbd9b67f338f142a.xls")); + HSSFSheet sheet = hwb.getSheetAt(0); + HSSFRow row = null; + HSSFCell cell = null; + for (int i = sheet.getFirstRowNum(); i <= sheet.getPhysicalNumberOfRows(); i++) { + row = sheet.getRow(i); + if(row!=null){ + log.info("r:{}",row.getRowNum()); + + } + } + + log.info("end"); + } + + public static class EventExample implements HSSFListener { + private SSTRecord sstrec; + + /** + * 此方法监听传入记录并根据需要处理它们 + * + * @param record读取时找到的记录 + */ + public void processRecord(Record record) { + switch (record.getSid()) { + //BOFRecord可以表示工作表或工作簿的开头 + case BOFRecord.sid: + BOFRecord bof = (BOFRecord)record; + if (bof.getType() == bof.TYPE_WORKBOOK) { + System.out.println("监听到工作表"); + } else if (bof.getType() == bof.TYPE_WORKSHEET) { + System.out.println("监听到工作簿"); + } + break; + case BoundSheetRecord.sid: + BoundSheetRecord bsr = (BoundSheetRecord)record; + System.out.println("工作簿名称: " + bsr.getSheetname()); + break; + } + } + } + @Test public void test2() throws Exception { // 方法2 如果写到不同的sheet 同一个对象