导出使用的工具类
import com.netinfo.emp.record.web.entity.ExcelData; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; import java.net.URLEncoder; import java.util.List; import java.util.Map; /** * Created by zzq on 2019/05/09. */ public class ExportExcelUtils { public static void exportExcel(HttpServletResponse response, String fileName, ExcelData data) throws Exception { // 告诉浏览器用什么软件可以打开此文件 response.setHeader("content-Type", "application/vnd.ms-excel"); // 下载文件的默认名称 response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(fileName, "utf-8")); exportExcel(data, response.getOutputStream()); } public static void exportExcel(ExcelData data, OutputStream out) throws Exception { XSSFWorkbook wb = new XSSFWorkbook(); try { String sheetName = data.getName(); if (null == sheetName) { sheetName = "Sheet1"; } XSSFSheet sheet = wb.createSheet(sheetName); writeExcel(wb, sheet, data); wb.write(out); } finally { wb.close(); out.close(); } } private static void writeExcel(XSSFWorkbook wb, Sheet sheet, ExcelData data) { int rowIndex = writeTitlesToExcel(wb, sheet, data.getTitles()); writeRowsToExcel(wb, sheet, data.getRows(), rowIndex); autoSizeColumns(sheet, data.getTitles().size() + 1); } private static int writeTitlesToExcel(XSSFWorkbook wb, Sheet sheet, Maptitles) { int rowIndex = 0; int colIndex = 0; Font titleFont = wb.createFont(); titleFont.setFontName("simsun"); titleFont.setBold(true); // titleFont.setFontHeightInPoints((short) 14); titleFont.setColor(IndexedColors.BLACK.index); XSSFCellStyle titleStyle = wb.createCellStyle(); titleStyle.setFont(titleFont); setBorder(titleStyle, BorderStyle.THIN); Row titleRow = sheet.createRow(rowIndex); // titleRow.setHeightInPoints(25); for (String field : titles.values()) { Cell cell = titleRow.createCell(colIndex); cell.setCellValue(field); cell.setCellStyle(titleStyle); colIndex++; } rowIndex++; return rowIndex; } private static int writeRowsToExcel(XSSFWorkbook wb, Sheet sheet, List > rows, int rowIndex) { int colIndex = 0; Font dataFont = wb.createFont(); dataFont.setFontName("simsun"); // dataFont.setFontHeightInPoints((short) 14); dataFont.setColor(IndexedColors.BLACK.index); XSSFCellStyle dataStyle = wb.createCellStyle(); dataStyle.setFont(dataFont); setBorder(dataStyle, BorderStyle.THIN); for (List
使用类
/** * 导出列表数据到excel * @param response * @param queryId * @param pageIndex * @param pageSize * @return */ public WebReturn exportRecord(HttpServletResponse response, String queryId, Integer pageIndex, Integer pageSize) { WebReturn webReturn = new WebReturn(); try { //webReturn = queryRecord2(queryId, pageIndex, pageSize); if (!webReturn.isResult()) { log.error(String.format("exportRecord -mediaQuery fail. [%d]%s", webReturn.getCode(), webReturn.getMsg())); return webReturn; } WebReturn webReturn1 = viewColumnService.getColumns("9112610110100000000", "2", "1"); if (!webReturn1.isResult()) { log.error(String.format("exportRecord -getColumns fail. [%d]%s", webReturn1.getCode(), webReturn1.getMsg())); return webReturn1; } // //ExcelData data = new ExcelData(); data.setName("数据列表"); Map //titles = new LinkedHashMap<>(); List viewColumns = (List )webReturn1.getData(); for (ViewColumn column : viewColumns){ if (column.getViewOnPage() == 1){ titles.put(column.getColId(), column.getDisplay()); } } data.setTitles(titles); // List ExportExcelUtils.exportExcel(response, "数据.xlsx", data); }catch (Exception ex){ log.error(String.format("exportRecord fail. %s", ex.getMessage())); ex.printStackTrace(); webReturn.setResult(false); webReturn.setCode(Defines.RET_FAIL); webReturn.setMsg(Defines.MSG_SERVER_ERROR); } return webReturn; }