Java 通过 Apache POI 导入导出 Excel

添加maven依赖

<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi</artifactId>
	<version>4.1.0</version>
</dependency>

POI 中的组件

组件 含义
HSSFWorkbook excel的文档对象
HSSFSheet excel的表单
HSSFRow excel的行
HSSFCell excel的列
HSSFFont excel的字体
HSSFDataFormat 日期格式
HSSFHeader sheet的头
HSSFFooter sheet的尾

一个Excel文件对应于一个HSSFWorkbook对象,一个HSSFWorkbook对象可以有多个HSSFSheet对象组成,一个HSSFSheet对象是由多个HSSFRow对象组成,一个HSSFRow对象是由多个HSSFCell对象组成。

导出Excel

@Test
public void writeTest(){
    HSSFWorkbook workbook = new HSSFWorkbook();

    //列样式
    HSSFCellStyle cellStyle = workbook.createCellStyle();
    //水平居中
    cellStyle.setAlignment(HorizontalAlignment.CENTER);
    //垂直居中
    cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    //设置需要前景颜色或背景颜色,一定要指定填充方式
    //1. 未指定填充方式,即使设置了前景色和背景色,也不会显示
    //2. 设置填充,前景色,背景色,显示填充,前景色和背景色混合颜色
    //3. 设置填充,前景色,显示填充,前景色
    //4. 设置填充,背景色,显示填充,不显示背景色
    cellStyle.setFillPattern(FillPatternType.DIAMONDS);
    cellStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.RED.getIndex());
    cellStyle.setFillBackgroundColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex());

    //左边框
    cellStyle.setLeftBorderColor(HSSFColor.HSSFColorPredefined.LIGHT_YELLOW.getIndex());
    cellStyle.setBorderLeft(BorderStyle.DOTTED);
    //右边框
    cellStyle.setRightBorderColor(HSSFColor.HSSFColorPredefined.GOLD.getIndex());
    cellStyle.setBorderRight(BorderStyle.THICK);
    //上边框
    cellStyle.setTopBorderColor(HSSFColor.HSSFColorPredefined.GREEN.getIndex());
    cellStyle.setBorderTop(BorderStyle.DASHED);
    //下边框
    cellStyle.setBottomBorderColor(HSSFColor.HSSFColorPredefined.PINK.getIndex());
    cellStyle.setBorderBottom(BorderStyle.HAIR);

    //字体样式
    HSSFFont font = workbook.createFont();
    font.setFontName("黑体");
    font.setFontHeightInPoints((short) 15);
    font.setColor(HSSFColor.HSSFColorPredefined.GOLD.getIndex());
    cellStyle.setFont(font);

    for(int sheetNum = 0; sheetNum <= 2 ; sheetNum++){
        HSSFSheet sheet = workbook.createSheet("测试sheet" + sheetNum);

        //行高
        sheet.setDefaultRowHeightInPoints(40);
        //指定列宽
        sheet.setColumnWidth(0,256 * 15);
        sheet.setColumnWidth(1,256 * 30);
        sheet.setColumnWidth(2,256 * 30);

        //标题
        HSSFRow titleRow = sheet.createRow(0);
        HSSFCell titleCell = titleRow.createCell(0);
        titleCell.setCellValue("测试标题");
        titleCell.setCellStyle(cellStyle);
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,2));

        //头部
        HSSFRow headRow = sheet.createRow(1);
        headRow.createCell(0).setCellValue("序号");
        headRow.getCell(0).setCellStyle(cellStyle);
        headRow.createCell(1).setCellValue("测试1");
        headRow.getCell(1).setCellStyle(cellStyle);
        headRow.createCell(2).setCellValue("测试2");
        headRow.getCell(2).setCellStyle(cellStyle);

        //设值
        for(int i = 2 ; i <= 10 + sheetNum ; i++){
            HSSFRow row = sheet.createRow(i);
            for(int j = 0 ; j <= 2 ; j++){
                HSSFCell cell = row.createCell(j);
                cell.setCellStyle(cellStyle);
                if(j == 0){
                    cell.setCellValue(i - 1);
                }else{
                    cell.setCellValue("设值 行: " + (i - 1) + " 列: " + (j + 1));
                }
            }
        }
    }

    try(
        FileOutputStream outputStream = new FileOutputStream("D:\\测试.xls");
    	){
        workbook.write(outputStream);
    }catch (IOException e){
        e.printStackTrace();
    }
}

Java 通过 Apache POI 导入导出 Excel
导入Excel

@Test
public void readTest(){
    Map<String, List<Object>> resultMap = new HashMap<>(16);

    try(
        FileInputStream inputStream = new FileInputStream("D:\\测试.xls");
    	){
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        for(int sheetNum = 0 ; sheetNum < workbook.getNumberOfSheets() ; sheetNum++){
            HSSFSheet sheet = workbook.getSheetAt(sheetNum);
            for(int rowNum = 2 ; rowNum < sheet.getPhysicalNumberOfRows() ; rowNum++){
                HSSFRow row = sheet.getRow(rowNum);
                List<Object> cellList = new ArrayList<>();
                for(int cellNum = 0 ; cellNum < row.getPhysicalNumberOfCells() ; cellNum++){
                    HSSFCell cell = row.getCell(cellNum);
                    switch (cell.getCellType()) {
                        case NUMERIC:
                            cellList.add(cell.getNumericCellValue());
                            break;
                        case STRING:
                            cellList.add(cell.getStringCellValue());
                        default:
                    }
                }
                StringBuilder keyStringBuilder = new StringBuilder();
                resultMap.put(keyStringBuilder
                        .append("sheet")
                        .append(sheetNum)
                        .append("row")
                        .append(rowNum - 1).toString(),cellList);
            }
        }
    }catch (IOException e){
        e.printStackTrace();
    }
}

参考:
HSSFWorkbook 模版使用

上一篇:php-Codeigniter cookie没有设置?


下一篇:PHP-将变量从MY_Controller传递到视图