Java实现导出数据到excel

导出数据到excel工作簿

OutputStream outputStream = null;
        try {
            String fileName = "XXX.xls"; //文件名称自定义
            //文件路径
            String filePath = RuoYiConfig.getDownloadPath() + fileName;
            File file = new File(filePath);
            //判断文件时候存在,存在就删除重新创建
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            //创建工作簿及定义工作簿名称
            HSSFWorkbook wb = new HSSFWorkbook();
            //excel标签名
            HSSFSheet sheet = wb.createSheet("风险综合评级系统取数数据表");
            //设置列宽及列数
            int[] excelHeaderWidth = new int[5];
            for (int i = 0; i < excelHeaderWidth.length; i++) {
                excelHeaderWidth[i] = 200;
            }
            //风格设置
            //整体风格(单独设置)
            HSSFCellStyle style = this.createStyle(wb, false, true, false);
            //标题风格 加粗(单独设置)
            HSSFCellStyle styletitle = this.createStyle(wb, true, false, true);
            //标题风格 不加粗(单独设置)
            HSSFCellStyle styletitle1 = this.createStyle(wb, true, false, false);
            //文本风格
            HSSFCellStyle textStyle = wb.createCellStyle();
            //设置列为文本
            sheet.setDefaultColumnStyle(0, textStyle);
            sheet.setDefaultColumnStyle(1, textStyle);
            sheet.setDefaultColumnStyle(2, textStyle);
            sheet.setDefaultColumnStyle(3, textStyle);
            sheet.setDefaultColumnStyle(4, textStyle);

            //设置列宽度(像素)
            for (int i = 0; i < excelHeaderWidth.length; i++) {
                sheet.setColumnWidth(i, 32 * excelHeaderWidth[i]);
            }
            
			//设置标题行
            HSSFRow row1 = sheet.createRow(0);
            HSSFCell cell = row1.createCell(0);         //第一个单元格
            cell.setCellValue("指标编码");                  //设定值
            cell.setCellStyle(style);                   //内容居中

            cell = row1.createCell(1);                   //第二个单元格
            cell.setCellValue("指标名称");
            cell.setCellStyle(style);

            cell = row1.createCell(2);                   //第三个单元格
            cell.setCellValue("部门名称");
            cell.setCellStyle(style);

            cell = row1.createCell(3);                   //第四个单元格
            cell.setCellValue("风险点");
            cell.setCellStyle(style);

            cell = row1.createCell(4);                   //第五个单元格
            cell.setCellValue("指标值");
            cell.setCellStyle(style);

            //添加excel数据
            //tbcrr07List 为业务数据
            for (int i = 0; i < tbcrr07List.size(); i++) {
            //添加列元素
                HSSFRow row = sheet.createRow(i+1);
                row.createCell(0).setCellValue("//取list中集合对象属性");
                row.createCell(1).setCellValue("");
                row.createCell(2).setCellValue("");
                row.createCell(3).setCellValue("");
                row.createCell(4).setCellValue("");
            }
            //创建输出流及输出文件
            outputStream = new FileOutputStream(filePath);
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
            return AjaxResult.success(file.getName(), "UTF-8");
        } catch (RuntimeException e) {
            logger.error("导出Excel失败。", e);
            throw e;
        } finally {
            //关闭流
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }

Excel样式设计

//设置excel样式
    private HSSFCellStyle createStyle(HSSFWorkbook wb, boolean isTitle, boolean isRight, boolean isBold) {
        //整体风格(单独设置)
        HSSFCellStyle style = wb.createCellStyle();//风格设置
        //文本字体
        HSSFFont font = wb.createFont();//字体设置
        font.setColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());//颜色
        //判断是不是标题
        if (isTitle) {
            font.setFontHeightInPoints((short) 12); //设置字体大小
        } else {
            font.setFontHeightInPoints((short) 10); //设置字体大小
        }
        //字体是否加粗
        if (isBold) {
            font.setBold(true);
        } else {
            font.setBold(false);
        }
        style.setFont(font);
        //是否靠右显示
        if (isRight) {
            style.setAlignment(HorizontalAlignment.RIGHT);
        } else {
            style.setAlignment(HorizontalAlignment.CENTER);
        }
        return style;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值