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/"); 
        }  
    }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值