java poi导出excel合并单元格

导出效果

代码:

import java.math.BigDecimal;

public class CwmonthlyPlanSub0Bean {
	/**
	 * 主键
	 */
	private Long id;
	/**
	 * 付款类
	 */
	private String fkl;
	/**
	 * 付款事项
	 */
	private String fksx;
	
	/**
	 * 本期预算金额
	 */
	private BigDecimal bqysje;
	
	
/**
 * 
 * 电汇金额
 */
	private BigDecimal dhje;
/**
 * 
 
 */
	private BigDecimal cdje;
	private String remarks;
	public CwmonthlyPlanSub0Bean(Long id, String fkl, String fksx, BigDecimal bqysje, BigDecimal dhje,BigDecimal cdje, String remarks) {
		super();
		this.id = id;
		this.fkl = fkl;
		this.fksx = fksx;
		
		this.bqysje = bqysje;
		
		this.dhje=dhje;
		this.cdje=cdje;
		this.remarks=remarks;
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the fkl
	 */
	public String getFkl() {
		return fkl;
	}

	/**
	 * @param fkl the fkl to set
	 */
	public void setFkl(String fkl) {
		this.fkl = fkl;
	}

	/**
	 * @return the fksx
	 */
	public String getFksx() {
		return fksx;
	}

	/**
	 * @param fksx the fksx to set
	 */
	public void setFksx(String fksx) {
		this.fksx = fksx;
	}

	

	/**
	 * @return the bqysje
	 */
	public BigDecimal getBqysje() {
		return bqysje;
	}

	/**
	 * @param bqysje the bqysje to set
	 */
	public void setBqysje(BigDecimal bqysje) {
		this.bqysje = bqysje;
	}


	/**
	 * @return the dhje
	 */
	public BigDecimal getDhje() {
		return dhje;
	}

	/**
	 * @param dhje the dhje to set
	 */
	public void setDhje(BigDecimal dhje) {
		this.dhje = dhje;
	}

	/**
	 * @return the cdje
	 */
	public BigDecimal getCdje() {
		return cdje;
	}

	/**
	 * @param cdje the cdje to set
	 */
	public void setCdje(BigDecimal cdje) {
		this.cdje = cdje;
	}

	/**
	 * @return the remarks
	 */
	public String getRemarks() {
		return remarks;
	}

	/**
	 * @param remarks the remarks to set
	 */
	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}




	
}
public static void main(String args[]) {
		// 模拟部分数据
		List<CwmonthlyPlanSub0Bean> detail = new ArrayList<CwmonthlyPlanSub0Bean>();
		// MonthlyFundingEntity entity=dao.get
		CwmonthlyPlanSub0Bean d1 = new CwmonthlyPlanSub0Bean(1L, "人工", "工资", new BigDecimal("4.675"),
				new BigDecimal("4.675"), new BigDecimal("4.675"), "555");
		CwmonthlyPlanSub0Bean d2 = new CwmonthlyPlanSub0Bean(2L, "人工", "福利费", new BigDecimal("44.675"),
				new BigDecimal("4.675"), new BigDecimal("4.675"), "66");
		CwmonthlyPlanSub0Bean d3 = new CwmonthlyPlanSub0Bean(3L, "原料", "电铜", new BigDecimal("6.675"),
				new BigDecimal("4.675"), new BigDecimal("4.675"), "77");
		CwmonthlyPlanSub0Bean d4 = new CwmonthlyPlanSub0Bean(4L, "原料", "绿柱石", new BigDecimal("7.675"),
				new BigDecimal("4.675"), new BigDecimal("4.675"), "88");
		CwmonthlyPlanSub0Bean d5 = new CwmonthlyPlanSub0Bean(5L, "燃料动力", "测试", new BigDecimal("8.675"),
				new BigDecimal("4.675"), new BigDecimal("4.675"), "999");
		detail.add(d1);
		detail.add(d2);
		detail.add(d3);
		detail.add(d4);
		detail.add(d5);

		try {
			FileOutputStream fout = new FileOutputStream("D:/data/students.xls");
			ExportExcels(detail, fout);
			fout.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
public static void ExportExcels(List<CwmonthlyPlanSub0Bean> detail, FileOutputStream fout) {

		try {
			// 1.创建工作簿
			HSSFWorkbook workbook = new HSSFWorkbook();
			// 1.1创建合并单元格对象
			CellRangeAddress callRangeAddress1 = new CellRangeAddress(0, 0, 0, 5);// 起始行,结束行,起始列,结束列
			// 填报部门
			CellRangeAddress callRangeAddress2 = new CellRangeAddress(1, 1, 0, 1);// 起始行,结束行,起始列,结束列
			// 编制时间:
			CellRangeAddress callRangeAddress3 = new CellRangeAddress(1, 1, 2, 4);// 起始行,结束行,起始列,结束列
		
			// 项目
			CellRangeAddress callRangeAddress5 = new CellRangeAddress(2, 3, 0, 0);// 起始行,结束行,起始列,结束列
			
			// 核算内容
			CellRangeAddress callRangeAddress6 = new CellRangeAddress(2, 3, 1, 1);// 起始行,结束行,起始列,结束列
			// 本期预算金额
			CellRangeAddress callRangeAddress7 = new CellRangeAddress(2, 3, 2, 2);// 起始行,结束行,起始列,结束列

			// 付款方式
			CellRangeAddress callRangeAddress8 = new CellRangeAddress(2, 2, 3, 4);// 起始行,结束行,起始列,结束列
			// 备注
			CellRangeAddress callRangeAddress9 = new CellRangeAddress(2, 3, 5, 5);// 起始行,结束行,起始列,结束列

			// 经办人、部门负责人
			CellRangeAddress callRangeAddressPersion1 = new CellRangeAddress(detail.size() + 5, detail.size() + 5 + 1,
					0, 2);// 起始行,结束行,起始列,结束列
			CellRangeAddress callRangeAddressPersion2 = new CellRangeAddress(detail.size() + 5, detail.size() + 5 + 1,
					3, 5);// 起始行,结束行,起始列,结束列

			// 样式配置start
			// title
			HSSFCellStyle erStyle = createCellStyle(workbook, (short) 13, true, true);
			// 项目名称和时间
			HSSFCellStyle sanStyle = createCellStyle(workbook, (short) 10, false, false);
			// 标题样式
			HSSFCellStyle colStyle = createCellStyle(workbook, (short) 10, true, true);
			// 内容样式
			HSSFCellStyle cellStyle = createCellStyle(workbook, (short) 10, false, true);
			// 2.创建工作表
			HSSFSheet sheet = workbook.createSheet("2024年2月份部门支出预算明细表");
			sheet.autoSizeColumn(1);
			sheet.setColumnWidth(1, sheet.getColumnWidth(1) * 50 / 10);

			// 2.1加载合并单元格对象
			sheet.addMergedRegion(callRangeAddress1);
			sheet.addMergedRegion(callRangeAddress2);
			sheet.addMergedRegion(callRangeAddress3);
			

			sheet.addMergedRegion(callRangeAddress5);
			
			sheet.addMergedRegion(callRangeAddress6);
			sheet.addMergedRegion(callRangeAddress7);
			sheet.addMergedRegion(callRangeAddress8);
			sheet.addMergedRegion(callRangeAddress9);
			sheet.addMergedRegion(callRangeAddressPersion1);
			sheet.addMergedRegion(callRangeAddressPersion2);

			// 设置默认列宽
			sheet.setDefaultColumnWidth(15);
			// 3.创建行
			// 3.1创建头标题行;并且设置头标题
			HSSFRow rower = sheet.createRow(0);
			HSSFCell celler = rower.createCell(0);
			// 加载单元格样式
			celler.setCellStyle(erStyle);
			celler.setCellValue("2024年2月份部门支出预算明细表");

			// 创建第二行
			HSSFRow rowsan = sheet.createRow(1);
			HSSFCell cellsan = rowsan.createCell(0);
			HSSFCell cellsan1 = rowsan.createCell(2);
			HSSFCell cellsan2 = rowsan.createCell(5);
			// 加载单元格样式
			cellsan.setCellStyle(sanStyle);
			cellsan.setCellValue("填报部门:333");
			cellsan1.setCellStyle(sanStyle);
			cellsan1.setCellValue("编制时间:2017年 10月 20日");
			cellsan2.setCellStyle(sanStyle);
			cellsan2.setCellValue(" 单位:万元");

			// 3.2创建列标题;并且设置列标题
			HSSFRow row2 = sheet.createRow(2);
		
			
			String[] titles = { "项目", "核算内容", "本期预算金额", "付款方式", "", "备注" };// ""为占位字符串
			for (int i = 0; i < titles.length; i++) {
				HSSFCell cell2 = row2.createCell(i);
				// 加载单元格样式
				
				colStyle.setBorderTop(BorderStyle.THIN);
				colStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
				colStyle.setBorderBottom(BorderStyle.THIN);
				colStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
				colStyle.setBorderLeft(BorderStyle.THIN);
				colStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
				colStyle.setBorderRight(BorderStyle.THIN);
				colStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
				cell2.setCellStyle(colStyle);
				cell2.setCellValue(titles[i]);

			}

			HSSFRow rowfour = sheet.createRow(3);
			String[] titlefour = {  "电汇", "承兑" };
			for (int i = 0; i < titlefour.length; i++) {
				HSSFCell cell2 = rowfour.createCell(i + 3);
				// 加载单元格样式
				cell2.setCellStyle(colStyle);
				colStyle.setBorderTop(BorderStyle.THIN);
				colStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
				colStyle.setBorderBottom(BorderStyle.THIN);
				colStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
				colStyle.setBorderLeft(BorderStyle.THIN);
				colStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
				colStyle.setBorderRight(BorderStyle.THIN);
				colStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
				cell2.setCellValue(titlefour[i]);
			}

			// 招标人代表
			HSSFRow rowpersion = sheet.createRow(detail.size() + 5);
			HSSFCell cellpersion = rowpersion.createCell(0);
			HSSFCell cellpersion1 = rowpersion.createCell(3);

			// 加载单元格样式
			cellpersion.setCellStyle(sanStyle);
			cellpersion.setCellValue("经办人:");
			cellpersion1.setCellStyle(sanStyle);
			cellpersion1.setCellValue("部门负责人:");
			BigDecimal count = new BigDecimal(0);
			BigDecimal dhCount = new BigDecimal(0);
			BigDecimal cdCount = new BigDecimal(0);
			// 4.操作单元格;将用户列表写入excel
			if (detail != null) {
				int i = 1;
				for (int j = 0; j < detail.size(); j++) {
					// 创建数据行,前面有两行,头标题行和列标题行
					HSSFRow row3 = sheet.createRow(j + 4);
					HSSFCell cell0 = row3.createCell(0);
					cell0.setCellStyle(cellStyle);
					cellStyle.setBorderTop(BorderStyle.THIN);
					cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
					cellStyle.setBorderBottom(BorderStyle.THIN);
					cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
					cellStyle.setBorderLeft(BorderStyle.THIN);
					cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
					cellStyle.setBorderRight(BorderStyle.THIN);
					cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
					cell0.setCellValue(detail.get(j).getFkl());

					HSSFCell cell1 = row3.createCell(1);
					cell1.setCellStyle(cellStyle);
					cell1.setCellValue(detail.get(j).getFksx());

					HSSFCell cell2 = row3.createCell(2);
					cell2.setCellStyle(cellStyle);
					cell2.setCellValue(String.valueOf(detail.get(j).getBqysje()));

					HSSFCell cell3 = row3.createCell(3);
					cell3.setCellStyle(cellStyle);
					cell3.setCellValue(String.valueOf(detail.get(j).getDhje()));
					HSSFCell cell4 = row3.createCell(4);
					cell4.setCellStyle(cellStyle);

					cell4.setCellValue(String.valueOf(detail.get(j).getCdje()));
					count = count.add(detail.get(j).getBqysje());
					dhCount = dhCount.add(detail.get(j).getDhje());
					cdCount = cdCount.add(detail.get(j).getCdje());
					HSSFCell cell5 = row3.createCell(5);
					cell5.setCellStyle(cellStyle);
					cell5.setCellValue(detail.get(j).getRemarks());

				}
			}
			// 合计
			HSSFRow hssfRow = sheet.createRow(detail.size() + 4);
			HSSFCell hssfCell = hssfRow.createCell(0);
			HSSFCell hssfCel0 = hssfRow.createCell(1);
			HSSFCell hssfCel2 = hssfRow.createCell(2);
			HSSFCell hssfCel3 = hssfRow.createCell(3);
			HSSFCell hssfCel4 = hssfRow.createCell(4);
			HSSFCell hssfCel5 = hssfRow.createCell(5);
			// 加载单元格样式
			HSSFCellStyle totleStyle = createCellStyle(workbook, (short) 10, true, true);
			hssfCell.setCellStyle(totleStyle);
			totleStyle.setAlignment(HorizontalAlignment.CENTER);// 水平居中
			totleStyle.setBorderTop(BorderStyle.THIN);
			totleStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
			totleStyle.setBorderBottom(BorderStyle.THIN);
			totleStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
			totleStyle.setBorderLeft(BorderStyle.THIN);
			totleStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
			totleStyle.setBorderRight(BorderStyle.THIN);
			totleStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
			hssfCell.setCellValue("合计:");
			HSSFCellStyle countStyle = createCellStyle(workbook, (short) 10, true, true);
			countStyle.setBorderTop(BorderStyle.THIN);
			countStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
			countStyle.setBorderBottom(BorderStyle.THIN);
			countStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
			countStyle.setBorderLeft(BorderStyle.THIN);
			countStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
			countStyle.setBorderRight(BorderStyle.THIN);
			countStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
			hssfCel2.setCellStyle(countStyle);
			hssfCel2.setCellValue(String.valueOf(count));
			hssfCel3.setCellStyle(countStyle);
			hssfCel3.setCellValue(String.valueOf(dhCount));
			hssfCel4.setCellStyle(countStyle);
			hssfCel4.setCellValue(String.valueOf(cdCount));
			hssfCel5.setCellStyle(countStyle);
			hssfCel5.setCellValue("");
			hssfCel0.setCellStyle(countStyle);
			hssfCel0.setCellValue("");
			
			setBorderStyle(BorderStyle.THIN,callRangeAddress5, sheet);
			setBorderStyle(BorderStyle.THIN,callRangeAddress6, sheet);
			setBorderStyle(BorderStyle.THIN,callRangeAddress7, sheet);
			setBorderStyle(BorderStyle.THIN,callRangeAddress8, sheet);
			setBorderStyle(BorderStyle.THIN,callRangeAddress9, sheet);
			// 5.输出
			workbook.write(fout);
//             workbook.close();
			// out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	public static void  setBorderStyle(BorderStyle border,CellRangeAddress region,HSSFSheet sheet){
		RegionUtil.setBorderBottom(BorderStyle.THIN,region, sheet);
		RegionUtil.setBorderLeft(BorderStyle.THIN,region, sheet);
		RegionUtil.setBorderRight(BorderStyle.THIN,region, sheet);
		RegionUtil.setBorderTop(BorderStyle.THIN,region, sheet);
	}

	/**
	 * @param workbook
	 * @param fontsize
	 * @return 单元格样式
	 */
	private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontsize, boolean flag, boolean flag1) {
		// TODO Auto-generated method stub
		HSSFCellStyle style = workbook.createCellStyle();
		// 是否水平居中
		if (flag1) {
			style.setAlignment(HorizontalAlignment.CENTER);// 水平居中
		}
		style.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
		// 创建字体
		HSSFFont font = workbook.createFont();
		// 是否加粗字体
		if (flag) {
			font.setBold(true);
		}
		font.setFontHeightInPoints(fontsize);
		// 加载字体
		style.setFont(font);
		return style;
	}

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值