pom.xml
<!--Excel導出-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
QuestionExcelView.java
package com.zillion.cloud.api.knowledge.service.impl;
import com.zillion.cloud.api.knowledge.model.QuestionVo;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.springframework.web.servlet.view.document.AbstractXlsView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
public class QuestionExcelView extends AbstractXlsView {
@Override
protected void buildExcelDocument(Map<String, Object> map, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
// change the file name
response.setHeader("Content-Disposition", "attachment; filename=\"questions.xls\"");
List<QuestionVo> questions = (List<QuestionVo>)map.get("questions");
// create excel xls sheet
Sheet sheet = workbook.createSheet("AC Question");
sheet.setDefaultColumnWidth(30);
// create style for header cells
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontName("Arial");
style.setFillForegroundColor(HSSFColor.BLUE.index);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
font.setBold(true);
font.setColor(HSSFColor.WHITE.index);
style.setFont(font);
// create header row
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("Case ID");
header.getCell(0).setCellStyle(style);
header.createCell(1).setCellValue("SSA的疑問");
header.getCell(1).setCellStyle(style);
header.createCell(2).setCellValue("SME的解答");
header.getCell(2).setCellStyle(style);
header.createCell(3).setCellValue("站點");
header.getCell(3).setCellStyle(style);
header.createCell(4).setCellValue("技能");
header.getCell(4).setCellStyle(style);
header.createCell(5).setCellValue("分類");
header.getCell(5).setCellStyle(style);
header.createCell(6).setCellValue("涉及到的知識點");
header.getCell(6).setCellStyle(style);
int rowCount = 1;
for(QuestionVo temp : questions){
Row userRow = sheet.createRow(rowCount++);
userRow.createCell(0).setCellValue(temp.getCaseId());
userRow.createCell(1).setCellValue(temp.getQuestion());
userRow.createCell(2).setCellValue(temp.getAnswer());
userRow.createCell(3).setCellValue(temp.getSiteName());
userRow.createCell(4).setCellValue(temp.getSkillName());
userRow.createCell(5).setCellValue(temp.getCatalogName());
userRow.createCell(6).setCellValue(temp.getPoint());
}
}
}
Controller
@RequestMapping("/excel")
public ModelAndView download(Question queryK, HttpServletRequest request){
List<QuestionVo> questions = questionService.listVo(queryK,"id","asc");//內容
QuestionExcelView excelView = new QuestionExcelView();
Map<String, Object> map = new HashMap<String, Object>();
map.put("questions", questions);
return new ModelAndView(excelView,map);
}