首页>代码>spring+spring mvc+mybatis+spring boot自定义查询条件实现excel报表文件导出生成>/ygq-report/src/main/java/com/ygq/report/base/controller/BaseController.java
package com.ygq.report.base.controller; import java.io.IOException; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import javax.servlet.http.HttpServletResponse; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Workbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.ModelAndView; import com.ygq.report.config.SpringContextUtil; public class BaseController { private final static Logger logger = LoggerFactory.getLogger(BaseController.class); /** * @param reportEngine * 查询对应的sql * @param parameter * 查询参数 * @return List<Object> */ protected List<Object> queryList(String reportEngine, Map<String, Object> parameter) { SqlSessionFactory sqlSessionFactory = SpringContextUtil.getBean(SqlSessionFactory.class); SqlSession session = sqlSessionFactory.openSession(); try { List<Object> list = session.selectList(reportEngine, parameter); return list; } finally { logger.debug("关闭 session" + session.toString()); session.close(); } } /** * * @param reportEngine * 查询对应的sql * @param parameter * 查询参数 * @return int 查询总计 */ protected int queryListCount(String reportEngine, Map<String, Object> parameter) { SqlSessionFactory sqlSessionFactory = SpringContextUtil.getBean(SqlSessionFactory.class); SqlSession session = sqlSessionFactory.openSession(); try { String statement = reportEngine; Map<?, ?> map = session.selectOne(statement, parameter); int count = 0; if (map.get("count") != null) { return Integer.valueOf(map.get("count").toString()); } return count; } finally { session.close(); } } protected ModelAndView downExcel(HttpServletResponse response, Workbook book, String name) { response.reset(); response.setContentType("application/vnd.ms-excel;charset=utf-8"); try { name = java.net.URLEncoder.encode(name, "UTF-8"); name = name + ".xlsx"; response.setHeader("Content-Disposition", "attachment;filename=" + name); java.io.OutputStream out = response.getOutputStream(); book.write(out); } catch (IOException e) { } return null; } @SuppressWarnings("unchecked") protected HSSFWorkbook writeExcel2007(List<Map<String, String>> lists, String sheetName) { HSSFWorkbook book = new HSSFWorkbook(); HSSFSheet sheet = book.createSheet(sheetName); if (org.apache.commons.collections.CollectionUtils.isNotEmpty(lists)) { Map<String, String> colomnsMap = lists.get(0); HSSFRow columnRow = sheet.createRow(0); int index = 0; for (Entry<String, String> entry : colomnsMap.entrySet()) { HSSFCell cell = columnRow.createCell(index); String key = entry.getKey(); cell.setCellValue(key); index++; } int rowIndex = 1; for (Map<String, String> map : lists) { HSSFRow row = sheet.createRow(rowIndex); Iterator<HSSFCell> iterator = columnRow.iterator(); while (iterator.hasNext()) { HSSFCell columnCell = iterator.next(); HSSFCell cell = row.createCell(columnCell.getColumnIndex()); String key = columnCell.getStringCellValue(); Object value = (map.get(key)); if (null == (value)) { value = ""; } cell.setCellValue(String.valueOf(value)); } rowIndex++; } } return book; } }