1. 程式人生 > >匯出excel(超過65535條資料)例子

匯出excel(超過65535條資料)例子

    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;