Excel動態分sheet頁

     
    /** 
     * sheet分頁(動態的換sheet) 
     * @author hanchuang 
     * */  
public class ExcelSheet {  
          
        /**  
         * @category firstExcel 
         * @exception fileNotFindException 
         * */  
        //1、列數;2、文件內容;3、文件名;4、每頁最大記錄數;5、文件路徑
        public void pageSheetExcel(int column,String content,String filename,int maxRows,String url){  
            //初始化  
            HSSFWorkbook wk = new HSSFWorkbook();       //創建工作簿  
            String []cellname=content.substring(content.indexOf("。")).split(",");
            Arrays.fill(cellname,0,1,cellname[0].substring(1, cellname[0].length()));//去掉分隔符“。”
            int cell=cellname.length/column;
            System.out.println(cell+"----");
            String[][] dataMsg = new String[cell][column];  //共多少條數據;多少列-----           

            for(int i = 0 ;i<dataMsg.length;i++){        //初始化表格數據  
                for(int j = 0;j<dataMsg[0].length;j++){
                	for(int hc=(j+i*column);hc<(j+1+i*column);hc++){
                	
                			 dataMsg[i][j]=cellname[hc];  //內容-----
                	}
                   
                }  
            } 
            
            String [] headerMsg=content.substring(0, content.indexOf("。")).split(",");
            // String[] headerMsg = {"一","二","三"};     //初始化表頭 -------- 
              
            /************************************************/  
            /*  分頁邏輯              
            /*  定義每頁最大記錄數      
            /*  根據最大記錄數計算出sheet數    
            /*  有可能最後一頁記錄達不到最大記錄數
            /************************************************/  
            int maxRow = maxRows;         //每頁最大記錄數  
            int pageSheet = (cell/maxRow) + 1;//分的頁數  
            int unFill = (cell%maxRow);       //最後一頁可能不滿maxRow  
              
            for(int k = 0 ; k<pageSheet; k++){  
                HSSFSheet sheet = wk.createSheet("第" + k + "頁");  
                sheet.setColumnWidth(0,0);// Excel空一行    
                //initTitle(headerMsg, sheet, wk);  
                initHeader(headerMsg, sheet, wk);  
                String[][] data;  
                if(k != pageSheet-1){  
                    data = new String[maxRow][dataMsg[0].length];  
                    for(int i = 0;i<maxRow;i++){  
                        for(int j= 0; j<dataMsg[0].length; j++){  
                            data[i][j] = dataMsg[ k*maxRow + i ][j];  
                        }  
                    }  
                }else {//最後一頁的記錄  
                    data = new String[unFill][dataMsg[0].length];  
                    for(int i = 0; i < unFill ;i++){  
                        for(int j= 0; j<dataMsg[0].length; j++){  
                            data[i][j] = dataMsg[k*maxRow + i ][j];  //
                        }  
                    }  
                }  
                creatCell(data, sheet, wk);  
            }  
              
            //生成Excel  
            try {  

                FileOutputStream fileOut = new FileOutputStream(  
                		url+filename); //------- 
                
                wk.write(fileOut);  
                fileOut.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
          
        /** 
         * 設置標題 
         * @param headermsg:表頭信息,用之標識合併多少列 
         * @param title:標題 
         * @param sheet:所在頁 
         * @param wk:工作簿 
         * */  
        private void initTitle(String[] headerMsg,String title,HSSFSheet sheet,HSSFWorkbook wk){  
              
            //初始化  
            HSSFRow titleRow = sheet.createRow(0);  
            HSSFCell titleCell = titleRow.createCell(1);  
            HSSFCellStyle style = wk.createCellStyle();  
              
            //位置  
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
              
            //邊框  
            style.setBorderBottom(CellStyle.BORDER_THIN);  
            style.setBorderLeft(CellStyle.BORDER_THIN);  
            style.setBorderRight(CellStyle.BORDER_THIN);  
            style.setBorderTop(CellStyle.BORDER_THIN);  
              
//            //背景  
//            style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);  
//            style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
              
            //字體  
            HSSFFont font = wk.createFont();  
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
            font.setFontName("宋體");  
            font.setFontHeightInPoints((short)16);  
            style.setFont(font);  
            titleRow.setHeightInPoints((float)20);  
            titleCell.setCellStyle(style);  
              
            //text  
            titleCell.setCellValue(title);  
              
            //合併  
            sheet.addMergedRegion(new CellRangeAddress(0,1,1,headerMsg.length));  
        }  
          
        /** 
         * 設置表頭 
         * @param headerMsg:表頭字段 
         * @param sheet:所在頁 
         * @param wk:工作簿 
         * */  
        private void initHeader(String[] headerMsg,HSSFSheet sheet,HSSFWorkbook wk){  
              
            //初始化  
            HSSFRow headerRow = sheet.createRow(0);  
            HSSFCellStyle style = wk.createCellStyle();  
              
            //位置  
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
            style.setBorderBottom(CellStyle.BORDER_THIN);  
            style.setBorderLeft(CellStyle.BORDER_THIN);  
            style.setBorderRight(CellStyle.BORDER_THIN);  
            style.setBorderTop(CellStyle.BORDER_THIN);  
//              
//            //背景  
//            style.setFillForegroundColor(HSSFColor.GREEN.index);  
//            style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
              
            //字體  
            HSSFFont font = wk.createFont();  
            font.setFontName("宋體");  
            font.setFontHeightInPoints((short)11);  
            style.setFont(font);  
              
            //text  
            for(int i=0;i<headerMsg.length;i++){  
                HSSFCell headerCell = headerRow.createCell(i+1);//第一行空出來  
                headerCell.setCellStyle(style);  
                headerCell.setCellValue(headerMsg[i]);  
            }  
        }  
          
        /** 
         * 設置表頭 
         * @param dataMsg:字段內容 
         * @param sheet:所在頁 
         * @param wk:工作簿 
         * */  
        private void creatCell(String[][] dataMsg,HSSFSheet sheet,HSSFWorkbook wk){  
            HSSFCellStyle style = wk.createCellStyle();  
              
            //邊框  
            style.setBorderBottom(CellStyle.BORDER_THIN);  
            style.setBorderLeft(CellStyle.BORDER_THIN);  
            style.setBorderRight(CellStyle.BORDER_THIN);  
            style.setBorderTop(CellStyle.BORDER_THIN);  
              
//            //背景  
//            style.setFillForegroundColor(HSSFColor.YELLOW.index);  
//            style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
              
            //字體  
            HSSFFont font = wk.createFont();  
            font.setFontName("宋體");  
            font.setFontHeightInPoints((short)8);  
            style.setFont(font);  
              
            //text  
            if(dataMsg.length != 0){  
                for(int i = 0 ;i < dataMsg.length;i++){  //控制行  
                    HSSFRow dataRow = sheet.createRow(i+1);  
                    for(int j = 0;j<dataMsg[0].length;j++){  //控制列  
                        HSSFCell headerCell = dataRow.createCell(j+1);//第一列空出來  
                        headerCell.setCellStyle(style);  
                        headerCell.setCellValue(dataMsg[i][j]);  
                    }  
                }  
            }  
        }  
          
        /** 
         * main函數測試 
         * @param args 
         * */  
        public static void main(String[] args) {  
           
        	String content="count(0),處理日期,處理時間。1,2017-12-04,09:45:00,2,2017-12-04,09:45:00,";
        	
        	 new ExcelSheet().pageSheetExcel(3, content, "hanchuang.xls",60000,"D:/timingNums/"); 
        }  
    }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章