@ResponseBody
@RequestMapping(params="method=loadOutExcel")
publicvoidloadOutExcel(HttpServletRequestrequest)throwsIOException{
//人员登录id
Farmerfarmer=(Farmer)request.getSession().getAttribute("farmer");
intid=farmer.getId();
//根据登录id,从数据库读取全部值
ListorderDetailsList=orderDetailService.getHistoryOrderDetailListExcel(id);
XSSFWorkbookworkbook=newXSSFWorkbook();
XSSFCellStylecellStyle=workbook.createCellStyle();
//创建EXCEL表名
XSSFSheetspreadsheet=workbook.createSheet("历史订单");
XSSFRowrow=spreadsheet.createRow(0);
XSSFCellcell;
//在EXCEL的第一行建立各列的标题
cell=row.createCell(0);
cell.setCellValue("配送时间");
cell=row.createCell(1);
cell.setCellValue("订单量");
cell=row.createCell(2);
cell.setCellValue("农场名称");
cell=row.createCell(3);
cell.setCellValue("商品名称");
cell=row.createCell(4);
cell.setCellValue("商品数量");
cell=row.createCell(5);
cell.setCellValue("单位");
intcountIndex=0;
Stringdeli_day="";//定义一个临时存储值
intstartRow=1;
intsum=0;
intcountIndex_s=0;
intstartRow_s=1;
inti=1;
for(OrderWaitPojoo:orderDetailsList){
row=spreadsheet.createRow(i);
cell=row.createCell(0);
cell.setCellValue(o.getDelivery_day());
if("".equals(deli_day)){//当第一次循环的时候将第一个实际值赋值给临时变量
deli_day=o.getDelivery_day();
}else{
if(o.getDelivery_day().equals(deli_day)){//第二次循环取得的值与第一次循环取得值进行对比,如果相等,计数器countIndex+1
countIndex++;
}else{
//如果比较不相等,则合并单元格,现在合并的是从第startRow行开始,到startRow+countIndex行结束,后边两个0是从第0列开始,到第0列结束
spreadsheet.addMergedRegion(newCellRangeAddress(startRow,startRow+countIndex,0,0));
startRow=startRow+countIndex+1;
countIndex=0;
deli_day=o.getDelivery_day();
}
}
cell=row.createCell(1);
cell.setCellValue(o.getSum());
if(sum==0){
sum=o.getSum();
}else{
if(o.getSum()==sum){
countIndex_s++;
}else{
spreadsheet.addMergedRegion(newCellRangeAddress(startRow_s,startRow_s+countIndex_s,1,1));
startRow_s=startRow_s+countIndex_s+1;
countIndex_s=0;
sum=o.getSum();
}
}
cell=row.createCell(2);
cell.setCellValue(o.getFarm_name());
cell=row.createCell(3);
cell.setCellValue(o.getProduct_name());
cell=row.createCell(4);
cell.setCellValue(o.getAmount());
cell=row.createCell(5);
cell.setCellValue(o.getUnit());
i++;
}
FileOutputStreamout=newFileOutputStream(newFile("D:/历史订单.xlsx"));
workbook.write(out);
out.close();
System.out.println(
"exceldatabase.xlsxwrittensuccessfully");
}