请教!java读取Excel计算列的问题

feihong_wang 2018-04-25 10:06:21
码代码突然遇到个问题,想获得从Excel中通过固定日期拿到的星期值
EXCEL中计算列是这样写的
=WEEKDAY(A3)
现在想拿到这列值,但是通过XSSFFormulaEvaluator类读取到方格里的计算值为null,我是这么写的
XSSFWorkbook wb = new XSSFWorkbook();
XSSFFormulaEvaluator evaluate = new XSSFFormulaEvaluator (wb);
通过循环拿到对应方格的值后
CellValue tempCellValue = evaluator.evaluate(xssfCell);
String iCellValue = tempCellValue.getStringValue();
return iCellValue;
...全文
930 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
feihong_wang 2018-04-26
  • 打赏
  • 举报
回复
引用 7 楼 hbhbhbhbhb1021 的回复:
这个代码是调试过的,可以取出来。那个EXCEL的文件是什么版本的?OFFICE的版本
我现在可以取出来,我的意思是直接取到星期几而不是再用枚举进行转换,这样写就可以写进工具类了,如果只是取到double再写枚举,函数一变方法就不能用了
hbhbhbhbhb1021 2018-04-26
  • 打赏
  • 举报
回复
EXCEL中找一个没用的sheet,写个枚举,用vlookup函数,直接改单元格的函数吧
feihong_wang 2018-04-25
  • 打赏
  • 举报
回复
这个计算结果只能拿到一个double类型的值,1.0对应星期天,2.0对应星期一,以此类推,楼主直接在后头用switch进行选择了,大哥们有没有更好点的办法啊
hbhbhbhbhb1021 2018-04-25
  • 打赏
  • 举报
回复
这个代码是调试过的,可以取出来。那个EXCEL的文件是什么版本的?OFFICE的版本
feihong_wang 2018-04-25
  • 打赏
  • 举报
回复
引用 5 楼 hbhbhbhbhb1021 的回复:
加个枚举就行了啊

/**
         * 得到Excel,并解析内容  对2007及以上版本 使用XSSF解析
         * @param file
         * @throws FileNotFoundException
         * @throws IOException
         * @throws InvalidFormatException
         */
        @Test
        public static void getExcelAsFile(String file) throws FileNotFoundException, IOException, InvalidFormatException {


            InputStream ins = null;
            Workbook wb = null;
            ins=new FileInputStream(new File(file));
            wb = WorkbookFactory.create(ins);
            ins.close();
            FormulaEvaluator formulaEvaluator=new XSSFFormulaEvaluator((XSSFWorkbook) wb);

            //3.得到Excel工作表对象
            Sheet sheet = wb.getSheetAt(0);
            //总行数
            int trLength = sheet.getLastRowNum();
            //4.得到Excel工作表的行
            Row row = sheet.getRow(0);
            //总列数
            int tdLength = row.getLastCellNum();
            //5.得到Excel工作表指定行的单元格
            Cell cell = row.getCell((short)1);
            //6.得到单元格样式
            CellStyle cellStyle = cell.getCellStyle();

            for(int i=1;i<=trLength;i++){
                //得到Excel工作表的行
                Row row1 = sheet.getRow(i);
                for(int j=0;j<tdLength;j++){
                    //得到Excel工作表指定行的单元格
                    Cell cell1 = row1.getCell(j);
                    /**
                     * 为了处理:Excel异常Cannot get a text value from a numeric cell
                     * 将所有列中的内容都设置成String类型格式
                     */
                    if(cell1!=null){
                        cell1.setCellType(XSSFCell.CELL_TYPE_FORMULA);
                        cell1.setCellFormula("WEEKDAY(C1)");

                        System.out.print( Week.getName(formulaEvaluator.evaluate(cell1).getNumberValue()));

                    }



                    //获得每一列中的值
                    System.out.print(cell1+"                   ");
                }
                System.out.println();
            }

            //将修改后的数据保存
            OutputStream out = new FileOutputStream(file);
            wb.write(out);
        }


