今天碰到一個需求,要求將用戶回答的問卷及問題導出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(); } } }
導出結果: