getNumericCellValue():在用poi读取excel数据时,为什么cell.getNumericCellValue()总是得到8.8888888E7

在用poi读取excel数据时,为什么cell.getNumericCellValue()总是得到8.8888888E7(应该得到数字88888888才对)

使用DecimalFormat

java.text.DecimalFormat formatter = new java.text.DecimalFormat("########");

String str = formatter.format(cell.getNumericCellValue());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用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类来读取图像。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值