將數據庫中的數據導入到excel中

                  從數據庫中讀取數據導入到excel中,如果數據量小,的確很簡單,直接用POI輸出就OK了,可是我這邊有>6W的數據要輸出,java運行了直接溢出了,于是前輩給了個取巧的辦法,調用java命令并設置vm參數-Xms1024M -Xmx1024M,呵呵,這樣很大了,那么下面就是著手運行這個命令就行了,首先把這個導出的方法寫在要調用的類的main方法中,然后在我的網站應用的Action中去執行一個命令腳本,windows下用bat,linux下用sh,由于要執行腳本,在linux下要注意就是要把你的應用讀寫權限打開。那么先從調用的命令開始
              1.設置好參數,就是main中的String[] args參數比如輸出excel的路徑
                          String excelPath = Struts2Utils.getSession().getServletContext().getRealPath("/upload/doc/export.xls");
              2.生成好bat或者是sh文件
                          String osName = System.getProperty("os.name");//獲取操作系統
                          if (osName.toLowerCase().indexOf("windows") != -1) {
                                  filePath = Struts2Utils.getSession().getServletContext().getRealPath("/WEB-INF/classes/excelDb.bat");
                          } 
                          else
                         {
                                 filePath = Struts2Utils.getSession().getServletContext().getRealPath("/WEB-INF/classes/excelDb.sh");
                         }

                        String cmdLine ="";
                        cmdLine+="cd "+classPath+" \r\n";
                        cmdLine+=" java -Xms1024M -Xmx1024M -Djava.ext.dirs="+dirPath+" com.twc.common.utils.command.Command "+excelPath;
                        try {
                                 FileUtils.writeFile(new File(filePath), cmdLine.getBytes());//生成文件
                        } catch (IOException e1) {
                             e1.printStackTrace();
                        }
              3.調用這個bat或者是sh文件
                

                  Process proc = null;
                  try {
                           String result = "";
                           proc = Runtime.getRuntime().exec(filePath);
                           BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                           while ((result = bufferedReader.readLine()) != null)
                           System.out.println(result);

                           if(proc.waitFor() != 0) {
                                      logger.error("Can't execute: " + cmdLine);
                           }
                          if(error.length() > 0) {
                                      logger.warn(error);
                           }
                  } catch (Exception e) {
                                      logger.error(e.getMessage(), e);
                  } finally {
                                      proc.destroy();
                                      proc = null;
                  }

          這樣Action的使命就完成了,那么接下來就是實現調用類的導出excel方法了(我的調用類是Command類)下面的方法是main中的,我截取了最重要的出來
           1 List<Vector> result = new ArrayList<Vector>();
           2             dba.open();
           3             try {
           4             
           5                 String sql = sb.toString();
           6                 String[] arrFields = fields.split(",");
           7                 int size = arrFields.length;
           8                 dba.query(sql);
           9                 while (dba.rs != null && dba.rs.next()) {
          10             
          11                     Vector cell = new Vector();
          12                     for(int i=1;i<size+1;i++)
          13                     {
          14                         cell.add(dba.rs.getString(i));
          15                     }
          16                     result.add(cell);
          17                 }//從數據庫的到數據存放在result中
          18             } catch (SQLException e) {
          19                 
          20             }
          21             PoiUtils e  =   new  PoiUtils(excelPath);
          22             int cnt=1;
          23             int size = result.size();
          24             int block = size/Command.BLOCK;
          25             if(block<1)//判斷數據是否超出excel極限(excel的一個sheet最多存放6w多行數據)
          26             {
          27                 //直接把所有數據輸出到一個sheet中去
          28                 e.createSheet(0);
          29                 e.createRow(0);
          30                 for(int j=0;j<arrField.length;j++)
          31                 {
          32                     
          33                         e.setCell(j, arrField[j]);
          34                     
          35                 }
          36                 for(int l=1;l<size+1;l++)    
          37                 {        e.createRow(l);
          38                         for(int j=0;j<arrField.length;j++)
          39                         {
          40                             if(result.get(l-1).get(j)!=null)
          41                             {
          42                                 e.setCell(j, result.get(l-1).get(j).toString());
          43                             }
          44                             else
          45                             {
          46                                 e.setCell(j, "");
          47                             }
          48                         }
          49                 }
          50             }
          51             else
          52             {
          53                 //如果超出了范圍則把數據分別輸出到k個sheet中去
          54                 for(int k=0;k<block;k++)
          55                 {
          56                     //第k+1個sheet
          57                     e.createSheet(k+1);
          58                     //數據字段名
          59                     e.createRow(0);
          60                     for(int j=0;j<arrField.length;j++)
          61                     {
          62                         
          63                             e.setCell(j, arrField[j]);
          64                         
          65                     }
          66                     int start = k*Command.BLOCK;
          67                     int end = Command.BLOCK*(k+1);
          68                     for(int l=start;l<end;l++)    //輸出到excel中
          69                     {        
          70                             e.createRow(cnt);
          71                             cnt++;
          72                             if(cnt==Command.BLOCK+1)cnt = 1;
          73                             for(int j=0;j<arrField.length;j++)
          74                             {
          75                                 if(result.get(l).get(j)!=null)
          76                                 {
          77                                     e.setCell(j, result.get(l).get(j).toString());
          78                                 }
          79                                 else
          80                                 {
          81                                     e.setCell(j, "");
          82                                 }
          83                             }
          84                     }
          85                 }
          86             }
          87             
          88              try    {
          89                     e.exportXLS();//輸出結果
          90                     System.out.println("Exprot Success!");
          91                 }   catch  (XLSException e1)   {
          92                 
          93                 } 




          posted on 2010-03-31 15:39 Cloud kensin 閱讀(1430) 評論(0)  編輯  收藏 所屬分類: Java

          <2010年3月>
          28123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導航

          統計

          常用鏈接

          留言簿(4)

          隨筆分類

          相冊

          相冊

          搜索

          最新評論

          主站蜘蛛池模板: 黄冈市| 安徽省| 康平县| 昌吉市| 拉萨市| 桑日县| 太湖县| 攀枝花市| 车致| 班玛县| 孟连| 阜宁县| 鄂尔多斯市| 绥德县| 大渡口区| 鲁甸县| 廉江市| 南汇区| 鄂尔多斯市| 屏东县| 万全县| 荣昌县| 松溪县| 宁河县| 沾化县| 垣曲县| 沁阳市| 满城县| 吴江市| 桐梓县| 隆德县| 凌源市| 海阳市| 延津县| 贞丰县| 美姑县| 六枝特区| 邻水| 玉林市| 自治县| 龙川县|