javaPoi导出excel并设置多级表头

public static HSSFWorkbook checkAccountSuccessDataExcel(List> list,String dateTest,String fatherIdText){

		// 1.创建工作簿
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 一级标题
		CellRangeAddress callRangeAddress01 = new CellRangeAddress(0, 0, 0, 20);// 起始行,结束行,起始列,结束列
		// 二级标题
		CellRangeAddress callRangeAddress11 = new CellRangeAddress(1, 1, 0, 10);// 起始行,结束行,起始列,结束列
		CellRangeAddress callRangeAddress12 = new CellRangeAddress(1, 1, 11, 20);// 起始行,结束行,起始列,结束列
		// 三级标题
		CellRangeAddress callRangeAddress21 = new CellRangeAddress(2, 2, 0, 0);
		CellRangeAddress callRangeAddress22 = new CellRangeAddress(2, 2, 1, 2);
		CellRangeAddress callRangeAddress23 = new CellRangeAddress(2, 2, 3, 4);
		CellRangeAddress callRangeAddress24 = new CellRangeAddress(2, 2, 5, 6);
		CellRangeAddress callRangeAddress25 = new CellRangeAddress(2, 2, 7, 8);
		CellRangeAddress callRangeAddress26 = new CellRangeAddress(2, 2, 9, 10);
		CellRangeAddress callRangeAddress27 = new CellRangeAddress(2, 2, 11, 12);
		CellRangeAddress callRangeAddress28 = new CellRangeAddress(2, 2, 13, 14);
		CellRangeAddress callRangeAddress29 = new CellRangeAddress(2, 2, 15, 16);
		CellRangeAddress callRangeAddress291 = new CellRangeAddress(2, 2, 17, 18);
		CellRangeAddress callRangeAddress292 = new CellRangeAddress(2, 2, 19, 20);

		// 标题样式
		HSSFCellStyle telStyle = createCellStyle(workbook, (short) 10, true,true,false);
		// 内容样式
		HSSFCellStyle cellStyle = createCellStyle(workbook, (short) 10, false,true,false);
		
		// 2.创建工作表
		HSSFSheet sheet = workbook.createSheet("sheet1");
		// 2.1加载合并单元格对象
		sheet.addMergedRegion(callRangeAddress01);
		sheet.addMergedRegion(callRangeAddress11);
		sheet.addMergedRegion(callRangeAddress12);
		sheet.addMergedRegion(callRangeAddress21);
		sheet.addMergedRegion(callRangeAddress22);
		sheet.addMergedRegion(callRangeAddress23);
		sheet.addMergedRegion(callRangeAddress24);
		sheet.addMergedRegion(callRangeAddress25);
		sheet.addMergedRegion(callRangeAddress26);
		sheet.addMergedRegion(callRangeAddress27);
		sheet.addMergedRegion(callRangeAddress28);
		sheet.addMergedRegion(callRangeAddress29);
		sheet.addMergedRegion(callRangeAddress291);
		sheet.addMergedRegion(callRangeAddress292);
		// 设置默认列宽
		sheet.setDefaultColumnWidth(15);
		
		// 一级标题
		HSSFRow row1 = sheet.createRow(0);
		HSSFCell cell1 = row1.createCell(0);
		// 加载单元格样式
		cell1.setCellStyle(telStyle);
		cell1.setCellValue("对账成功报表");

		//二级标题
		HSSFRow row2 = sheet.createRow(1);
		HSSFCell cell21 = row2.createCell(0);
		HSSFCell cell22 = row2.createCell(11);
		// 加载单元格样式
		cell21.setCellStyle(telStyle);
		cell21.setCellValue("时间:"+(dateTest==null?"":dateTest));
		cell22.setCellStyle(telStyle);
		cell22.setCellValue("业务类型:"+(fatherIdText==null?"":fatherIdText));

		//三级标题
		 HSSFRow row3 = sheet.createRow(2);
		 HSSFCell cell31 =  row3.createCell(0);
		 HSSFCell cell32 = row3.createCell(1);
		 HSSFCell cell33 = row3.createCell(3);
		 HSSFCell cell34 = row3.createCell(5);
		 HSSFCell cell35 = row3.createCell(7);
		 HSSFCell cell36 = row3.createCell(9);
		 HSSFCell cell37 = row3.createCell(11);
		 HSSFCell cell38 = row3.createCell(13);
		 HSSFCell cell39 = row3.createCell(15);
		 HSSFCell cell310 = row3.createCell(17);
		 HSSFCell cell311 = row3.createCell(19);
		 //加载单元格样式
		 cell31.setCellStyle(telStyle);
		 cell31.setCellValue("");
		 cell32.setCellStyle(telStyle);
		 cell32.setCellValue("财付通");
		 cell33.setCellStyle(telStyle);
		 cell33.setCellValue("支付宝");
		 cell34.setCellStyle(telStyle);
		 cell34.setCellValue("翼支付");
		 cell35.setCellStyle(telStyle);
		 cell35.setCellValue("银联支付");
		 cell36.setCellStyle(telStyle);
		 cell36.setCellValue("付费通");
		 cell37.setCellStyle(telStyle);
		 cell37.setCellValue("支付宝wap");
		 cell38.setCellStyle(telStyle);
		 cell38.setCellValue("财付通 H5");
		 cell39.setCellStyle(telStyle);
		 cell39.setCellValue("集团小白支付");
		 cell310.setCellStyle(telStyle);
		 cell310.setCellValue("微信支付");
		 cell311.setCellStyle(telStyle);
		 cell311.setCellValue("合计");

		// 3.2创建列标题;并且设置列标题
		HSSFRow rowtel = sheet.createRow(3);
		String[] titles = { "", "笔数", "金额","笔数", "金额","笔数", "金额","笔数", "金额","笔数", "金额","笔数", "金额","笔数", "金额","笔数", "金额","笔数", "金额","笔数", "金额"};// ""为占位字符串
		for (int i = 0; i < titles.length; i++) {
			HSSFCell cell2 = rowtel.createCell(i);
			// 加载单元格样式
			cell2.setCellStyle(telStyle);
			cell2.setCellValue(titles[i]);
		}

		// 4.操作单元格;将用户列表写入excel
		if (list != null) {
			Map map = null;
			for (int j = 0; j < list.size(); j++) {
				map = list.get(j);
				// 创建数据行,前面有两行,头标题行和列标题行
				HSSFRow rowContent1 = sheet.createRow(j + 4);
				
				HSSFCell cellContent1 = rowContent1.createCell(0);
				cellContent1.setCellStyle(cellStyle);
				cellContent1.setCellValue((String) map.get("fChannelName"));

				HSSFCell cellContent2 = rowContent1.createCell(1);
				cellContent2.setCellStyle(cellStyle);
				cellContent2.setCellValue((Long) map.get("totalZ1"));
				
				
				HSSFCell cellContent3 = rowContent1.createCell(2);
				cellContent3.setCellStyle(cellStyle);
				cellContent3.setCellValue((Double) map.get("amountZ1"));
				
				HSSFCell cellContent4 = rowContent1.createCell(3);
				cellContent4.setCellStyle(cellStyle);
				cellContent4.setCellValue((Long) map.get("totalZ2"));
				
				HSSFCell cellContent5 = rowContent1.createCell(4);
				cellContent5.setCellStyle(cellStyle);
				cellContent5.setCellValue((Double) map.get("amountZ2"));
				
				HSSFCell cellContent6 = rowContent1.createCell(5);
				cellContent6.setCellStyle(cellStyle);
				cellContent6.setCellValue((Long) map.get("totalZ3"));
				
				HSSFCell cellContent7 = rowContent1.createCell(6);
				cellContent7.setCellStyle(cellStyle);
				cellContent7.setCellValue((Double) map.get("amountZ3"));
				
				HSSFCell cellContent8 = rowContent1.createCell(7);
				cellContent8.setCellStyle(cellStyle);
				cellContent8.setCellValue((Long) map.get("totalZ4"));
				
				HSSFCell cellContent9 = rowContent1.createCell(8);
				cellContent9.setCellStyle(cellStyle);
				cellContent9.setCellValue((Double) map.get("amountZ4"));
				
				HSSFCell cellContent10 = rowContent1.createCell(9);
				cellContent10.setCellStyle(cellStyle);
				cellContent10.setCellValue((Long) map.get("totalZ5"));
				
				HSSFCell cellContent11 = rowContent1.createCell(10);
				cellContent11.setCellStyle(cellStyle);
				cellContent11.setCellValue((Double) map.get("amountZ5"));
				
				HSSFCell cellContent12 = rowContent1.createCell(11);
				cellContent12.setCellStyle(cellStyle);
				cellContent12.setCellValue((Long) map.get("totalZ6"));
				
				HSSFCell cellContent13 = rowContent1.createCell(12);
				cellContent13.setCellStyle(cellStyle);
				cellContent13.setCellValue((Double) map.get("amountZ6"));

				HSSFCell cellContent14 = rowContent1.createCell(13);
				cellContent14.setCellStyle(cellStyle);
				cellContent14.setCellValue((Long) map.get("totalZ7"));
				
				HSSFCell cellContent15 = rowContent1.createCell(14);
				cellContent15.setCellStyle(cellStyle);
				cellContent15.setCellValue((Double) map.get("amountZ7"));
				
				HSSFCell cellContent16 = rowContent1.createCell(15);
				cellContent16.setCellStyle(cellStyle);
				cellContent16.setCellValue((Long) map.get("totalZ8"));
				
				HSSFCell cellContent17 = rowContent1.createCell(16);
				cellContent17.setCellStyle(cellStyle);
				cellContent17.setCellValue((Double) map.get("amountZ8"));
				
				HSSFCell cellContent18 = rowContent1.createCell(17);
				cellContent18.setCellStyle(cellStyle);
				cellContent18.setCellValue((Long) map.get("totalZ9"));
				
				HSSFCell cellContent19 = rowContent1.createCell(18);
				cellContent19.setCellStyle(cellStyle);
				cellContent19.setCellValue((Double) map.get("amountZ9"));
				
				HSSFCell cellContent20 = rowContent1.createCell(19);
				cellContent20.setCellStyle(cellStyle);
				cellContent20.setCellValue((Long) map.get("total"));
				
				HSSFCell cellContent21 = rowContent1.createCell(20);
				cellContent21.setCellStyle(cellStyle);
				cellContent21.setCellValue((Double) map.get("amount"));
				
				
				
				
			}
		}
		return workbook;
	}

 

你可能感兴趣的:(javaPoi导出excel并设置多级表头)