public enum Week {
        Mon("星期一", 2.0), Tue("星期二", 3.0), Wed("星期三", 4.0), Thu("星期四", 5.0), Fri("星期五", 6.0), Sat("星期六", 7.0), Sun("星期日", 1.0);
        // 成员变量
        private String dayName;
        private double index;
        // 构造方法
        private Week(String dayName, double index) {
            this.dayName = dayName;
            this.index = index;
        }
        // 普通方法
        public static String getName(double index) {
            for (Week w : Week.values()) {
                if (w.getIndex() == index) {
                    return w.dayName;
                }
            }
            return null;
        }

        public String getDayName() {
            return dayName;
        }

        public void setDayName(String dayName) {
            this.dayName = dayName;
        }

        public double getIndex() {
            return index;
        }

        public void setIndex(double index) {
            this.index = index;
        }
    }
就是取不出来咯
hbhbhbhbhb1021 2018-04-25
  • 打赏
  • 举报
回复
加个枚举就行了啊

/**
         * 得到Excel,并解析内容  对2007及以上版本 使用XSSF解析
         * @param file
         * @throws FileNotFoundException
         * @throws IOException
         * @throws InvalidFormatException
         */
        @Test
        public static void getExcelAsFile(String file) throws FileNotFoundException, IOException, InvalidFormatException {


            InputStream ins = null;
            Workbook wb = null;
            ins=new FileInputStream(new File(file));
            wb = WorkbookFactory.create(ins);
            ins.close();
            FormulaEvaluator formulaEvaluator=new XSSFFormulaEvaluator((XSSFWorkbook) wb);

            //3.得到Excel工作表对象
            Sheet sheet = wb.getSheetAt(0);
            //总行数
            int trLength = sheet.getLastRowNum();
            //4.得到Excel工作表的行
            Row row = sheet.getRow(0);
            //总列数
            int tdLength = row.getLastCellNum();
            //5.得到Excel工作表指定行的单元格
            Cell cell = row.getCell((short)1);
            //6.得到单元格样式
            CellStyle cellStyle = cell.getCellStyle();

            for(int i=1;i<=trLength;i++){
                //得到Excel工作表的行
                Row row1 = sheet.getRow(i);
                for(int j=0;j<tdLength;j++){
                    //得到Excel工作表指定行的单元格
                    Cell cell1 = row1.getCell(j);
                    /**
                     * 为了处理:Excel异常Cannot get a text value from a numeric cell
                     * 将所有列中的内容都设置成String类型格式
                     */
                    if(cell1!=null){
                        cell1.setCellType(XSSFCell.CELL_TYPE_FORMULA);
                        cell1.setCellFormula("WEEKDAY(C1)");

                        System.out.print( Week.getName(formulaEvaluator.evaluate(cell1).getNumberValue()));

                    }



                    //获得每一列中的值
                    System.out.print(cell1+"                   ");
                }
                System.out.println();
            }

            //将修改后的数据保存
            OutputStream out = new FileOutputStream(file);
            wb.write(out);
        }


public enum Week {
        Mon("星期一", 2.0), Tue("星期二", 3.0), Wed("星期三", 4.0), Thu("星期四", 5.0), Fri("星期五", 6.0), Sat("星期六", 7.0), Sun("星期日", 1.0);
        // 成员变量
        private String dayName;
        private double index;
        // 构造方法
        private Week(String dayName, double index) {
            this.dayName = dayName;
            this.index = index;
        }
        // 普通方法
        public static String getName(double index) {
            for (Week w : Week.values()) {
                if (w.getIndex() == index) {
                    return w.dayName;
                }
            }
            return null;
        }

        public String getDayName() {
            return dayName;
        }

        public void setDayName(String dayName) {
            this.dayName = dayName;
        }

        public double getIndex() {
            return index;
        }

        public void setIndex(double index) {
            this.index = index;
        }
    }
feihong_wang 2018-04-25
  • 打赏
  • 举报
回复
引用 2 楼 hbhbhbhbhb1021 的回复:
是的,可以参考下取数那段

