首页 > 编程语言 >java导入excel数据,要求数据精度与文件一致

java导入excel数据,要求数据精度与文件一致

时间:2024-07-17 11:25:21浏览次数:10  
标签:java format res excel else cell sdf 导入 getCellStyle

最近应客户需求,导入excel表格,且要求数据精度和日期格式与文件一致。

之前虽然做过导入导出的功能,但要求没有这么细致,因此在网上查找了大量的文件,找到了表格的cell.getCellStyle().getDataFormatString()这个属性,可以根据属性在程序里转换成自己需要的格式。

public String getCellValue(Row row, int column) {
        if (row == null) {
            return "";
        }
        Object val = "";
        String res = "";
        Cell cell = row.getCell(column); 
        try {
            if (StringUtils.isNotNull(cell)) {
//                System.out.println("------------"+ cell.getCellType()
//                 +"====="+row.getCell(1).getStringCellValue()+"**********"); 
                if (cell.getCellType() == CellType.NUMERIC) {
                     val = cell.getNumericCellValue();
//                    System.out.println(cell.getCellStyle().getDataFormat()+"------------"+ cell.getCellType()
//                     +"====="+row.getCell(1).getStringCellValue()+"*********$$$$"+val+"**********"+cell.getCellStyle().getDataFormatString());
                    SimpleDateFormat sdf = getDateFormat(cell.getCellStyle().getDataFormatString().replaceAll("\"", "")); 
                    if (HSSFDateUtil.isCellDateFormatted(cell)) {//处理数值格式
                        res = sdf.format(cell.getDateCellValue());
                    }  else if (DateUtil.isCellDateFormatted(cell) 
                            || sdf !=null)) {  
                        val = DateUtil.getJavaDate((Double) val); 
                        res = sdf.format(val);
                    } else { 
                        if(cell.getCellStyle().getDataFormatString().contains("-")) {  
                            String f1 = cell.getCellStyle().getDataFormatString();
//                            System.out.println("format========"+f1);
                            f1 =f1.replace("\\", "").replace("\"", "");  
//                            System.out.println("format========"+f1);
                             DecimalFormat format = new DecimalFormat();  
                             format.applyPattern(f1);  
                             res = format.format(val);
                        }else     if(cell.getCellStyle().getDataFormatString().contains("%")) {
                            res = new DecimalFormat(cell.getCellStyle().getDataFormatString().split("%")[0]).format(cell.getNumericCellValue()*100)+"%";
                        }else     if(cell.getCellStyle().getDataFormatString().equals("General")) {  
                             DecimalFormat format = new DecimalFormat();  
                             format.applyPattern("#");  
                             res = format.format(val);
                        }else     if(cell.getCellStyle().getDataFormatString().contains(";")) {
                            String format = cell.getCellStyle().getDataFormatString().split(";")[0];
//                            System.out.println("format========"+format);
                            format =format.substring(0,format.lastIndexOf("0")+1);
//                            System.out.println("format========"+format);
                            res = new DecimalFormat(format).format(val);
                        }else {
                            val = new BigDecimal(val.toString()); 
                            res = val.toString();
                        }
                    } 
                }else if (cell.getCellType().equals(CellType.FORMULA)) {
                    switch (cell.getCachedFormulaResultType()) {
                    case STRING:
                        res = cell.getStringCellValue();
                        break;
                    case NUMERIC:
                        NumberFormat nf = NumberFormat.getInstance(); 
//                        System.out.println(cell.getCellStyle().getDataFormat()+"------------"+ cell.getCellType()
//                         +"====="+row.getCell(1).getStringCellValue()+"**********"+"$$$$"+HSSFDateUtil.isCellDateFormatted(cell)+"**********"+cell.getCellStyle().getDataFormatString());
                        nf.setGroupingUsed(false);
                        res = String.valueOf(nf.format(cell.getNumericCellValue()));
                        SimpleDateFormat sdf = getDateFormat(cell.getCellStyle().getDataFormatString().replaceAll("\"", "")); 
                        if (row.getCell(1).getStringCellValue().contains("孵天")) {
                            res = getTime(cell.getNumericCellValue());
                        } else if (DateUtil.isCellDateFormatted(cell)||sdf!=null) { 
                            //                            System.out.println(res+"------"+cell.getNumericCellValue()); 
                            val = DateUtil.getJavaDate(cell.getNumericCellValue()); // POI Excel 日期格式转换
                            res = sdf.format(val).replace("00时00分", "");
                        }else { 
                            double d = cell.getNumericCellValue();   
                            if(cell.getCellStyle().getDataFormatString().contains(";[Red]")) {
//                                System.out.println(cell.getCellStyle().getDataFormat()+"------------"+ cell.getCellType()
//                                 +"====="+row.getCell(1).getStringCellValue()+"**********"+ val+"     $$$$         "+d+"**********"+cell.getCellStyle().getDataFormatString());
                                res = new DecimalFormat(cell.getCellStyle().getDataFormatString().split(";[Red]")[0]).format(d);
                            }
                        }
                        break;
                    case BOOLEAN:
                        res = String.valueOf(cell.getBooleanCellValue());
                        break;
                    default:
                        res = "";
                    }
                    // res = evaluate.formatAsString();
                } else if (cell.getCellType() == CellType.STRING) {
                    res = cell.getStringCellValue();
                } else if (cell.getCellType() == CellType.BOOLEAN) {
                    res = Boolean.toString(cell.getBooleanCellValue());
                } else if (cell.getCellType() == CellType.ERROR) {
                    res = Byte.toString(cell.getErrorCellValue());
                }
            } 
        } catch (Exception e) {
            // TODO: handle exception
        }
        return res;
    }

