最近的公司需求,因为Excel表头样式较为复杂,不易直接用poi写出。
需要的Excel为这种:
直接模板导出不能成为这样。
public void exportCheckCsdn(HttpServletResponse response) {//获取到MNR 和 MNR-DT 的List// 此处写 获取到指定list 的语句List<MnrExcelDTO> mnrExcelDTOS = new ArrayList<>();//sheet2,如果是单个sheet,不需要这个List<MnrDtExcelDTO> mnrDtExcelDTOS = new ArrayList<>();
// list赋值//excel模板路径File fi = null;try {
// 这边写的模板在resources下的 templates 下fi = ResourceUtils.getFile("classpath:templates/MNR_Check_Result.xlsx");} catch (FileNotFoundException e) {e.printStackTrace();}XSSFWorkbook wb = null;try {//读取excel模板wb = new XSSFWorkbook(new FileInputStream(fi));} catch (IOException e) {e.printStackTrace();}XSSFSheet mnrSheet = wb.getSheetAt(0);XSSFSheet mnrSheetDt = wb.getSheetAt(1);//从第二行开始 sheet1的 ,因为第一行为模板for (int i = 2; i < mnrExcelDTOS.size() + 2; i++) {//获取行数据//根据字段名获取对应行数据MnrExcelDTO rowData = mnrExcelDTOS.get(i - 2);Row row = mnrSheet.getRow(i);if (row == null) {// 创建新行对象row = mnrSheet.createRow(i);}for (int j = 0; j < MnrExcelDTO.class.getDeclaredFields().length; j++) {// 获取当前单元格Cell cell = row.getCell(j);if (cell == null) {// 如果单元格不存在,创建一个新的单元格对象cell = row.createCell(j);}// 获取对应单元格的字段名称Field field = MnrExcelDTO.class.getDeclaredFields()[j];String fieldName = field.getName();// 获取字段对应的 getter 方法Method method = null;String cellValue = "";try {//获取get方法method = rowData.getClass().getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));// 调用 getter 方法获取字段的属性值Object value = method.invoke(rowData);if (null == value) {cellValue = "";} else {// 将值转换为字符串类型,并设置到单元格中cellValue = String.valueOf(value);}cell.setCellValue(cellValue);} catch (InvocationTargetException e) {e.printStackTrace();} catch (NoSuchMethodException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}CellStyle redCellStyle = null;Object value = "";try {
// 如果字段名称不等于ckStatus,则设置单元格的样式if (!"ckStatus".equals(fieldName)) {value = method.invoke(rowData);// 将值转换为字符串类型,并设置到单元格中cellValue = String.valueOf(value);cell.setCellValue(cellValue);}} catch (InvocationTargetException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}redCellStyle = wb.createCellStyle();// 重点:从现有样式克隆style,只修改Font,其它style不变redCellStyle.cloneStyleFrom(cell.getCellStyle());// 获取原有字体Font oldFont = wb.getFontAt(redCellStyle.getFontIndexAsInt());// 创建新字体Font redFont = wb.createFont();// 重点:保留原字体样式redFont.setFontName(oldFont.getFontName()); // 保留原字体redFont.setFontHeightInPoints(oldFont.getFontHeightInPoints()); // 保留原字体高度redFont.setBold(true); // 加粗redFont.setColor(IndexedColors.RED.getIndex()); // 字体颜色:红色// 设置红色字体redCellStyle.setFont(redFont);// 设置样式cell.setCellStyle(redCellStyle);}}//从第二行开始 sheet2的 ,因为第一行为模板 同上for (int i = 2; i < mnrDtExcelDTOS.size() + 2; i++) {//获取行数据//根据字段名获取对应行数据MnrDtExcelDTO rowData = mnrDtExcelDTOS.get(i - 2);Row row = mnrSheet.getRow(i);if (row == null) {// 创建新行对象row = mnrSheet.createRow(i);}for (int j = 0; j < MnrExcelDTO.class.getDeclaredFields().length; j++) {// 获取当前单元格Cell cell = row.getCell(j);if (cell == null) {// 如果单元格不存在,创建一个新的单元格对象cell = row.createCell(j);}// 获取对应单元格的字段名称Field field = MnrExcelDTO.class.getDeclaredFields()[j];String fieldName = field.getName();// 获取字段对应的 getter 方法Method method = null;String cellValue = "";try {//获取get方法method = rowData.getClass().getMethod("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1));// 调用 getter 方法获取字段的属性值Object value = method.invoke(rowData);if (null == value) {cellValue = "";} else {// 将值转换为字符串类型,并设置到单元格中cellValue = String.valueOf(value);}cell.setCellValue(cellValue);} catch (InvocationTargetException e) {e.printStackTrace();} catch (NoSuchMethodException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}CellStyle redCellStyle = null;Object value = "";try {
// 如果字段名称不等于ckStatus,则设置单元格的样式//也可以加上其他的判断,比如字符串末尾有 er 字符后缀,表示处理失败,给他变色即可if (!"ckStatus".equals(fieldName)) {value = method.invoke(rowData);// 将值转换为字符串类型,并设置到单元格中cellValue = String.valueOf(value);cell.setCellValue(cellValue);}} catch (InvocationTargetException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}redCellStyle = wb.createCellStyle();// 重点:从现有样式克隆style,只修改Font,其它style不变redCellStyle.cloneStyleFrom(cell.getCellStyle());// 获取原有字体Font oldFont = wb.getFontAt(redCellStyle.getFontIndexAsInt());// 创建新字体Font redFont = wb.createFont();// 重点:保留原字体样式redFont.setFontName(oldFont.getFontName()); // 保留原字体redFont.setFontHeightInPoints(oldFont.getFontHeightInPoints()); // 保留原字体高度redFont.setBold(true); // 加粗redFont.setColor(IndexedColors.RED.getIndex()); // 字体颜色:红色// 设置红色字体redCellStyle.setFont(redFont);// 设置样式cell.setCellStyle(redCellStyle);}}ServletOutputStream outputStream = null;try {ExcelUtil.setResponse(response, "userName-today-check");outputStream = response.getOutputStream();wb.write(outputStream);outputStream.flush();outputStream.close();wb.close();} catch (IOException e) {e.printStackTrace();}}