[转] JAVA读取excel数据(插入oracle数据库)

原文地址:http://blog.csdn.net/zczzsq/article/details/16803349

本实例做的是读取execl(只能读取.xls的execl,即只能读取03版的),如果是.xlsx类型的话 手工转化一下即可,应用的jar包是apache的poi系类的jar包和ojdbc14.jar的数据库连接包。

poi的jar报的官方下载地址:http://poi.apache.org/

 

还需要说明是对execl的读取java可以通过横坐标和纵坐标读取到execl的每个单元格,

根据个人情况看需要读取execl表格中的那些内容,下面这个代码是通用的读取execl的代码

  1. for (int numSheet = 0; numSheet < workBook.getNumberOfSheets(); numSheet++) {  
  2.                 Sheet sheet = workBook.getSheetAt(numSheet);  
  3.                 if (sheet == null) {  
  4.                     continue;  
  5.                 }  
  6.                 // 循环行Row  
  7.                 for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {  
  8.                     Row row = sheet.getRow(rowNum);  
  9.                     if (row == null) {  
  10.                         continue;  
  11.                     }  
  12.                       
  13.                     // 循环列Cell  
  14.                     ArrayList<String> arrCell =new ArrayList<String>();  
  15.                     for (int cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) {  
  16.                         Cell cell = row.getCell(cellNum);  
  17.                         if (cell == null) {  
  18.                             continue;  
  19.                         }  
  20.                         publicExcel(cell); //publicExcel是下文中execl数据格式的转换,返回值即每个单元格的值                        }  
  21.                 }  
  22.             }  

 

一个完整的事例类
 

  1. public class ExeclOperate {  
  2.       
  3.     public static void main(String[] args) throws Exception {  
  4.         ExeclOperate e=new ExeclOperate();  
  5.         e.getExcel();  
  6.         System.out.println("导入完成!");  
  7.     }  
  8.     /** 
  9.      * 用于连接oracle数据库的方法 
  10.      * 只需修改中的参数getConnection("url","用户名","密码"); 
  11.      */  
  12.     public Connection conn(){  
  13.         try {  
  14.         //第一步:加载JDBC驱动  
  15.         Class.forName("oracle.jdbc.driver.OracleDriver");  
  16.         //第二步:创建数据库连接  
  17.         Connection con =DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.156:1521:tianjiao", "E5AVPS", "E5AVPS");  
  18.         return con;  
  19.         }catch(ClassNotFoundException cnf){  
  20.           System.out.println("driver not find:"+cnf);  
  21.           return null;  
  22.         }catch(SQLException sqle){  
  23.           System.out.println("can't connection db:"+sqle);  
  24.           return null;  
  25.         }  
  26.           catch (Exception e) {  
  27.         System.out.println("Failed to load JDBC/ODBC driver.");  
  28.         return null;  
  29.         }  
  30.     }  
  31.     /** 
  32.      * “95509”咨询清单  的读取 
  33.      * @throws Exception 
  34.      */  
  35.     public void getExcel() throws Exception {  
  36.         // 创建对Excel工作簿文件的引用       
  37.         HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream("D:\\1.xls"));       
  38.          // 创建对工作表的引用。  
  39.          // 在Excel文档中,第一张工作表的缺省索引是0,  
  40.          // 读取左上端单元  
  41.          for (int i = 0; i < workbook.getNumberOfSheets(); i++) {// 循环sheet  
  42.                  if(workbook.getSheetAt(i).getPhysicalNumberOfRows()>0){  
  43.                      HSSFSheet childSheet = workbook.getSheetAt(i);  
  44.                         for(int rowi=1;rowi<400000;rowi++){  
  45.                             //System.out.println(childSheet.getRow(rowi).getCell(1).equals(""));  
  46.                             if(childSheet.getRow(rowi).getCell(0).equals("") || childSheet.getRow(rowi).getCell(0)==null) break;  
  47.                             String cell1=this.publicExcel(childSheet.getRow(rowi).getCell(0));  
  48.                             if(cell1==null) break;  
  49.                             //对于double类型的数据装换为string类型进行字符串截取 只取整数。  
  50.                             cell1=cell1.substring(0, cell1.length()-2);  
  51.                             String cell2=this.publicExcel(childSheet.getRow(rowi).getCell(1));  
  52.                             String cell3=this.publicExcel(childSheet.getRow(rowi).getCell(2));  
  53.                             String cell4=this.publicExcel(childSheet.getRow(rowi).getCell(3));  
  54.                             String cell5=this.publicExcel(childSheet.getRow(rowi).getCell(4));  
  55.                             String cell6=this.publicExcel(childSheet.getRow(rowi).getCell(5));  
  56.                             cell6=cell6.substring(0, cell6.length()-2);  
  57.                             //拼装插入数据库的sql     
  58.                             String insert="insert into w_95509 values('"+cell1+"','"+cell2+"','"+cell3+"','"+cell4+"','"+cell5+"','"+cell6+"')";  
  59.                             System.out.println("SQL:"+insert);  
  60.                             insert(insert);  
  61.                               
  62.                         }     
  63.                 }  
  64.             }  
  65.     }  
  66.   
  67.         /** 
  68.          * execl数据格式的转换 
  69.          * @param cell 
  70.          * @return 
  71.          */  
  72.         public String publicExcel( HSSFCell cell){  
  73.             String value = null;          
  74.                   switch (cell.getCellType()) {  
  75.                   case HSSFCell.CELL_TYPE_NUMERIC:  
  76.                    value = "" + cell.getNumericCellValue();  
  77.                    break;  
  78.                   case HSSFCell.CELL_TYPE_STRING:  
  79.                    value = cell.getStringCellValue();  
  80.                    break;  
  81.                   case HSSFCell.CELL_TYPE_BLANK:  
  82.                    ;  
  83.                    break;  
  84.                   default:   
  85.                 }  
  86.             return value;  
  87.         }  
  88.           
  89.           
  90.         /** 
  91.          * 插入数据 只需要传入插入sql即可 
  92.          * 插入sql的样例:insert into t_department values('D004','金融部'); 
  93.          * @param insert 插入语句 
  94.          * @return 
  95.          * @throws SQLException  
  96.          */  
  97.         public int insert(String insert) throws SQLException{  
  98.             Connection conn = this.conn();  
  99.             int re = 0;  
  100.             try{  
  101.                 conn.setAutoCommit(false);//事物开始                  
  102.                 Statement sm = conn.createStatement();  
  103.                 re = sm.executeUpdate(insert);  
  104.                 if(re < 0){               //插入失败  
  105.                     conn.rollback();      //回滚  
  106.                     sm.close();  
  107.                     conn.close();    
  108.                     return re;  
  109.                 }  
  110.                 conn.commit();            //插入正常  
  111.                 sm.close();  
  112.                 conn.close();    
  113.                 return re;  
  114.             }  
  115.             catch(Exception e){  
  116.                 e.printStackTrace();  
  117.             }  
  118.             conn.close();    
  119.             return 0;  
  120.               
  121.         }   
  122.   
  123.     }  

 

posted @ 2016-04-15 15:55  dirgo  阅读(765)  评论(0编辑  收藏  举报