poi导入excel获取单元格格式小数位数

poi导入excel获取单元格格式小数位数,找了很久都没找到,还是从导出设置方法去反推的导入读取方法(2023.09.27 19:02):

Object value=null;
                        CellType cellType=cell.getCellType();
                        switch(cellType){
                        case STRING:
                            value=cell.getStringCellValue();
                            break;
                        case BOOLEAN:
                            value=cell.getBooleanCellValue();
                            break;
                        case NUMERIC:
                            value=cell.getNumericCellValue();
                            break;
                        case FORMULA:
                            try {
                                value=cell.getStringCellValue();
                            } catch (IllegalStateException e) {
//                                DecimalFormat df = new DecimalFormat("#.##");
                                XSSFCellStyle cellStyle = cell.getCellStyle();

                                //获取excel里面设置单元格样式里面设置的小数位数
                                String dataFormatString = cellStyle.getDataFormatString();//'0.00_ '
                                if(dataFormatString != null && dataFormatString.endsWith("_ ")){
                                    dataFormatString = dataFormatString.substring(0, dataFormatString.length()-2);
                                }
                                DecimalFormat df = new DecimalFormat(dataFormatString);
                                value = df.format(cell.getNumericCellValue());
                            }
                            break;
                        default:
                            value="";
                        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用POICellValueFormatter类来获取Excel单元格格式。具体的操作如下: 1. 首先,使用POI读取Excel文件并获取一个单元格对象; 2. 然后,使用这个单元格对象的getCellType方法获取单元格据类型; 3. 倘若该单元格据类型为值型的话,可以使用POI的DateUtil类来判断该单元格格式,并进行相应的处理。 例如,以下是一个使用POI获取Excel单元格格式的示例代码: ```java // 导入POI相关的库 import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; 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.ss.usermodel.CellValue; import org.apache.poi.ss.usermodel.CellValue.CellTypeEnum; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataFormatter; public class Example { public static void main(String[] args) throws Exception { // 读取Excel文件 Workbook workbook = WorkbookFactory.create(new File("example.xlsx")); Sheet sheet = workbook.getSheetAt(0); // 获取某个单元格 Cell cell = sheet.getRow(0).getCell(0); // 判断单元格据类型 if (cell.getCellType() == CellType.NUMERIC) { // 判断单元格格式 if (DateUtil.isCellDateFormatted(cell)) { // 处理日期型据 Date date = cell.getDateCellValue(); String formattedDate = new SimpleDateFormat("yyyy/MM/dd").format(date); System.out.println(formattedDate); } else { // 处理普通值型据 Double value = cell.getNumericCellValue(); System.out.println(value); } } else { // 处理其他类型的据 DataFormatter formatter = new DataFormatter(); String text = formatter.formatCellValue(cell); System.out.println(text); } } } ``` 以上代码使用POI读取了一个Excel文件中的单元格,并根据该单元格据类型和格式进行相应的处理。具体可以根据实际需要进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值