Commit ec4344321cddc41e87a887921ae396e2de4a7978
1 parent
711787ac
Exists in
master
excel读取数字类型优化
Showing
2 changed files
with
8 additions
and
6 deletions
Show diff stats
build.gradle
src/main/java/com/taover/util/UtilExcel.java
| ... | ... | @@ -2,6 +2,7 @@ package com.taover.util; |
| 2 | 2 | |
| 3 | 3 | import java.io.File; |
| 4 | 4 | import java.io.FileOutputStream; |
| 5 | +import java.math.BigDecimal; | |
| 5 | 6 | import java.text.DecimalFormat; |
| 6 | 7 | import java.text.SimpleDateFormat; |
| 7 | 8 | import java.util.ArrayList; |
| ... | ... | @@ -10,6 +11,7 @@ import java.util.HashMap; |
| 10 | 11 | import java.util.List; |
| 11 | 12 | import java.util.Map; |
| 12 | 13 | |
| 14 | +import org.apache.poi.hssf.usermodel.HSSFDataFormatter; | |
| 13 | 15 | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| 14 | 16 | import org.apache.poi.ss.usermodel.Cell; |
| 15 | 17 | import org.apache.poi.ss.usermodel.CellStyle; |
| ... | ... | @@ -461,10 +463,10 @@ public class UtilExcel { |
| 461 | 463 | || cell.getCellStyle().getDataFormat()==57 || cell.getCellStyle().getDataFormat()==32 || cell.getCellStyle().getDataFormat()==20) { |
| 462 | 464 | // 如果是date类型则 ,获取该cell的date值 |
| 463 | 465 | return new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(cell.getNumericCellValue())); |
| 464 | - } else { | |
| 465 | - // 纯数字gaoming 新增兼容浮点型,转成字符串读取,不然小数点被省略了 | |
| 466 | - cell.setCellType(CellType.STRING); | |
| 467 | - return UtilString.trimCodePage(cell.getRichStringCellValue().getString()); | |
| 466 | + } else { | |
| 467 | + //gaoming 这个是poi自带获取value通用,如果再有问题就用这个试试!!! | |
| 468 | + HSSFDataFormatter dataFormatter = new HSSFDataFormatter(); | |
| 469 | + return dataFormatter.formatCellValue(cell); | |
| 468 | 470 | } |
| 469 | 471 | }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){ |
| 470 | 472 | return cell.getBooleanCellValue(); |
| ... | ... | @@ -501,7 +503,7 @@ public class UtilExcel { |
| 501 | 503 | |
| 502 | 504 | public static void main(String args[]){ |
| 503 | 505 | //String filepath = "C:\\Users\\root\\Desktop\\千丁-6.27.xlsx"; |
| 504 | - String filepath = "C:\\Users\\EDZ\\Desktop\\mmm.xlsx"; | |
| 506 | + String filepath = "C:\\Users\\EDZ\\Desktop\\aaa.xlsx"; | |
| 505 | 507 | List<List<Object>> data = null; |
| 506 | 508 | |
| 507 | 509 | try { | ... | ... |