java读取xlsx_Java POI读取excel 支持xls、xlsx

这篇博客介绍了如何使用Java的Apache POI库读取xls和xlsx格式的Excel文件。代码示例展示了如何遍历单元格,处理字符串、数字、日期和布尔类型的数据,并且详细解释了如何读取Excel中的图片。
摘要由CSDN通过智能技术生成

import java.io.File;

import java.io.FileInputStream;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFPicture;

import org.apache.poi.hssf.usermodel.HSSFPictureData;

import org.apache.poi.hssf.usermodel.HSSFShape;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.DateUtil;

import org.apache.poi.ss.usermodel.Row;

public final class TestImportExcel {

public static void main(String[] args) throws Exception {

File excelFile = new File("test.xls");

HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(excelFile));

HSSFSheet sheet = wb.getSheetAt(0);

for (Row row : sheet) {

for (Cell cell : row) {

switch (cell.getCellType()) {

case Cell.CELL_TYPE_STRING:

System.out.print(cell.getRichStringCellValue().getString());

break;

case Cell.CELL_TYPE_NUMERIC:

if (DateUtil.isCellDateFormatted(cell)) {

System.out.print(String.valueOf(cell.getDateCellValue()));

} else {

System.out.print(cell.getNumericCellValue());

}

break;

case Cell.CELL_TYPE_BOOLEAN:

System.out.print(cell.getBooleanCellValue());

break;

default:

}

}

System.out.println();

}

//读取图片

List pictures = wb.getAllPictures();

for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {

if (shape instanceof HSSFPicture) {

HSSFPicture pic = (HSSFPicture) shape;

int pictureIndex = pic.getPictureIndex()-1;

HSSFPictureData picData = pictures.get(pictureIndex);

System.out.println("image-size:" + picData.getData().length);

}

}

System.out.println(wb.getSheetName(0));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值