这个是我的导入的数据的一些年月日的格式,就加入了进来,希望能帮助大家。

private SimpleDateFormat getDateFormat(String s1) {
        // TODO Auto-generated method stub
        SimpleDateFormat sdf = null;
        if(s1.equals("yyyy年mm月dd日hh时mm分")) {
            sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分"); 
        }else if(s1.equals("yyyy年mm月dd日")) {
            sdf = new SimpleDateFormat("yyyy年MM月dd日"); 
        }else if(s1.equals("mm月dd日hh时mm分")) {
            sdf = new SimpleDateFormat("MM月dd日HH时mm分"); 
        }else if(s1.equals("mm月dd日hh时")) {
            sdf = new SimpleDateFormat("MM月dd日HH时"); 
        }else if(s1.equals("mm月dd日")) {
            sdf = new SimpleDateFormat("MM月dd日"); 
        }else if(s1.equals("hh时mm分")) {
            sdf = new SimpleDateFormat("HH时mm分"); 
        }else if(s1.contains("h:mm")) {
            sdf = new SimpleDateFormat("HH:mm"); 
        }else if(s1.contains("yyyy.mm")) {
            sdf = new SimpleDateFormat("yyyy.MM"); 
        }else if(s1.equals("yyyy年mm月dd日hh时")) {
            sdf = new SimpleDateFormat("yyyy年MM月dd日HH时"); 
        }else if(s1.equals("hh时")) {
            sdf = new SimpleDateFormat("HH时"); 
        }
        return sdf;
    }

标签:java,format,res,excel,else,cell,sdf,导入,getCellStyle
From: https://blog.csdn.net/qq_35129486/article/details/140489838

相关文章

  • python 导入时与运行时
    转载自我自己的github博客——>半天钟的博客元编程相关博文的目录及链接这篇博文是元编程系列博文中的其中一篇、这个系列中其他博文的目录和连接见下:使用python特性管理实例属性浅析python属性描述符(上)浅析python属性描述符(下)python导入时与运行时python元......
  • java的数组
    程序=逻辑+数据,数组是存储数据的强而有力的手段。——闫学灿一维数组数组的定义//int[]a;//定义//a=newint[10];//初始化int[]a=newint[10],b;//边定义边初始化,b也是数组,但是没有初始化,是一个空数组float[]f=newfloa......
  • Java JVM——12. 垃圾回收理论概述
    1.前言1.1 什么是垃圾?在提到什么是垃圾之前,我们先看下面一张图:从上图我们可以很明确的知道,Java和C++语言的区别,就在于垃圾收集技术和内存动态分配上,C++语言没有垃圾收集技术,需要我们手动的收集。垃圾收集,不是Java语言的伴生产物,早在1960年,第一门开始使用......
  • java中用高德工具测试两点的距离
    文章讲述了如何在Java中利用DistinctUtil工具类通过高德地图API获取两个地理位置之间的驾车距离,涉及经纬度处理、URL构建、HTTP请求和JSON解析过程。摘要由CSDN通过智能技术生成  代码如下:StringstartLongitude=entity.getLONGITUDE();//起点(当前位置)经度......
  • 一种使用Excel直观验证机器视觉引导坐标的方法
    1.首先格式化数据,作为像素坐标,右为机械手坐标2.各自绘制散点图3.将散点图背景色调至半透明:右键图表,填充选项选择无填充 4.将标记点填充色调节至半透明5.缩放图表使二者的标记点尽量重合,就可以看出数据的相对偏移情况 Done! ......
  • JAVA中的运算符
    赋值运算符赋值运算符有一个有趣的特性:它允许创建赋值链。例如,分析下面的代码段:intx,y,z;x=y=z=100;//setx,y,andzto100算术运算符需要注意的地方:(1)当将除法运算符用于整数类型时,其结果不会包含小数部分。(2)求模运算符%,返回除法操作的余数。它既可以用......
  • javaJDK以及IDLE软件的安装
    记录数据开发实习生的学习之路目录记录数据开发实习生的学习之路前言一、java的安装说明二、intelijIDLE的安装总结前言本文用于从零开始,零基础学习java和sql以适应工作要求,博主会将每日学习的进度同步到文章中,希望能给大家一些帮助一、java的安装说明:本......
  • Python办公自动化:效率飞跃,自动化批量汇总Excel到Word
    Python办公自动化:效率飞跃,自动化批量汇总Excel到Word原创 丹心向阳 数海丹心 2024年06月23日07:30 山东摘要:每个月底,是许多数据分析师的梦魇,尤其是当他们需要从成百上千的Excel报表中汇总数据到Word时。本文将讲述小李如何使用Python自动化技术,几秒钟完成原本需要通宵达旦......
  • Java计算机毕业设计的家政服务平台(开题报告+源码+论文)
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景:随着城市化进程的加速和生活节奏的加快,现代家庭对于家政服务的需求日益增长。传统的家政服务方式往往存在信息不对称、服务质量参差不齐、预约流程繁......
  • Java计算机毕业设计的在线英语学习系统(开题报告+源码+论文)
    本系统(程序+源码)带文档lw万字以上 文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景:在全球化的今天,英语作为国际交流的重要工具,其学习需求日益增长。然而,传统英语学习方式受限于时间、地点和教学资源,难以满足广大学习者个性化、灵活化......