java POI导出excel多个sheet

excel方法:
public class ExcelUtil {
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++;
}
}
}
}

调用:
public void exportAllyScoreCount(HttpServletResponse response,List<ProcessOrder> processOrders,List<ProcessOrderBOM> bom){

String fileName = System.currentTimeMillis()+"-"+".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<ProcessOrder> tScoreTotalDtos =processOrders;

for(ProcessOrder tScoreCountDto : tScoreTotalDtos){
List rowData = new ArrayList();
String d = String.valueOf(tScoreCountDto.getFinishTime());
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.US);
Date date1=sdf.parse(d);
sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

rowData.add(tScoreCountDto.getBatchId());

rowData.add(String.valueOf(tScoreCountDto.getProductDetail()));

data0.add(rowData);
}
/*生产计划BOM*/
List<List<String>> data1 = new ArrayList<List<String>>();
List<ProcessOrderBOM> tIndexDetais = bom;

for(ProcessOrderBOM tIndexDetailDto : tIndexDetais){
List rowData = new ArrayList();
rowData.add(String.valueOf(tIndexDetailDto.getProjectCode()));
rowData.add(String.valueOf(tIndexDetailDto.getNum()));
data1.add(rowData);
}
String[] headers0 = { "订单号(批次号)\n"};
String[] headers1 = { "项目号\n","需求量\n"};

ExcelUtil eeu = new ExcelUtil();
XSSFWorkbook workbook = new XSSFWorkbook();
eeu.exportExcel(workbook, 0, "单信息列表", headers0, data0, ouputStream);
eeu.exportExcel(workbook, 1, "生产计划", headers1, data1, ouputStream);

//原理就是将所有的数据一起写入,然后再关闭输入流。
workbook.write(ouputStream);
ouputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}



posted @ 2020-04-09 14:35  holyDan  阅读(...)  评论(...编辑  收藏