内容分多个excel文件进行存储数据

/** 
 * 分多个excel文件进行存储数据
 * @author hanchuang 
 * */       
public class AccessExcel {  
  
    String[] titleCell;  
    String[][] allCell;  
    jxl.Workbook workBook;  
    Sheet sheet; 
    int cell;
    int column;
      
        /* 
         * 将相关的数据存储到数组中 
         */  
    //1、列数;2、文件内容;3、文件路径;4、需要分隔的行数 
     public void readExcel(int columns,String content,String fileurl,int number) {
        	column=columns;
        	String []cellname=content.substring(content.indexOf("。")).split(",");
        	Arrays.fill(cellname,0,1,cellname[0].substring(1, cellname[0].length()));//去掉分隔符“。”
             cell=cellname.length/column;//---多少行数据
             allCell = new String[cell][column];  //共多少条数据;多少列----- 
            for(int i = 0 ;i<allCell.length;i++){        //初始化表格数据  
                for(int j = 0;j<allCell[0].length;j++){
                	for(int hc=(j+i*column);hc<(j+1+i*column);hc++){
                	
                		 allCell[i][j]=cellname[hc];  
                	}
                   
                }  
            } 
            
             titleCell=content.substring(0, content.indexOf("。")).split(",");//表头
             
             //将数组内容放入excel中
             splitExcel(number, fileurl);  
      
        }  
      
        /* 
         *@param number代表需要分隔的行数 
         *@param fileurl代表分隔文件后存储的路径 
         */  
    public void splitExcel(int number, String fileurl) {  
      
            int index = (int) Math.ceil(cell / number);//计算需要分隔多少个文件  
            File[] files = new File[index + 1];  
            //初始化文件数组  
            for (int i = 0; i <= index; i++) {  
                files[i] = new File(fileurl +"("+i+")" + ".xls");  
      
            }  
            int n = number;  
            int y = 0;//用于记录行的位置  
            for (int i = 0; i <= index; i++) {  
      
                try {  
                    jxl.write.WritableWorkbook ww = Workbook  
                            .createWorkbook(files[i]);  
                    WritableSheet ws = ww.createSheet("sheet1", 0); 
                    //设置表格表头
                    for (int t = 0; t <column; t++) {  
                        ws.addCell(new Label(t, 0, titleCell[t]));  
                    }  
                    //将数组中的内容放入表格单元格中
                    out: for (int m = 1; y < cell; y++, m++) {  
      
                        for (int x = 0; x < column; x++) {  
      
                            if (y >number) {  
                                number += n;  
                                break out;  
                            }  
      
                            ws.addCell(new Label(x, m, allCell[y][x]));  
      
                        }  
      
                    }  
                    ww.write();  
                    ww.close();  
      
                } catch (IOException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                } catch (RowsExceededException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                } catch (WriteException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
            }  
      
        }

    /** 
     * @param args 
     */  
  public static void main(String[] args) { 
	 
    	String content="count(0),处理日期,处理时间。1,2,3,4,5,6,7,8,9,9,1,2,3,4,5,6,7,8,9,9,1,2,3,4,5,6,7,8,9,9," +
    			         "1,2,3,4,5,6,7,8,9,9,1,2,3,4,5,6,7,8,9,9,1,2,3,4,5,6,7,8,9,9,1,2,3,4,5,6,7,8,9,9," +
    			         "1,2,3,4,5,6,7,8,9,9,1,2,3,4,5,6,7,8,9,9,1,2,3,4,5,6,7,8,9,9,1,2,3,4,5,6,7,8,9,9,0";
    
    	AccessExcel ae = new AccessExcel();  
        ae.readExcel(3,content,"D:\\timingNums\\hanchuang",19);  
        
  
    }  
       
 }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值