package com.ifish.excel; import com.ifish.entity.VipCompany; import com.ifish.entity.VipMember; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.springframework.web.servlet.view.document.AbstractExcelView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; import java.net.URLEncoder; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Map; /** * @author: yan.y * @Description: * @Date: Created in 23:59 2018/7/10 * @Modified by: */ public class VipCompanyExportExcel extends AbstractExcelView{ @Override protected void buildExcelDocument(Map model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { //表头样式 HSSFCellStyle style = workbook.createCellStyle(); //字体样式 HSSFFont fontStyle = workbook.createFont(); fontStyle.setFontName("微软雅黑"); fontStyle.setFontHeightInPoints((short)12); style.setFont(fontStyle); HSSFSheet sheet = workbook.createSheet("sheet"); getCell(sheet,0,0).setCellValue("序号"); getCell(sheet,0,1).setCellValue("单位名称"); getCell(sheet,0,2).setCellValue("证书名称"); getCell(sheet,0,3).setCellValue("证书编号"); getCell(sheet,0,4).setCellValue("证书等级"); getCell(sheet,0,5).setCellValue("证书类型"); getCell(sheet,0,6).setCellValue("证书申请时间"); getCell(sheet,0,7).setCellValue("证书到期时间"); getCell(sheet,0,8).setCellValue("开票时间"); getCell(sheet,0,9).setCellValue("出证时间"); getCell(sheet,0,10).setCellValue("年审时间"); getCell(sheet,0,11).setCellValue("费用"); getCell(sheet,0,12).setCellValue("备注"); Calendar calendar = Calendar.getInstance(); List vipCompanys = (List) model.get("vipCompanys"); for (int i = 0; i < vipCompanys.size(); i++) { VipCompany vipCompany = vipCompanys.get(i); getCell(sheet,i+1,0).setCellValue(vipCompany.getCompanySerial()); getCell(sheet,i+1,1).setCellValue(vipCompany.getVipMember().getMemberName()); getCell(sheet,i+1,2).setCellValue(vipCompany.getCertificateName()); getCell(sheet,i+1,3).setCellValue(vipCompany.getCertificateCard()); getCell(sheet,i+1,4).setCellValue(vipCompany.getCertificateLevel()); getCell(sheet,i+1,5).setCellValue(vipCompany.getCertificateType()); if (vipCompany.getCertificateApplyTime() != null) { Date certificateApplyTime = vipCompany.getCertificateApplyTime(); calendar.setTime(certificateApplyTime); getCell(sheet,i+1,6).setCellValue(calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH)); } if (vipCompany.getCertificateMaturityTime() != null) { Date certificateMaturityTime = vipCompany.getCertificateMaturityTime(); calendar.setTime(certificateMaturityTime); getCell(sheet,i+1,7).setCellValue(calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH)); } if (vipCompany.getOpenticketTime() != null) { Date openticketTime = vipCompany.getOpenticketTime(); calendar.setTime(openticketTime); getCell(sheet,i+1,8).setCellValue(calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH)); } if (vipCompany.getOutcardTime() != null) { Date outcardTime = vipCompany.getOutcardTime(); calendar.setTime(outcardTime); getCell(sheet,i+1,9).setCellValue(calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH)); } if (vipCompany.getAnnualReviewTime() != null) { Date annualReviewTime = vipCompany.getAnnualReviewTime(); calendar.setTime(annualReviewTime); getCell(sheet,i+1,10).setCellValue(calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH)); } getCell(sheet,i+1,11).setCellValue(vipCompany.getCost()); getCell(sheet,i+1,12).setCellValue(vipCompany.getCertificateRemark()); } String filename = URLEncoder.encode("单位评价证信息导出.xls", "UTF-8"); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=" + filename); OutputStream ouputStream = response.getOutputStream(); workbook.write(ouputStream); ouputStream.flush(); ouputStream.close(); } }