java中利用poi工具生成自定义的复杂人员信息表并excel导出

您所在的位置:网站首页 佛系的定义意思 java中利用poi工具生成自定义的复杂人员信息表并excel导出

java中利用poi工具生成自定义的复杂人员信息表并excel导出

2023-06-28 13:41| 来源: 网络整理| 查看: 265

java中利用poi工具生成复杂的人员信息表并excel导出 概要

话不多说,直接上效果图

在这里插入图片描述 在这里插入图片描述

poi包

引用需要用到的包,可自行搜索依赖

import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.util.CellRangeAddress; 主体代码 OutputStream outputStream = response.getOutputStream(); try { String values = request.getParameter("values"); if(StringUtil.isNotNull(values)){ //业务数据对象 HrStaffPersonInfo info = (HrStaffPersonInfo) getServiceImp(request).findByPrimaryKey(values); // 自定义创建excel HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("信息表1"); HSSFSheet sheet2 = wb.createSheet("信息表2"); //设置宽度(多少列) int[] width = {13, 13, 13, 13, 13, 13, 13, 13, 13, 13}; for (int columnIndex = 0; columnIndex e.printStackTrace(); }finally { outputStream.flush(); outputStream.close(); }

下面是开始画页签数据,具体的业务代码就不展现了

private void setMess(HSSFSheet sheet, Map styles, HrStaffPersonInfo info) throws Exception { //添加标题行 HSSFRow row0 = sheet.createRow(0); row0.setHeight((short) (50 * 20)); HSSFCell cell0 = row0.createCell(0); cell0.setCellStyle(styles.get("title")); String cellValues0 = "人员信息采集表"; cell0.setCellValue(cellValues0); CellRangeAddress cra = new CellRangeAddress(0, 0, 0, 9); sheet.addMergedRegion(cra); //添加1-4行数据 for(int i=1;i Map styles = new HashMap(); //设置字体 HSSFFont headFont = wb.createFont(); headFont.setFontName("微软雅黑"); headFont.setFontHeightInPoints((short) 16); headFont.setBold(true); HSSFFont bodyFont = wb.createFont(); bodyFont.setFontName("微软雅黑"); bodyFont.setFontHeightInPoints((short) 10); //标题行样式 HSSFCellStyle style = wb.createCellStyle(); style.setFont(headFont); style.setWrapText(true); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); styles.put("title", style); //数据头居中样式 style = wb.createCellStyle(); style.setFont(bodyFont); style.setWrapText(true); //设置字体水平和垂直居中,jar包版本比较低,新版本jar包放入的类型可自行上网搜索 style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); style.setFillForegroundColor((short) 27); //设置边框,jar包版本比较低,新版本jar包放入的类型可自行上网搜索 style.setBorderTop(org.apache.poi.ss.usermodel.BorderStyle.THIN); style.setBorderBottom(org.apache.poi.ss.usermodel.BorderStyle.THIN); style.setBorderLeft(org.apache.poi.ss.usermodel.BorderStyle.THIN); style.setBorderRight(org.apache.poi.ss.usermodel.BorderStyle.THIN); style.setTopBorderColor(IndexedColors.BLACK.index); style.setBottomBorderColor(IndexedColors.BLACK.index); style.setLeftBorderColor(IndexedColors.BLACK.index); style.setRightBorderColor(IndexedColors.BLACK.index); styles.put("header_center", style); //数据头居右样式 style = wb.createCellStyle(); style.setFont(bodyFont); style.setWrapText(true); style.setAlignment(HorizontalAlignment.RIGHT); style.setFillForegroundColor((short) 27); style.setBorderTop(org.apache.poi.ss.usermodel.BorderStyle.THIN); style.setBorderBottom(org.apache.poi.ss.usermodel.BorderStyle.THIN); style.setBorderLeft(org.apache.poi.ss.usermodel.BorderStyle.THIN); style.setBorderRight(org.apache.poi.ss.usermodel.BorderStyle.THIN); style.setTopBorderColor(IndexedColors.BLACK.index); style.setBottomBorderColor(IndexedColors.BLACK.index); style.setLeftBorderColor(IndexedColors.BLACK.index); style.setRightBorderColor(IndexedColors.BLACK.index); styles.put("header_right", style); return styles; } 小结

因为这个需求给到的excel表,字段太多,且都是不同的字段,没有太多相同业务能封装代码,也可能是时间太紧迫,只用了一天半把这个需求从0实现完,导致代码多半都是业务代码拼装出来的,业务代码就不贴上来了,没太大意义 总结的话: 只需要了解poi包里的几个核心方法就行 1.设置单元格样式,包括高度,宽度,字体样式,边框样式等等 2.合并单元格,(开始行,结束行,开始列,结束列)这样的规则去合并,就可以合并成自己需要的样子了



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3