ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JAVA报表开发-POI处理EXCEL-导出(简单样式进阶)

2022-05-28 16:33:34  阅读:208  来源: 互联网

标签:JAVA HSSFCellStyle EXCEL cell THIN 设置 POI sheet font


一、导出时样式的设置

如果要求导出的excel如下内容:

 

1、通过上图可以看出有些样式需要我们来设置,来看一下都有哪些知识点:

1.画框线

/**
 * 设置框线
 */
HSSFCellStyle contentStyle = book.createCellStyle();
contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//底线
contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//顶部线
contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左侧线
contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右侧线

2、合并单元格

//合并单元格 起始行, 结束行, 起始列, 结束列        
sheet.addMergedRegion(new CellRangeAddress(0,0,0,4));

3、设置行高

/**
设置行高
*/
sheet.getRow(1).setHeight((short)500);

4、设置表格的对齐方式和字体

//*设置对齐方式和字体***/
//内容部分的样式
style_content.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置水平居中
style_content.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//设置垂直居中

HSSFFont font = book.createFont();//创建字体
font.setFontName("宋体");//设置字体名称
font.setFontHeightInPoints((short)11);//设置字体大小
style_content.setFont(font);//对样式设置字体
        
//标题样式
HSSFCellStyle style_title = book.createCellStyle();//创建标题样式
style_title.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置水平居中
style_title.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//设置垂直居中
HSSFFont titleFont = book.createFont();//设置标题字体
titleFont.setFontName("黑体");
titleFont.setBold(true);//加粗
titleFont.setFontHeightInPoints((short)18);//字体大小    
style_title.setFont(titleFont);//将标题字体设置到标题样式
sheet.getRow(0).getCell(0).setCellStyle(style_title);//单元格设置标题样式

二、代码

public void downLoadXlsxWithStyle(HttpServletResponse response) throws Exception {
        Workbook workbooks = new XSSFWorkbook();
        Sheet sheet = workbooks.createSheet("有样式的excel");


        //需求:1、边框线:全边框 2、行高 :42 列宽 3、合并单元格:第1行的第1个单元格到第5个单元格 4、对齐方式:水平垂直都要居中 5、字体:黑体18号字

        //1、边框线:全边框
        CellStyle bigTitleRowCellStyle = workbooks.createCellStyle();
        bigTitleRowCellStyle.setBorderLeft(BorderStyle.THIN);//左边框 THIN 细线
        bigTitleRowCellStyle.setBorderBottom(BorderStyle.THIN);//下边框
        bigTitleRowCellStyle.setBorderTop(BorderStyle.THIN);//上边框
        bigTitleRowCellStyle.setBorderRight(BorderStyle.THIN);//右边框

        //4、对齐方式:水平垂直都要居中
        bigTitleRowCellStyle.setAlignment(HorizontalAlignment.CENTER); //水平居中对齐
        bigTitleRowCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直居中对齐

        //5、字体
        Font font = workbooks.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short)18);
        //把字体放入到样式中
        bigTitleRowCellStyle.setFont(font);

        Row bigTitleRow = sheet.createRow(0);

        //2、行高 列宽
        bigTitleRow.setHeightInPoints(42);//设置行高
        //设置列宽
        sheet.setColumnWidth(0,5*256); //1代表的是一个标准字母宽度的256分之一
        sheet.setColumnWidth(1,8*256);
        sheet.setColumnWidth(2,15*256);
        sheet.setColumnWidth(3,15*256);
        sheet.setColumnWidth(4,30*256);

        for (int i = 0; i <5 ; i++) {
            Cell cell = bigTitleRow.createCell(i);
            cell.setCellStyle(bigTitleRowCellStyle);
        }
        //合并单元格
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,4));//int firstRow 起始行, int lastRow 结束行, int firstCol 开始列, int lastCol 结束列

        //向指定的单元格放入数据
        sheet.getRow(0).getCell(0).setCellValue("审核信息");

        /**
         * 处理小标题
         */

        //需求:1、边框线:全边框 2、行高 :42 列宽 3、合并单元格:第1行的第1个单元格到第5个单元格 4、对齐方式:水平垂直都要居中 5、字体:黑体18号字
        //1、边框线:全边框
        CellStyle littleRowCellStyle = workbooks.createCellStyle();
        //样式克隆
        littleRowCellStyle.cloneStyleFrom(bigTitleRowCellStyle);
        //5、字体
        Font littleFont = workbooks.createFont();
        littleFont.setFontName("宋体");
        littleFont.setFontHeightInPoints((short)12);
        littleFont.setBold(true);
        littleRowCellStyle.setFont(littleFont);

        //1、边框线:全边框
        CellStyle contentRowCellStyle = workbooks.createCellStyle();
        //样式克隆
        contentRowCellStyle.cloneStyleFrom(bigTitleRowCellStyle);
        contentRowCellStyle.setAlignment(HorizontalAlignment.LEFT);
        Font contentFont = workbooks.createFont();
        contentFont.setFontName("楷体");
        contentFont.setFontHeightInPoints((short)8);
        contentFont.setBold(true);
        //把字体放入到样式中
        contentRowCellStyle.setFont(contentFont);

        Row tittleRow  = sheet.createRow(1);
        String [] titles = new String[]{"编号","姓名","手机号","入职日期","现住址"};
        Cell cell = null;
        for (int i = 0; i < 4; i++) {
            cell=tittleRow.createCell(i);//创建单元格  列
            cell.setCellValue(titles[i]);//写入内容
            cell.setCellStyle(contentRowCellStyle);
        }


        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");

        List<User> userList = userMapper.selectAll();
        int rowIndex=1;
        for (User user : userList) {
            Row row = sheet.createRow(rowIndex); //创建行
            cell = row.createCell(0); //创建列
            cell.setCellValue(user.getId());
            cell.setCellStyle(contentRowCellStyle);

            cell = row.createCell(1);
            cell.setCellValue(user.getUserName());
            cell.setCellStyle(contentRowCellStyle);

            cell = row.createCell(2);
            cell.setCellValue(user.getPhone());
            cell.setCellStyle(contentRowCellStyle);

            cell = row.createCell(3);
            cell.setCellValue(simpleDateFormat.format(user.getHireDate()));
            cell.setCellStyle(contentRowCellStyle);

            cell = row.createCell(4);
            cell.setCellValue(user.getAddress());
            cell.setCellStyle(contentRowCellStyle);

            rowIndex++;
        }



        //文件导出 一个流(outputStream)两个头(文件的打开方式 in-line attachement, 文件的下载时mime类型)
        String filename="员工数据.xlsx";
        response.setHeader("content-disposition","attachment;filename="+new String(filename.getBytes(),"ISO8859-1"));
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        workbooks.write(response.getOutputStream());
    }

 

 

 

标签:JAVA,HSSFCellStyle,EXCEL,cell,THIN,设置,POI,sheet,font
来源: https://www.cnblogs.com/mangoubiubiu/p/16320942.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有