EasyExcel带有标题头的两种简单读方法

工作中有使用easyexcel依赖库读写excel,简单记录下来~

案例如下:

引入easyexcel依赖

<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->

创建用例Bean对象

方法一:单独创建读的监听器实现ReadListener接口

覆写invoke方法(在invoke方法里写入你需要对读取的数据进行的操作)

测试实现:

需指定对象类,读取第一个sheet,文件流回自动关闭,注意:消息头的行数2列,使用headRowNumber()标记表头的列数,表头不读取。

 

方法二:监听器与实现合并

执行结果:

方式二更为简洁,目前工作中使用的是方法二,这只是简单的读实现,仅供参考~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
EasyExcel是一款Java的Excel操作工具,它可以帮助我们读取和操作Excel文件。不幸的是,EasyExcel目前还不支持直接读取带有图片的Excel文件。但是,你可以使用其他的Java库,比如Apache POI,来实现读取带有图片的Excel文件。 使用Apache POI,你可以使用`XSSFWorkbook`类来读取Excel文件,然后使用`XSSFPictureData`类来获取图片数据。下面是一个示例代码,展示了如何使用Apache POI读取带有图片的Excel文件: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFPictureData; import java.io.FileInputStream; import java.io.IOException; public class ExcelReader { public static void main(String[] args) { try { FileInputStream file = new FileInputStream("path/to/your/excel/file.xlsx"); Workbook workbook = new XSSFWorkbook(file); Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { if (cell.getCellType() == CellType.STRING) { System.out.print(cell.getStringCellValue() + "\t"); } else if (cell.getCellType() == CellType.NUMERIC) { System.out.print(cell.getNumericCellValue() + "\t"); } else if (cell.getCellType() == CellType.BOOLEAN) { System.out.print(cell.getBooleanCellValue() + "\t"); } else if (cell.getCellType() == CellType.BLANK) { System.out.print("\t"); } else if (cell.getCellType() == CellType.ERROR) { System.out.print("ERROR" + "\t"); } else if (cell.getCellType() == CellType.FORMULA) { System.out.print(cell.getCellFormula() + "\t"); } else if (cell.getCellType() == CellType._NONE) { // Do nothing } if (cell.getCellType() == CellType.PICTURE) { for (int i = 0; i < workbook.getAllPictures().size(); i++) { PictureData pictureData = workbook.getAllPictures().get(i); if (pictureData instanceof XSSFPictureData) { XSSFPictureData xssfPictureData = (XSSFPictureData) pictureData; byte[] pictureBytes = xssfPictureData.getData(); // 在这里可以对图片数据进行处理 // ... } } } } System.out.println(); } workbook.close(); file.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 你需要将代码中的`"path/to/your/excel/file.xlsx"`替换为你实际的Excel文件路径。这段代码会遍历Excel文件的所有单元格,并输出单元格的值。如果单元格中包含图片,你可以在`if (cell.getCellType() == CellType.PICTURE)`的代码块中对图片数据进行处理。 希望这个示例能帮到你!如果有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值