Java自定义excel样式并导出(poi)


前言

本文提供了一些利用Java设计Excel表格的参数,自定义设置需求表格,话不多说,直接上代码


一、准备工作

这里可以参考作者的上一篇文章,导入poi的包就可以。
自带详细注释,小白也能看懂。

二、实现类源码

1.

代码如下(示例):

//todo
        //查询数据
        List<HospitalExcel> list = new ArrayList<>();
        HospitalExcel hospitalExcel1 = new HospitalExcel();
        HospitalExcel hospitalExcel2 = new HospitalExcel();
        hospitalExcel1.setHospName("11111");
        hospitalExcel1.setCCityName("儿童医院");
        hospitalExcel2.setHospName("1");
        list.add(hospitalExcel1);
        list.add(hospitalExcel2);

        try {
            //新建工作簿
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("工作薄");//工作薄名称
            //设置每格数据的样式
            HSSFFont ParamFontStyle = workbook.createFont();
            CellStyle cellParamStyle = workbook.createCellStyle();
            cellParamStyle.setAlignment(HorizontalAlignment.CENTER);//垂直居中
            cellParamStyle.setVerticalAlignment(VerticalAlignment.CENTER);//水平居中
            cellParamStyle.setWrapText(false);//自动换行
            ParamFontStyle.setFontHeightInPoints((short) 13);//字体大小
            ParamFontStyle.setFontName("等线");
            cellParamStyle.setFont(ParamFontStyle);
            

            //设置表头的样式
            HSSFFont ParamFontStyle1 = workbook.createFont();
            CellStyle cellParamStyle1 = workbook.createCellStyle();
            cellParamStyle1.setAlignment(HorizontalAlignment.LEFT);
            cellParamStyle1.setVerticalAlignment(VerticalAlignment.DISTRIBUTED);
            cellParamStyle1.setWrapText(false);//自动换行
            ParamFontStyle1.setFontHeightInPoints((short) 15);
            ParamFontStyle1.setFontName("黑体");
            ParamFontStyle1.setBold(true);//是否打开加粗
            cellParamStyle1.setFont(ParamFontStyle1);
            
            //设置标题的样式
            HSSFFont ParamFontStyle2 = workbook.createFont();
            CellStyle cellParamStyle2 = workbook.createCellStyle();
            cellParamStyle2.setAlignment(HorizontalAlignment.CENTER);//垂直居中
            cellParamStyle2.setVerticalAlignment(VerticalAlignment.CENTER);//水平居中
            cellParamStyle2.setWrapText(true);//自动换行
            ParamFontStyle2.setFontHeightInPoints((short) 14);
            ParamFontStyle2.setFontName("黑体");
            ParamFontStyle2.setBold(true);
			cellParamStyle2.setFont(ParamFontStyle2);
            
            //定义列的宽度
            //sheet.setDefaultColumnWidth(40 * 1024);默认宽度
            sheet.setColumnWidth(0,888);
            sheet.setColumnWidth(1,5000);
            sheet.setColumnWidth(2,5000);
            sheet.setColumnWidth(3,5000);
            sheet.setColumnWidth(4,5000);
            sheet.setColumnWidth(5,5000);
            sheet.setColumnWidth(6,5000);
            sheet.setColumnWidth(7,5000);
            sheet.setColumnWidth(8,5000);
            sheet.setColumnWidth(9,5000);
            sheet.setColumnWidth(10,5000);
            //设置表头
            HSSFRow rows = sheet.createRow(0);
            HSSFCell cell = rows.createCell(0);//一列的第一个单元格(下面的类似)

            HSSFRow rows1 = sheet.createRow(1);
            HSSFCell cell1 = rows1.createCell(0);

            HSSFRow rows2 = sheet.createRow(2);
            HSSFCell cell2 = rows2.createCell(0);

            HSSFRow rows3 = sheet.createRow(3);
            HSSFCell cell3 = rows3.createCell(0);
            //合并单元格
            CellRangeAddress region = new CellRangeAddress(0, 3, 5, 10);
            sheet.addMergedRegion(region);
            CellRangeAddress region1 = new CellRangeAddress(0, 0, 0, 4);
            sheet.addMergedRegion(region1);
            CellRangeAddress region2 = new CellRangeAddress(1, 1, 0, 4);
            sheet.addMergedRegion(region2);
            CellRangeAddress region3 = new CellRangeAddress(2, 2, 0, 4);
            sheet.addMergedRegion(region3);
            CellRangeAddress region4 = new CellRangeAddress(3, 3, 0, 4);
            sheet.addMergedRegion(region4);
            cell.setCellStyle(cellParamStyle1);
            cell1.setCellStyle(cellParamStyle1);
            cell2.setCellStyle(cellParamStyle1);
            cell3.setCellStyle(cellParamStyle1);
            cell.setCellValue("致:" );
            cell1.setCellValue("号码:111111111111111111111");
            cell2.setCellValue("金额:1111111111111111111");
            cell3.setCellValue("日期:11111111111111111111");//填入值


            //列名
            HSSFRow row1 = sheet.createRow(4);
            HSSFCell row1Cell0 = row1.createCell(0);
            row1Cell0.setCellValue("序号");
            row1Cell0.setCellStyle(cellParamStyle1);
            HSSFCell row1Cell = row1.createCell(1);
            row1Cell.setCellValue("=案件号    Claim");
            row1Cell.setCellStyle(cellParamStyle2);
            HSSFCell row1Cell1 = row1.createCell(2);
            row1Cell1.setCellValue("=公司     Insurer");
            row1Cell1.setCellStyle(cellParamStyle2);
            HSSFCell row1Cell2 = row1.createCell(3);
            row1Cell2.setCellValue("会员号       Membership");
            row1Cell2.setCellStyle(cellParamStyle2);
            HSSFCell row1Cell3 = row1.createCell(4);
            row1Cell3.setCellValue("姓名  Patient's Name");
            row1Cell3.setCellStyle(cellParamStyle2);
            HSSFCell row1Cell4 = row1.createCell(5);
            row1Cell4.setCellValue("日期    Service Date");
            row1Cell4.setCellStyle(cellParamStyle2);
            HSSFCell row1Cell5 = row1.createCell(6);
            row1Cell5.setCellValue("号码      Invoice");
            row1Cell5.setCellStyle(cellParamStyle2);
            HSSFCell row1Cell6 = row1.createCell(7);
            row1Cell6.setCellValue("金额   Amount Billed");
            row1Cell6.setCellStyle(cellParamStyle2);
            HSSFCell row1Cell7 = row1.createCell(8);
            row1Cell7.setCellValue("付金额      Amount Paid");
            row1Cell7.setCellStyle(cellParamStyle2);
            HSSFCell row1Cell8 = row1.createCell(9);
            row1Cell8.setCellValue("付金额    Amount Rejected");
            row1Cell8.setCellStyle(cellParamStyle2);
            HSSFCell row1Cell9 = row1.createCell(10);
            row1Cell9.setCellValue("付原因     Explanation");
            row1Cell9.setCellStyle(cellParamStyle2);

            //放入图片
            BufferedImage bufferImg = null;
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            InputStream is = this.getClass().getResourceAsStream("/e6ab3c4617327341fdc1dc6eb355c26.jpg");//获取图片。本文放在resources下
            bufferImg = ImageIO.read(is);
            ImageIO.write(bufferImg, "jpg", byteArrayOut);
            //画图的顶级管理器,一个sheet只能获取一个
            HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
            //anchor主要用于设置图片的属性
            HSSFClientAnchor anchor = new HSSFClientAnchor(150, 220, 1023, 0, (short) 9, 0, (short) 10, 3);
            anchor.setAnchorType(ClientAnchor.AnchorType.byId(3));
            //插入图片
            patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
            //数据存入表格
            int sheet1Colume = 0;
            for (HospitalExcel hospitalExcel : list) {
                String uname = hospitalExcel.getHospName();

                HSSFRow row2 = sheet.createRow(sheet1Colume + 5);

                HSSFCell row2Cell = row2.createCell(0);
                row2Cell.setCellStyle(cellParamStyle);
                row2Cell.setCellValue(sheet1Colume + 1);//序号

                HSSFCell row2Cell1 = row2.createCell(1);
                row2Cell1.setCellStyle(cellParamStyle);
                row2Cell1.setCellValue(uname);

                HSSFCell row2Cell2 = row2.createCell(2);
                row2Cell2.setCellStyle(cellParamStyle);
                row2Cell2.setCellValue("uid");

                HSSFCell row2Cell3 = row2.createCell(3);
                row2Cell3.setCellStyle(cellParamStyle);
                row2Cell3.setCellValue("inputs");

                HSSFCell row2Cell4 = row2.createCell(4);
                row2Cell4.setCellStyle(cellParamStyle);
                row2Cell4.setCellValue("handles");

                HSSFCell row2Cell5 = row2.createCell(5);
                row2Cell5.setCellStyle(cellParamStyle);
                row2Cell5.setCellValue("overs");

                HSSFCell row2Cell6 = row2.createCell(6);
                row2Cell6.setCellStyle(cellParamStyle);
                row2Cell6.setCellValue("overs");

                HSSFCell row2Cell7 = row2.createCell(7);
                row2Cell7.setCellStyle(cellParamStyle);
                row2Cell7.setCellValue("overs");

                HSSFCell row2Cell8 = row2.createCell(8);
                row2Cell8.setCellStyle(cellParamStyle);
                row2Cell8.setCellValue("overs");

                HSSFCell row2Cell9 = row2.createCell(9);
                row2Cell9.setCellStyle(cellParamStyle);
                row2Cell9.setCellValue("overs");

                HSSFCell row2Cell10 = row2.createCell(10);
                row2Cell10.setCellStyle(cellParamStyle);
                row2Cell10.setCellValue("overs");
                sheet1Colume++;
            }

            String fileName = new String("eob".getBytes(), "UTF-8") + ".xls";
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setCharacterEncoding("UTF-8");
            OutputStream os = response.getOutputStream();
            workbook.write(os);
            os.flush();
            try {
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new Result<>(ResultEnum.OK);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

2.展示

在这里插入图片描述

总结

本文作者用作自己练习和记录,希望对你有帮助,共同学习。

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值