POI导出Excel文件

POI导出Excel文件

基于HttpServletResponse进行文件out,实现导出功能

主要使用poi进行excel文件导出功能

生成 .xlsx 后缀的excel文件,基于XSSFWorkbookXSSFSheetXSSFRowXSSFCell等对象

对象对应需要导入的包:

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
  • XSSFWorkbook 文件对象创建该对象以获得一个excel文件对象

    Workbook wb = new XSSFWorkbook();
    
  • XSSFSheet sheet对象即每个文件中的sheet,一个表格可以有多个sheet

    sheet对象创建时指定sheet名称

    XSSFSheet sheet = ((XSSFWorkbook) wb).createSheet("sheet1");
    

    设置列宽

    setColumnWidth(int columnIndex, int width)
    columnIndex列号需要设置的列下标,从0开始
    width宽度这个具体看效果了,参考设置值252 * 35

    sheet.setColumnWidth(1, 252 * 35 + 100);
    sheet.setColumnWidth(2, 252 * 15);
    
  • XSSFRow 行对象每条数据新建一个行对象

    行对象需要在sheet中创建
    创建新行需要指定行号,excel最大行数好像65535😂😂
    即:

    XSSFRow row_head = sheet.createRow(0);//表头
    XSSFRow row_1 = sheet.createRow(1);
    XSSFRow row_2 = sheet.createRow(2);
    

    设置行高:

    row_head.setHeight((short) 600);
    

    设置表头行-背景色:

    需要从表头设置的样式中设置颜色,然后表头行中设置此样式

    cellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.index);
    cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    
  • XSSFCell 列对象每行数据新建一个列对象

    列对象需要在行中创建,创建新列需要指定列下标
    在表头(row_head)中创建3列即:

    XSSFCell cell = row_head.createCell(0);
    cell.setCellValue("第一列");
    cell.setCellStyle(cellStyle);
    XSSFCell cell = row_head.createCell(1);
    cell.setCellValue("第二列");
    cell.setCellStyle(cellStyle);
    XSSFCell cell = row_head.createCell(2);
    cell.setCellValue("第三列");
    cell.setCellStyle(cellStyle);
    
  • CellStyle单元格样式每个单元格都需要设置样式

    设置边框 ,上下左右,边框样式:CellStyle.BORDER_*

    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
    cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
    

    设置边框颜色,上下左右,颜色:HSSFColor.*.index

    cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
    cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
    cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
    cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
    

    设置 水平 垂直居中

    cellStyle.setAlignment(CellStyle.ALIGN_CENTER);//水平
    cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直
    
  • Font字体样式设置单元格的字体样式,如需要使用,则每个单元格都需要设置字体样式:cellStyle.setFont(font_data);

    • 包名:org.apache.poi.ss.usermodel.Font
      创建font对象: Font font = wb.createFont();

        font.setFontName("黑体");
        font.setFontHeightInPoints((short) 16);// 设置字体大小
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//字段宽度
        font.setColor(0);//字体颜色
      

      Color0无颜色不设置;HSSFColor.GREEN.index绿色

简单介绍一下需要使用到的类,和作用

使用是按照顺序创建对象

类名作用创建
org.apache.poi.xssf.usermodel.XSSFWorkbookexcel文件对象Workbook wb = new XSSFWorkbook();
org.apache.poi.xssf.usermodel.XSSFSheetsheet对象XSSFSheet sheet = ((XSSFWorkbook) wb).createSheet("sheetName");
org.apache.poi.xssf.usermodel.XSSFRow行对象XSSFRow row_head = sheet.createRow(0);
org.apache.poi.xssf.usermodel.XSSFCell列对象XSSFCell cell_data = row_data.createCell(0);

1.先创建文件对象
2.文件对象中创建sheet
3.sheet中创建 行^n^
4.行^n^中创建列^n^

样式相关类

类名作用创建
org.apache.poi.hssf.usermodel.HSSFCellStyle样式CellStyle cellStyle = wb.createCellStyle();
org.apache.poi.hssf.usermodel.HSSFFont字体Font font = wb.createFont();

截止到此,可以完成一般样式的单元格 创建工作,循环循环即可

单元格合并

多行,多列合并,本文简单介绍一下合并类的使用:CellRangeAddress

sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 0));//序号列

上述代码,合并第二行到第三行中的第1列(下标从0开始)
参数介绍new CellRangeAddress(起始行, 截止行, 起始列, 截止列)
例如,要合并第三行的第五个单元格,合并到第10行
sheet.addMergedRegion(new CellRangeAddress(4, 11, 4, 4)); 单列合并
sheet.addMergedRegion(new CellRangeAddress(4, 11, 4, 5)); 多列合并

最后嵌套一个循环 实现自动合并,简单的实现效果如下

在这里插入图片描述

需求描述:同一个公司下的人员进行公司名称合并
实现思路:将所有人员查询出来,则一共8条数据,将这8条数据的公司名称循环处理,最后得出一个MAP,如果需要顺序不乱可以使用:LinkedHashMap

代码:

public static void main(String[] args){
        Map<String,List<String>> childMap = new LinkedHashMap<>();
        List<String> l = new ArrayList<>();
        l.add("111");
        l.add("111");
        childMap.put("1",l);
        l = new ArrayList<>();
        l.add("222");
        l.add("333");
        l.add("444");
        childMap.put("2",l);
        l = new ArrayList<>();
        l.add("5555");
        childMap.put("3",l);
        l = new ArrayList<>();
        l.add("666");
        l.add("777");
        childMap.put("4",l);
        l = new ArrayList<>();
        l.add("8888");
        l.add("99999");
        childMap.put("5",l);

        int beginRow = 2;
        for (String key : childMap.keySet()){
            int size = childMap.get(key).size();
            if (size > 1){
                //需要合并
                int end = (beginRow+size-1);
                System.out.println(beginRow+"-------"+end);
                beginRow += (end - beginRow + 1);
            }else {
                beginRow += 1;
            }
        }
    }

输出内容:

2-------3
4-------6
8-------9
10-------11

将起始行和截止行,放入一个list中,由于excel的机制,必须先创建表格、行数据,之后才能进行合并,所以将需要合并的数据放入list中,循环结束后再循环list进行行合并

for (String s : spanList){
    String[] index = s.split("-");
    //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
    sheet.addMergedRegion(new CellRangeAddress(Integer.parseInt(index[0]), Integer.parseInt(index[1]), 0, 0));//序号列
    sheet.addMergedRegion(new CellRangeAddress(Integer.parseInt(index[0]), Integer.parseInt(index[1]), 1, 1));//序号列
    sheet.addMergedRegion(new CellRangeAddress(Integer.parseInt(index[0]), Integer.parseInt(index[1]), 16, 16));//序号列
    sheet.addMergedRegion(new CellRangeAddress(Integer.parseInt(index[0]), Integer.parseInt(index[1]), 17, 17));//序号列
}

以上!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值