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

来源:互联网 发布:如何成为淘宝砍价师 编辑:程序博客网 时间:2024/05/22 01:07
/**  * 分多个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);                }          }  

原创粉丝点击