java poi应用,导出数据时合并单元格、居中、单元格背景色

@日常记录java poi

java poi应用,导出数据时合并单元格、居中、单元格背景色

poi版本

poi-3.17

合并单元格

	// 合并日期占两行(4个参数,分别为起始行,结束行,起始列,结束列);
    // 行和列都是从0开始计数,且起始结束都会合并
     CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2);
     sheet.addMergedRegion(region);
     region = new CellRangeAddress(1, 1, 0, 2);
     sheet.addMergedRegion(region);

内容居中

	HSSFCellStyle style = workbook.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);	//左右居中
    //style.setVerticalAlignment(VerticalAlignment.CENTER);	//上下居中

单元格背景色

	style1.setFillForegroundColor(IndexedColors.GREEN.getIndex()); //背景色
    //旧版 CellStyle.SOLID_FOREGROUND
    style1.setFillPattern(FillPatternType.SOLID_FOREGROUND); //新版

详细代码

	//原始数据
    Map<String, Integer> map = new HashMap<String, Integer>();
    Map<String, Map<String, Integer>> subMap = new HashMap<String, Map<String, Integer>>();
    map.put("奥迪", 15);
    map.put("大众", 35);
    Map<String, Integer> sm = new HashMap<String, Integer>();
    sm.put("奥迪A4", 10);
    sm.put("奥迪A6", 5);
    subMap.put("奥迪", sm);
    sm = new HashMap<String, Integer>();
    sm.put("CC", 10);
    sm.put("大众1", 10);
    sm.put("大众2", 15);
    subMap.put("大众", sm);
	
	//格式及数据填充
	HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFCellStyle style = workbook.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);	//左右居中
    //style.setVerticalAlignment(VerticalAlignment.CENTER);	//上下居中
    style.setFillForegroundColor(IndexedColors.RED.getIndex()); //全局背景色
    //旧版 CellStyle.SOLID_FOREGROUND
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND); //新版
    HSSFSheet sheet = workbook.createSheet("sheet");

    HSSFCellStyle style1 = workbook.createCellStyle();
	style1.setAlignment(HorizontalAlignment.CENTER);	//左右居中
    //style.setVerticalAlignment(VerticalAlignment.CENTER);	//上下居中
    style1.setFillForegroundColor(IndexedColors.GREEN.getIndex()); //全局背景色
    //旧版 CellStyle.SOLID_FOREGROUND
    style1.setFillPattern(FillPatternType.SOLID_FOREGROUND); //新版
    
    HSSFRow row = sheet.createRow(0);
    HSSFRow row1 = sheet.createRow(1);
    HSSFRow row2 = sheet.createRow(2);
    HSSFRow row3 = sheet.createRow(3);
    HSSFCell cell = null;
    HSSFCell cell1 = null;
    HSSFCell cell2 = null;
    HSSFCell cell3 = null;
    int index = -1;
    int beforeIndex = 0;
    int subIndex = -1;
    for(Map.Entry<String, Map<String, Integer>> entry : subMap.entrySet()){
    	Map<String, Integer> value = entry.getValue();
    	if(value != null && !value.isEmpty()){
			cell = row.createCell(index+1);
			cell1 = row1.createCell(index+1);
			beforeIndex = index+1;
			index += value.size();
    		cell.setCellStyle(style1);
            cell.setCellValue(entry.getKey());
            cell1.setCellStyle(style);
            cell1.setCellValue(map.get(entry.getKey()));
            for(Map.Entry<String, Integer> subEntry : value.entrySet()){
            	subIndex++;
            	cell2 = row2.createCell(subIndex);
            	cell2.setCellStyle(style);
                cell2.setCellValue(subEntry.getKey());
                cell3 = row3.createCell(subIndex);
            	cell3.setCellStyle(style);
                cell3.setCellValue(subEntry.getValue());
            }
            // 合并日期占两行(4个参数,分别为起始行,结束行,起始列,结束列)
            // 行和列都是从0开始计数,且起始结束都会合并
            CellRangeAddress region = new CellRangeAddress(0, 0, beforeIndex, index);
            sheet.addMergedRegion(region);
            region = new CellRangeAddress(1, 1, beforeIndex, index);
            sheet.addMergedRegion(region);
    	}else{
    		cell = row.createCell(index+1);
			cell1 = row1.createCell(index+1);
			index += 1;
    		cell.setCellStyle(style);
            cell.setCellValue(entry.getKey());
            cell1.setCellStyle(style);
            cell1.setCellValue(map.get(entry.getKey()));
            subIndex ++;
    	}
    }
    File file = new File("D:\\demo.xls");
    FileOutputStream fout = new FileOutputStream(file);
    workbook.write(fout);
    fout.close();

运行结果

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Apache poi 导出excel合并单元格可以使用setRegionStyle方法来设置合并单元格的样式。这个方法接受三个参数,分别是Sheet对象、CellRangeAddress对象和CellStyle对象。在这个方法中,通过循环遍历合并单元格的每一行和每一列,然后获取对应的单元格并设置样式。具体的代码实现可以参考引用\[1\]中的示例代码。 此外,还可以使用PoiModel类来定义导出excel数据模型。这个类包含了内容、上一行同一位置内容、行标、列标等属性。可以根据实际需求来使用这个类。具体的代码实现可以参考引用\[2\]和引用\[3\]中的示例代码。 总结起来,Apache poi 导出excel合并单元格的步骤包括设置合并单元格的样式和定义导出数据的模型。可以根据具体的需求来使用相应的方法和类来实现导出功能。 #### 引用[.reference_title] - *1* *3* [poi 导出Excel 动态 合并单元格](https://blog.csdn.net/weixin_65436454/article/details/127806178)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [使用POI 导出Excel 动态合并单元格](https://blog.csdn.net/weixin_41722928/article/details/112849624)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值