导出多个sheet
要求:
页面导出,同时导出四个sheet,不同的sheet展示内容不同
原理
分别查出四个sheet对应的数据,然后调用四次POI,所有数据一起写入,最后关闭流。
注意:
全量导出数据量的控制,大于20万是否自动分sheet,以及容错处理
参考以下代码
1:设置sheet格式以及标题
@RequestMapping("/exportAllyScoreCount")
public void exportAllyScoreCount(HttpServletRequest request,HttpServletResponse response,TAllyParameterConfigDto dto){
data = new HashMap<>();
String reportDate=dto.getYearDate();
String fileName = System.currentTimeMillis()+"-"+reportDate+".xlsx";
try {
response.setHeader("Content-Disposition", "attachment; filename=".concat(fileName));
response.setHeader("Connection", "close");
response.setHeader("Content-Type", "application/vnd.ms-excel");
OutputStream ouputStream = response.getOutputStream();
/*合作商汇总得分*/
List<List<String>> data0 = new ArrayList<List<String>>();
List<TScoreCountDto> tScoreTotalDtos = mushAllytrialExportService.findTScoreTotalDtos(reportDate);
for(TScoreCountDto tScoreCountDto : tScoreTotalDtos){
List rowData = new ArrayList();
rowData.add(tScoreCountDto.getAllyId());
rowData.add(tScoreCountDto.getProvince());
rowData.add(tScoreCountDto.getCity());
rowData.add(tScoreCountDto.getCounty());
rowData.add(tScoreCountDto.getBureau()+"");
rowData.add(String.valueOf(tScoreCountDto.getMonthInterestScore()));
rowData.add(String.valueOf(tScoreCountDto.getCapitalSocre()));
rowData.add(String.valueOf(tScoreCountDto.getmTwoScore()));
rowData.add(String.valueOf(tScoreCountDto.getYqtzScore()));
rowData.add(String.valueOf(tScoreCountDto.getAllScore()));
rowData.add(String.valueOf(tScoreCountDto.getGrading()));
rowData.add(String.valueOf(tScoreCountDto.getProfitRate()));
rowData.add(String.valueOf(tScoreCountDto.getaPlusRate()));
rowData.add(String.valueOf(tScoreCountDto.getProfitCountRate()));
data0.add(rowData);
}
/*合作商明细*/
List<List<String>> data1 = new ArrayList<List<String>>();
List<TIndexDetailDto> tIndexDetais = mushAllytrialExportService.findTIndexDetais(reportDate);
for(TIndexDetailDto tIndexDetailDto : tIndexDetais){
List rowData = new ArrayList();
rowData.add(tIndexDetailDto.getAllyId());
rowData.add(tIndexDetailDto.getProvince());
rowData.add(tIndexDetailDto.getCity());
rowData.add(tIndexDetailDto.getCounty());
rowData.add(tIndexDetailDto.getBureau()+"");
rowData.add(tIndexDetailDto.getFirstTenderYear());
rowData.add(String.valueOf(tIndexDetailDto.getMonthInterestRateScore()));
rowData.add(String.valueOf(tIndexDetailDto.getCapitalRateScore()));
rowData.add(String.valueOf(tIndexDetailDto.getmTwoRateScore()));
rowData.add(String.valueOf(tIndexDetailDto.getYqtzRateScore()));
rowData.add(String.valueOf(tIndexDetailDto.getYearInterestRateScore()));
rowData.add(String.valueOf(tIndexDetailDto.getYearCapitalRateScore()));
rowData.add(String.valueOf(tIndexDetailDto.getYearMTwoRateScore()));
rowData.add(String.valueOf(tIndexDetailDto.getYearYqtzRateScore()));
rowData.add(String.valueOf(tIndexDetailDto.getYearSumScore()));
data1.add(rowData);
}
/*得分算法*/
List<List<String>> data2 = new ArrayList<List<String>>();
List<TScoreDto> tScores = mushAllytrialExportService.findTScores(dto);
for(TScoreDto tScoreDto : tScores){
List rowData = new ArrayList();
rowData.add(tScoreDto.getYearDate());
rowData.add(tScoreDto.getNormName());
rowData.add(String.valueOf(tScoreDto.getNormWeight()));
rowData.add(tScoreDto.getMaxScore()*100+"%");
rowData.add(tScoreDto.getMinScore()*100+"%");
data2.add(rowData);
}
/*分级算法参数*/
List<List<String>> data3 = new ArrayList<List<String>>();
List<TClassificationDto> classifications = mushAllytrialExportService.findClassifications(dto);
for(TClassificationDto tClassificationDto : classifications){
List rowData = new ArrayList();
rowData.add(tClassificationDto.getClassificationProject());
rowData.add(tClassificationDto.getClassificationS());
rowData.add(tClassificationDto.getScore());
rowData.add(tClassificationDto.getLimitYear());
rowData.add(tClassificationDto.getLimitMoney());
data3.add(rowData);
}
/*分润算法*/
List<List<String>> data4 = new ArrayList<List<String>>();
List<TProfitDto> tProfits = mushAllytrialExportService.findTProfits(dto);
for(TProfitDto tProfitDto : tProfits){
List rowData = new ArrayList();
rowData.add(tProfitDto.getProfitArea());
rowData.add(tProfitDto.getMaxScore());
rowData.add(tProfitDto.getMinScore());
rowData.add(tProfitDto.getMaxScoreRatio()*100 + "%");
rowData.add(tProfitDto.getMinScoreRatio()*100 + "%");
rowData.add(String.valueOf(tProfitDto.getProfitADoor()));
rowData.add(String.valueOf(tProfitDto.getProfitADoor()));
rowData.add(tProfitDto.getaRatio()*100+"%");
data4.add(rowData);
}
String[] headers0 = { "合作商ID","省", "市","县","局","三农当月还息率总得分","三农当月本金结清率总得分","三农M不良率2总得分","三农YQTZ率总得分","总分","分级","分润系数","A+系数","分润总系数"};
String[] headers1 = { "合作商ID","省", "市","县","局","首标年份","三农当月利息还息率得分","三农当月本金结清率得分","三农M不良率2得分","三农YQTZ率得分","三农当月利息还息率年得分","三农本金结清率年得分","三农M不良率2年得分","三农YQTZ率年得分","年总分"};
String[] headers2 = { "年份","指标名称", "权重","最高分限","最低分限"};
String[] headers3 = { "类型","级别", "分级得分","年限","存量上限(万)"};
String[] headers4 = { "区域","分数上限", "分数下限","比率上限","比率下限","分润门槛(万)","A+门槛(万)","A+比率"};
ExportExcelUtils eeu = new ExportExcelUtils();
XSSFWorkbook workbook = new XSSFWorkbook();
eeu.exportExcel(workbook, 0, "得分统计表", headers0, data0, ouputStream);
eeu.exportExcel(workbook, 1, "合作商指标明细", headers1, data1, ouputStream);
eeu.exportExcel(workbook, 2, "得分算法", headers2, data2, ouputStream);
eeu.exportExcel(workbook, 3, "分级算法", headers3, data3, ouputStream);
eeu.exportExcel(workbook, 4, "分润算法", headers4, data4, ouputStream);
//原理就是将所有的数据一起写入,然后再关闭输入流。
workbook.write(ouputStream);
ouputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2:填充数据
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.*;
/**
* @Title: exportExcel
* @Description: 导出Excel的方法
* @param workbook
* @param sheetNum (sheet的位置,0表示第一个表格中的第一个sheet)
* @param sheetTitle (sheet的名称)
* @param headers (表格的标题)
* @param result (表格的数据)
* @param out (输出流)
* @throws Exception
*/
public void exportExcel(XSSFWorkbook workbook, int sheetNum,
String sheetTitle, String[] headers, List<List<String>> result,
OutputStream out) throws Exception {
// 生成一个表格
XSSFSheet sheet = workbook.createSheet();
workbook.setSheetName(sheetNum, sheetTitle);
// 设置表格默认列宽度为20个字节
sheet.setDefaultColumnWidth((short) 20);
// 生成一个样式
XSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成一个字体
XSSFFont font = workbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
// 指定当单元格内容显示不下时自动换行
style.setWrapText(true);
// 产生表格标题行
XSSFRow row = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
XSSFCell cell = row.createCell((short) i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text.toString());
}
// 遍历集合数据,产生数据行
if (result != null) {
int index = 1;
for (List<String> m : result) {
row = sheet.createRow(index);
int cellIndex = 0;
for (String str : m) {
XSSFCell cell = row.createCell((short) cellIndex);
cell.setCellValue(str);
cellIndex++;
}
index++;
}
}
}