@SuppressWarnings("deprecation")
public HSSFWorkbook exportExcel(List<Operatelog> list){
int count = list.size();
int num = count%50000;
int num1;
if(num==0){
num1=count/50000;
}else{
num1=count/50000+1;
}
String[] excelHeader = {"用户名","操作状态","被操作数据id","ip地址","时间"};
HSSFWorkbook wb = new HSSFWorkbook();
for(int j=1;j<=num1;j++){
HSSFSheet sheet = wb.createSheet();
HSSFRow row1 = sheet.createRow(0);
row1.setHeight((short) 800);
HSSFCell cell1 = row1.createCell(0);
cell1.setCellValue("用户操作日志记录");
// 设置字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 15);// 字体高度
font.setColor(HSSFFont.COLOR_NORMAL); // 字体颜色
font.setFontName("隶书"); // 字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 宽度
// 设置单元格类型
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平布局:居中
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setWrapText(true);
cell1.setCellStyle(cellStyle); // 设置单元格样式
HSSFRow row = sheet.createRow(1);
row.setHeight((short)400); //设置行高
sheet.setColumnWidth(0, 7500); //设置没列的宽度
sheet.setColumnWidth(1, 8500);
sheet.setColumnWidth(2, 7500);
sheet.setColumnWidth(3, 11500);
sheet.setColumnWidth(4, 14500);
// 设置字体
HSSFFont fontlast = wb.createFont();
fontlast.setFontHeightInPoints((short) 12);// 字体高度
fontlast.setColor(HSSFFont.COLOR_NORMAL); // 字体颜色
fontlast.setFontName("宋体"); // 字体
HSSFCellStyle style = wb.createCellStyle();
style.setFont(fontlast);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setWrapText(true);
for(int i=0;i<excelHeader.length;i++){
HSSFCell cell = row.createCell(i);
cell.setCellValue(excelHeader[i]);
cell.setCellStyle(cellStyle);
}
//当没一个sheet满50000条的时候就新建一个sheet。每个sheet最多放65535条数据
for (int i = 50000*j-50000; i < 50000*j && i<count; i++) {
row = sheet.createRow(i-50000*j+50002);
Operatelog operateLog = list.get(i);
HSSFCell cell = row.createCell(0);
cell.setCellValue(operateLog.getUsername());
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue(operateLog.getOperatestatus());
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue(operateLog.getInfoid());
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue(operateLog.getIp());
cell.setCellStyle(style);
cell = row.createCell(4);
String createTime = operateLog.getCreatetime().toString();
//因为渠道的时间是yyyy-MM-dd HH:mm:ss.0这个形式,所以这样截取
cell.setCellValue(createTime.substring(0,createTime.lastIndexOf(".")));
cell.setCellStyle(style);
}
Region region = new Region(0, (short) 0, 0, (short) (4));
sheet.addMergedRegion(region);
}
return wb;
}