(54条消息) Java实现Excel导入和导出,看这一篇就够了(珍藏版)_zyqok的博客-优快云博客
以上是这个博主的导入导出 很全值得收藏
下面是我根据这位博主修改的 代码片段
private static String getCellValue(Cell cell,int rowNum) {
// 空白或空
if (cell == null || cell.getCellTypeEnum() == CellType.BLANK) {
//TODO 导入图片上传
if (cell.getSheet() instanceof XSSFSheet) {
XSSFSheet xssfSheet = (XSSFSheet) cell.getSheet();
for (XSSFShape shape : xssfSheet.getDrawingPatriarch().getShapes()) {
if (shape instanceof XSSFPicture) {
XSSFPicture picture = (XSSFPicture) shape;
// 获取图片所在单元格的坐标
XSSFClientAnchor anchor = picture.getClientAnchor();
if (anchor.getRow1() == rowNum && anchor.getCol1() == cell.getColumnIndex()) {
// 获取图片的二进制数据
byte[] pictureData = picture.getPictureData().getData();
// todo 上传图片 返回url
System.out.println("图片数据长度:" + pictureData.length);
}
}
}
}
return "";
}
// String类型
if (cell.getCellTypeEnum() == CellType.STRING) {
String val = cell.getStringCellValue();
if (val == null || val.trim().length() == 0) {
return "";
}
return val.trim();
}
// 数字类型
if (cell.getCellTypeEnum() == CellType.NUMERIC) {
String s = cell.getNumericCellValue() + "";
// 去掉尾巴上的小数点0
if (Pattern.matches(".*\\.0*", s)) {
return s.split("\\.")[0];
} else {
return s;
}
}
// 布尔值类型
if (cell.getCellTypeEnum() == CellType.BOOLEAN) {
return cell.getBooleanCellValue() + "";
}
// 错误类型
return cell.getCellFormula();
}