HSSFWorkbook(poi) 导出的数字型的单元格内容 (处理导出后数字不可求和的问题)

这篇博客介绍了如何使用HSSFWorkbook(Apache POI库)创建Excel文件,并解决数字型单元格内容导出后无法正确求和的问题。通过设置单元格的数据格式,确保数字可以被正确解析和计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        //创建一个excel文件
        HSSFWorkbook workBook = new  HSSFWorkbook();
        //excel内容
        HSSFSheet mySheet;
        mySheet = workBook.createSheet("sheet1");//创建一个工作薄
        mySheet.setColumnWidth(0,6000);// //第一个参数代表列id(从0开始),第2个参数代表列宽度值
        mySheet.setColumnWidth(1,6000);
        mySheet.setColumnWidth(2,6000);
        mySheet.setColumnWidth(3,10000);
        mySheet.setColumnWidth(4,10000);
        
        //单元格格式
        HSSFCellStyle style = workBook.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//对齐方式
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框

        HSSFFont font = workBook.createFont();//设置字体样式
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 16);    //字体大小
        font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);  
        style.setFont(font);
        
        //单元格格式
        HSSFCellStyle styleTwo = workBook.createCellStyle();
        styleTwo.setAlignment(HSSFCellStyle.ALIGN_CENTER);//对齐方式
        styleTwo.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
        styleTwo.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        styleTwo.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        styleTwo.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        
        HSSFFont fontTwo = workBook.createFont();//设置字体样式
        fontTwo.setFontName("宋体");
        fontTwo.setFontHeightInPoints((short) 16);
        fontTwo.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
        styleTwo.setFont(fontTwo);
        
        //单元格格式
        HSSFCellStyle styleThree = workBook.createCellStyle();
        styleThree.setAlignment(HSSFCellStyle.ALIGN_CENTER);//对齐方式
        styleThree.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
        styleThree.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        styleThree.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        styleThree.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        styleThree.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));//excel 自定义的格式

        HSSFFont fontThree = workBook.createFont();//设置字体样式
        fontThree.setFontName("宋体");
        fontThree.setFontHeightInPoints((short) 16);    //字体大小
        fontThree.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);  
        styleThree.setFont(font);
        
        HSSFRow myRow = mySheet.createRow(0);//创建 并设置第一行
        myRow.setHeight((short) 500);
        
        HSSFCell cell = myRow.createCell((short) 0);
        cell.setCellStyle(styleTwo);
        cell.setCellValue("用户名");
        cell = myRow.createCell((short) 1);
        cell.setCellStyle(styleTwo);
        cell.setCellValue("用户电话");
        cell = myRow.createCell((short) 2);
        cell.setCellStyle(styleTwo);
        cell.setCellValue("用户类型");
        cell = myRow.createCell((short) 3);
        cell.setCellStyle(styleTwo);
        cell.setCellValue("收益总金额(元)");
        cell = myRow.createCell((short)4);
        cell.setCellStyle(styleTwo);
        cell.setCellValue("钱包余额(元)");
        
        for(int i = 1; i <= list.size(); i++){
            myRow = mySheet.createRow(i);
            UserIncomeStatistic  userIncomeStatistic = list.get(i-1);   //数据信息
            cell = myRow.createCell((short) 0);
            cell.setCellStyle(style);
            cell.setCellValue(userIncomeStatistic.getUserName());
            cell = myRow.createCell((short) 1);
            cell.setCellStyle(style);
            cell.setCellValue(userIncomeStatistic.getUserMobile());
            cell = myRow.createCell((short) 2);
            cell.setCellStyle(style);
            String userType = "";
            if("Admin".equals(userIncomeStatistic.getUserType())){
                userType = "管理员";
            } else if("Business".equals(userIncomeStatistic.getUserType())){
                userType = "商家";
            } else if("Member".equals(userIncomeStatistic.getUserType())){
                userType = "会员";
            }
            cell.setCellValue(userType);
            cell = myRow.createCell((short) 3);
            cell.setCellStyle(styleThree);
            cell.setCellValue(Double.parseDouble(userIncomeStatistic.getIncomeAllSum()));   //字符串转为double
            cell = myRow.createCell((short) 4);
            cell.setCellStyle(styleThree);
            cell.setCellValue(Double.parseDouble(userIncomeStatistic.getBalance()));  //字符串转为double

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值