sheetAct=createExcelHead(sheetAct,writer,excelHead,title+num);}list=agentService.queryExportData(map);if(j<5){findExcelData(j*limit,list,sheetAct);}else{int num=j/5;findExcelData((j-num*5)*limit,list,sheetAct);}}writer.write();}catch(Exception e){LOGGER.error("Exception:", e.fillInStackTrace());}finally {if (null != writer) {writer.close();}}return out.toByteArray();}findExcelData 方法:private void findExcelData(int start,List<StudioSeriesPrice> list,ExcelWriter.Sheet sheetAct){StudioSeriesPrice detail=null;/*拼装excel内容*/for(int i=0;i<list.size();i++){detail=list.get(i);sheetAct.getOrCreateCell((start+i + 1), 0).setCellStyle(sheetAct.addStyle());sheetAct.getOrCreateCell((start+i + 1), 0).setCellValue(detail.getCompany());sheetAct.getOrCreateCell((start+i + 1), 1).setCellStyle(sheetAct.addStyle());sheetAct.getOrCreateCell((start+i + 1), 1).setCellValue(detail.getCreatetime());sheetAct.getOrCreateCell((start+i + 1), 2).setCellStyle(sheetAct.addStyle());sheetAct.getOrCreateCell((start+i + 1), 2).setCellValue(detail.getPrice());sheetAct.getOrCreateCell((start+i + 1), 3).setCellStyle(sheetAct.addStyle());sheetAct.getOrCreateCell((start+i + 1),3).setCellValue(detail.getNum());sheetAct.getOrCreateCell((start+i + 1), 4).setCellStyle(sheetAct.addStyle());sheetAct.getOrCreateCell((start+i + 1), 4).setCellValue(detail.getTypename());sheetAct.getOrCreateCell((start+i + 1), 5).setCellStyle(sheetAct.addStyle());sheetAct.getOrCreateCell((start+i + 1), 5).setCellValue(detail.getAgentName());sheetAct.getOrCreateCell((start+i + 1), 6).setCellStyle(sheetAct.addStyle());sheetAct.getOrCreateCell((start+i + 1), 6).setCellValue(detail.getPricename());}}/**** @param sheetAct* @param writer* @param excelHead* @param title* @return*/private ExcelWriter.Sheet createExcelHead( ExcelWriter.Sheet sheetAct,ExcelWriter writer,String [] excelHead,String title){ sheetAct = writer.getOrCreateSheet(title);/*拼装excel头部*/for(int i=0;i<excelHead.length;i++){sheetAct.getOrCreateCell(0,i).setCellStyle(sheetAct.addStyle());sheetAct.setColumnWidth(i, 6000);sheetAct.getOrCreateCell(0,i).setCellValue(excelHead[i]);}return sheetAct;}帮助类:package com.kerui.utils;import java.io.IOException;import java.io.OutputStream;import java.util.Date;import java.util.HashMap;import java.util.Map;import mons.io.IOUtils;import ermodel.HSSFCellStyle;import ermodel.CellStyle;import ermodel.DataFormat;import ermodel.Row;import ermodel.Workbook;import org.apache.poi.ss.util.CellRangeAddress;import ermodel.XSSFWorkbook;public final class ExcelWriter {public static final class Cell {private Workbook wb;private ermodel.Cell target;private Cell(Workbook wb, ermodel.Cell cell) {this.wb = wb;this.target = cell;}public void setCellValue(String value) {this.target.setCellValue(value);}public void setCellValue(Date value) {if (null == value) {this.setCellValue("未设置");return;}CellStyle style = this.wb.createCellStyle();DataFormat format = wb.createDataFormat();style.setDataFormat(format.getFormat("yyyy/m/dhh:mm:ss"));this.target.setCellStyle(style);this.target.setCellValue(value);}public void setCellValue(double value) {CellStyle style = this.wb.createCellStyle();DataFormat format = wb.createDataFormat();style.setDataFormat(format.getFormat("###,##0"));this.target.setCellStyle(style);this.target.setCellValue(value);}public void setCellStyle(CellStyle style){this.target.setCellStyle(style);}}public static final class Sheet{private Workbook wb;private ermodel.Sheet sheet;private Sheet(Workbook wb, ermodel.Sheet sheet) {this.sheet = sheet;this.wb = wb;}public void setColumnWidth(int column, int width){sheet.setColumnWidth(column, width);}public Cell getOrCreateCell(int rowBasedZero, int cellBasedZero) {Row row = this.sheet.getRow(rowBasedZero);if (null == row) {row = this.sheet.createRow(rowBasedZero);}return new Cell(this.wb, row.getCell(cellBasedZero, Row.CREATE_NULL_AS_BLANK));}//合并单元格public void mergedRegion(int intFirstRow, int intLastRow, int intFirstColumn, int intLastColumn){this.sheet.addMergedRegion(newCellRangeAddress(intFirstRow, intLastRow, intFirstColumn, intLastColumn));}//添加居中的样式public CellStyle addStyle(){CellStyle style = wb.createCellStyle();style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平return style;}}private Map<String, Sheet> sheetMap= new HashMap<String, Sheet>();private OutputStream out;private Workbook wb;public ExcelWriter(OutputStream out) {this.out = out;wb = new XSSFWorkbook();}public Sheet getOrCreateSheet(String sheet) {Sheet sheet1 = this.sheetMap.get(sheet);if (null != sheet1) {return sheet1;}ermodel.Sheet xlsSheet = this.wb.getSheet(sheet);if (null == xlsSheet) {xlsSheet = this.wb.createSheet(sheet);}Sheet obj = new Sheet(this.wb, xlsSheet);this.sheetMap.put(sheet, obj);return obj;}public void write() throws IOException {this.wb.write(this.out);}public void close() {this.sheetMap.clear();IOUtils.closeQuietly(this.out);}}。