标签:apach ima reg app public weight src null ica
今天碰到一个需求,要求将用户回答的问卷及问题导出Excel表格,问卷对应的问题数量不一致,需要动态添加列表头,简单记录。
要导出Excel需要添加poi.jar包
用户-问卷实体(固定列):
package com.lwl.bean; import com.util.annotation.BeanField; import lombok.Data; import java.sql.Timestamp; import java.util.List; /** * 问卷实体(用于导出excel) * @author linwenli */ @Data public class HyMktUserQuesBean { @BeanField("用户名") private String wechatName; @BeanField("联系电话") private String telephone; @BeanField("主题名称") private String questionName; @BeanField("参与时间") private Timestamp createTime; @BeanField("问题内容") private List<HyMktUserQuesAnswerBean> hyMktUserQuesAnswerBeans; }
用户-问卷问题实体(动态列):
package com.lwl.bean; import com.util.annotation.BeanField; import lombok.Data; /** * 问题及用户答案 * @author linwenli */ @Data public class HyMktUserQuesAnswerBean { @BeanField("问题名称") private String problemName; @BeanField("答案") private String optionName; }
导出方法:
package com.lwl.util; import com.lwl.bean.HyMktUserQuesAnswerBean; import com.lwl.bean.HyMktUserQuesBean; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.util.List; public class ExcelUtil { /** * 根据List<HyMktUserQuesBean> 导出数据到Excel * @author linwenli * @date 2019/5/09 15:27 * @param response * @param fileName * @param hyMktUserQuesBeans * @throws IOException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static void writeExcel(HttpServletResponse response, String fileName, List<HyMktUserQuesBean> hyMktUserQuesBeans) throws IOException, IllegalArgumentException, IllegalAccessException { HSSFWorkbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet(); // 数据表头开始行 CellStyle style = wb.createCellStyle(); Font font = wb.createFont(); font.setFontName("宋体"); // 设置字体大小 font.setFontHeightInPoints((short) 12); // 加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 设置背景色 style.setFillForegroundColor(HSSFColor.LIME.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 让单元格居中 style.setAlignment(HSSFCellStyle.SOLID_FOREGROUND); // 左右居中 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 上下居中 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); style.setWrapText(true);//设置自动换行 style.setFont(font); // 添加表数据 // 取出列表中问卷问题最多的对象做动态表头 HyMktUserQuesBean problemMax = null; int maxSize = 0; for (int n = 0; n < hyMktUserQuesBeans.size(); n++) { HyMktUserQuesBean hyMktUserQuesBean = hyMktUserQuesBeans.get(n); // 记录最大问题个数 if (hyMktUserQuesBean.getHyMktUserQuesAnswerBeans().size() > maxSize) { maxSize = hyMktUserQuesBean.getHyMktUserQuesAnswerBeans().size(); problemMax = hyMktUserQuesBean; } int index = 0; // 写excel数据 for (int i = 1; i < hyMktUserQuesBean.getHyMktUserQuesAnswerBeans().size(); i++) { // 第零行为表头行,不填充数据 Row row = sheet.createRow(n + 1); // 用户名 Cell firstCell = row.createCell(index); firstCell.setCellType(Cell.CELL_TYPE_STRING); firstCell.setCellValue(hyMktUserQuesBean.getWechatName()); sheet.autoSizeColumn((short) index++);// 设置单元格自适应 // 联系电话 Cell secondCell = row.createCell(index); secondCell.setCellType(Cell.CELL_TYPE_STRING); secondCell.setCellValue(hyMktUserQuesBean.getTelephone()); sheet.autoSizeColumn((short) index++);// 设置单元格自适应 // 主题名称 Cell thirdCell = row.createCell(index); thirdCell.setCellType(Cell.CELL_TYPE_STRING); thirdCell.setCellValue(hyMktUserQuesBean.getQuestionName()); sheet.autoSizeColumn((short) index++);// 设置单元格自适应 // 参与时间 Cell forthCell = row.createCell(index); forthCell.setCellType(Cell.CELL_TYPE_STRING); forthCell.setCellValue(DateUtil.translateDate(hyMktUserQuesBean.getCreateTime().getTime())); sheet.autoSizeColumn((short) index++);// 设置单元格自适应 // 动态表头 List<HyMktUserQuesAnswerBean> hyMktUserQuesAnswerBeans = hyMktUserQuesBean.getHyMktUserQuesAnswerBeans(); for(int k = 0; k < hyMktUserQuesAnswerBeans.size(); k++ ){ // 问题 Cell otherOneCell = row.createCell(index); otherOneCell.setCellType(Cell.CELL_TYPE_STRING); otherOneCell.setCellValue(hyMktUserQuesAnswerBeans.get(k).getProblemName()); sheet.autoSizeColumn((short) index++);// 设置单元格自适应 // 答案 Cell otherTwoCell = row.createCell(index); otherTwoCell.setCellType(Cell.CELL_TYPE_STRING); otherTwoCell.setCellValue(hyMktUserQuesAnswerBeans.get(k).getOptionName()); sheet.autoSizeColumn((short) index++);// 设置单元格自适应 } } } //添加表头 Row row = sheet.createRow(0); int index = 0; // 用户名 Cell indexCell = row.createCell(index); indexCell.setCellType(Cell.CELL_TYPE_STRING); indexCell.setCellStyle(style);//设置表头样式 indexCell.setCellValue("用户名"); sheet.autoSizeColumn((short) index++);// 设置单元格自适应 // 联系电话 Cell indexCell2 = row.createCell(index); indexCell2.setCellType(Cell.CELL_TYPE_STRING); indexCell2.setCellStyle(style);//设置表头样式 indexCell2.setCellValue("联系电话"); sheet.autoSizeColumn((short) index++);// 设置单元格自适应 // 主题名称 Cell indexCell3 = row.createCell(index); indexCell3.setCellType(Cell.CELL_TYPE_STRING); indexCell3.setCellStyle(style);//设置表头样式 indexCell3.setCellValue("主题名称"); sheet.autoSizeColumn((short) index++);// 设置单元格自适应 // 参与时间 Cell indexCell4 = row.createCell(index); indexCell4.setCellType(Cell.CELL_TYPE_STRING); indexCell4.setCellStyle(style);//设置表头样式 indexCell4.setCellValue("参与时间"); sheet.autoSizeColumn((short) index++);// 设置单元格自适应 for(int j = 0; j < problemMax.getHyMktUserQuesAnswerBeans().size(); j++ ){ // 问题 Cell otherOneCell = row.createCell(index); otherOneCell.setCellType(Cell.CELL_TYPE_STRING); otherOneCell.setCellStyle(style);//设置表头样式 otherOneCell.setCellValue("问题" + (j + 1)); sheet.autoSizeColumn((short) index++);// 设置单元格自适应 // 答案 Cell otherTwoCell = row.createCell(index); otherTwoCell.setCellType(Cell.CELL_TYPE_STRING); otherTwoCell.setCellStyle(style);//设置表头样式 otherTwoCell.setCellValue("问题" + (j + 1) + "答案"); sheet.autoSizeColumn((short) index++);// 设置单元格自适应 } response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=\"" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".xls" + "\""); OutputStream ouputStream = null; try { ouputStream = response.getOutputStream(); wb.write(ouputStream); } finally { ouputStream.close(); } } }
导出结果:
标签:apach ima reg app public weight src null ica
原文地址:https://www.cnblogs.com/new-life/p/10840394.html