使用poi读取Excel中的各种数据类型处理

Excel中可能会有各种数据格式,但poi只提供了那几种,这就要我们好好处理我们想要的数据了

以下是我的总结:

if (null != cell)
{
// 以下是判断数据的类型
switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC: // 数字

if (0 == cell.getCellType()) {//判断单元格的类型是否则NUMERIC类型
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 判断是否为日期类型
Date date = cell.getDateCellValue();
DateFormat formater = new SimpleDateFormat(
"yyyy-MM-dd HH:mm");
cellValue = formater.format(date);
}else{
cellValue = cell.getNumericCellValue() + "";
}
}
break;


case HSSFCell.CELL_TYPE_STRING: // 字符串
cellValue = cell.getStringCellValue();
break;


case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
cellValue = cell.getBooleanCellValue() + "";
break;


case HSSFCell.CELL_TYPE_FORMULA: // 公式
cellValue = cell.getCellFormula() + "";
break;


case HSSFCell.CELL_TYPE_BLANK: // 空值
cellValue = "";
break;


case HSSFCell.CELL_TYPE_ERROR: // 故障
cellValue = "非法字符";
break;


default:
cellValue = "未知类型";
break;

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用Apache POI库来读取Excel数据并提取图片。以下是一个简单的示例代码: ```java import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.util.IOUtils; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFPicture; import org.apache.poi.xssf.usermodel.XSSFPictureData; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ReadExcelWithImage { public static void main(String[] args) throws IOException { String filePath = "path/to/excel/file.xlsx"; InputStream inputStream = new FileInputStream(filePath); Workbook workbook = WorkbookFactory.create(inputStream); int sheetCount = workbook.getNumberOfSheets(); for (int i = 0; i < sheetCount; i++) { Sheet sheet = workbook.getSheetAt(i); if (sheet instanceof XSSFSheet) { XSSFSheet xssfSheet = (XSSFSheet) sheet; for (Row row : xssfSheet) { for (Cell cell : row) { switch (cell.getCellType()) { case STRING: System.out.print(cell.getStringCellValue() + "\t"); break; case NUMERIC: System.out.print(cell.getNumericCellValue() + "\t"); break; case BOOLEAN: System.out.print(cell.getBooleanCellValue() + "\t"); break; case FORMULA: System.out.print(cell.getCellFormula() + "\t"); break; default: System.out.print(" " + "\t"); } if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) { switch (cell.getCachedFormulaResultType()) { case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue() + "\t"); break; case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + "\t"); break; } } if (cell.getCellType() == Cell.CELL_TYPE_BLANK) { System.out.print(" " + "\t"); } if (cell.getCellType() == Cell.CELL_TYPE_ERROR) { System.out.print(cell.getErrorCellValue() + "\t"); } if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { System.out.print(cell.getBooleanCellValue() + "\t"); } if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { System.out.print(cell.getNumericCellValue() + "\t"); } if (cell.getCellType() == Cell.CELL_TYPE_STRING) { System.out.print(cell.getStringCellValue() + "\t"); } if (cell.getCellType() == Cell.CELL_TYPE_BLANK) { System.out.print("" + "\t"); } if (cell.getCellType() == Cell.CELL_TYPE_FORMULA && cell.getCachedFormulaResultType() == Cell.CELL_TYPE_NUMERIC) { System.out.print(cell.getNumericCellValue() + "\t"); } if (cell.getCellType() == Cell.CELL_TYPE_FORMULA && cell.getCachedFormulaResultType() == Cell.CELL_TYPE_STRING) { System.out.print(cell.getStringCellValue() + "\t"); } // get image from cell if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { if (xssfSheet.getDrawingPatriarch() != null) { for (XSSFPicture picture : xssfSheet.getDrawingPatriarch().getShapes().getPictures()) { XSSFClientAnchor anchor = picture.getClientAnchor(); if (anchor.getRow1() == row.getRowNum() && anchor.getCol1() == cell.getColumnIndex()) { XSSFPictureData pictureData = picture.getPictureData(); byte[] bytes = pictureData.getData(); String extension = pictureData.suggestFileExtension(); String fileName = "image" + row.getRowNum() + "_" + cell.getColumnIndex() + "." + extension; IOUtils.write(bytes, new FileOutputStream(fileName)); } } } } } System.out.println(""); } } } } } ``` 在这个例子,我们遍历每个单元格,并根据其类型打印出其内容。如果单元格包含图片,我们使用Apache POI的XSSFDrawing对象来提取图像并将其保存到文件。 请注意,上面的代码仅适用于XLSX格式的Excel文件。如果您的Excel文件是旧的XLS格式,您需要使用HSSFWorkbook和HSSFPicture类来读取图像。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值