/**
         * 得到Excel,并解析内容  对2007及以上版本 使用XSSF解析
         * @param file
         * @throws FileNotFoundException
         * @throws IOException
         * @throws InvalidFormatException
         */
        @Test
        public static void getExcelAsFile(String file) throws FileNotFoundException, IOException, InvalidFormatException {


            InputStream ins = null;
            Workbook wb = null;
            ins=new FileInputStream(new File(file));
            wb = WorkbookFactory.create(ins);
            ins.close();
            FormulaEvaluator formulaEvaluator=new XSSFFormulaEvaluator((XSSFWorkbook) wb);

            //3.得到Excel工作表对象
            Sheet sheet = wb.getSheetAt(0);
            //总行数
            int trLength = sheet.getLastRowNum();
            //4.得到Excel工作表的行
            Row row = sheet.getRow(0);
            //总列数
            int tdLength = row.getLastCellNum();
            //5.得到Excel工作表指定行的单元格
            Cell cell = row.getCell((short)1);
            //6.得到单元格样式
            CellStyle cellStyle = cell.getCellStyle();

            for(int i=1;i<=trLength;i++){
                //得到Excel工作表的行
                Row row1 = sheet.getRow(i);
                for(int j=0;j<tdLength;j++){
                    //得到Excel工作表指定行的单元格
                    Cell cell1 = row1.getCell(j);
                    /**
                     * 为了处理:Excel异常Cannot get a text value from a numeric cell
                     * 将所有列中的内容都设置成String类型格式
                     */
                    if(cell1!=null){
                        cell1.setCellType(XSSFCell.CELL_TYPE_FORMULA);
                        cell1.setCellFormula("WEEKDAY(C1)");

                        System.out.print( formulaEvaluator.evaluate(cell1).getNumberValue());

                    }



                    //获得每一列中的值
                    System.out.print(cell1+"                   ");
                }
                System.out.println();
            }

            //将修改后的数据保存
            OutputStream out = new FileOutputStream(file);
            wb.write(out);
        }
额,兄弟,可能我的表述有问题,我不是要导出EXCEL,是把EXCEL往数据库里导,现在要解析EXCEL里“=WEEKDAY(A3)“这样的函数,想直接拿到星期几,不是double类型1.0 2.0那样的值
hbhbhbhbhb1021 2018-04-25
  • 打赏
  • 举报
回复
EXCEL见这个图,只是个测试例子
hbhbhbhbhb1021 2018-04-25
  • 打赏
  • 举报
回复
是的,可以参考下取数那段

/**
         * 得到Excel,并解析内容  对2007及以上版本 使用XSSF解析
         * @param file
         * @throws FileNotFoundException
         * @throws IOException
         * @throws InvalidFormatException
         */
        @Test
        public static void getExcelAsFile(String file) throws FileNotFoundException, IOException, InvalidFormatException {


            InputStream ins = null;
            Workbook wb = null;
            ins=new FileInputStream(new File(file));
            wb = WorkbookFactory.create(ins);
            ins.close();
            FormulaEvaluator formulaEvaluator=new XSSFFormulaEvaluator((XSSFWorkbook) wb);

            //3.得到Excel工作表对象
            Sheet sheet = wb.getSheetAt(0);
            //总行数
            int trLength = sheet.getLastRowNum();
            //4.得到Excel工作表的行
            Row row = sheet.getRow(0);
            //总列数
            int tdLength = row.getLastCellNum();
            //5.得到Excel工作表指定行的单元格
            Cell cell = row.getCell((short)1);
            //6.得到单元格样式
            CellStyle cellStyle = cell.getCellStyle();

            for(int i=1;i<=trLength;i++){
                //得到Excel工作表的行
                Row row1 = sheet.getRow(i);
                for(int j=0;j<tdLength;j++){
                    //得到Excel工作表指定行的单元格
                    Cell cell1 = row1.getCell(j);
                    /**
                     * 为了处理:Excel异常Cannot get a text value from a numeric cell
                     * 将所有列中的内容都设置成String类型格式
                     */
                    if(cell1!=null){
                        cell1.setCellType(XSSFCell.CELL_TYPE_FORMULA);
                        cell1.setCellFormula("WEEKDAY(C1)");

                        System.out.print( formulaEvaluator.evaluate(cell1).getNumberValue());

                    }



                    //获得每一列中的值
                    System.out.print(cell1+"                   ");
                }
                System.out.println();
            }

            //将修改后的数据保存
            OutputStream out = new FileOutputStream(file);
            wb.write(out);
        }

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