通过POI将Excel数据导入数据库

本文介绍了一种将Excel文件中的数据导入到Oracle数据库的方法。通过Java编程实现,涉及使用Apache POI处理Excel文件及JDBC操作数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上代码
连接数据库类:  
  
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.SQLException;  
  
public class DBConnection {  
  
private String classString="oracle.jdbc.driver.OracleDriver";  
private String username="benz";  
private String password="benz";  
private String url="java:oracle:thin:@192.168.1.1:1521:bhdba";  
private Connection con=null;  
  
public Connection getConnection(){  
   try {  
    Class.forName(classString);  
    con=DriverManager.getConnection(url,username,password);  
   } catch (ClassNotFoundException e) {  
    e.printStackTrace();  
   } catch (SQLException e) {  
    e.printStackTrace();  
   }  
   return con;  
}  
  
}  
  
具体操纵类:  
  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.IOException;  
import java.sql.Connection;  
import java.sql.Date;  
import java.sql.PreparedStatement;  
import java.sql.SQLException;  
import java.text.ParseException;  
import java.text.SimpleDateFormat;  
  
import org.apache.poi.hssf.usermodel.HSSFCell;  
import org.apache.poi.hssf.usermodel.HSSFRow;  
import org.apache.poi.hssf.usermodel.HSSFSheet;  
import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  
public class ExcelToDB {  
  
private Connection con;  
private DBConnection db;  
private PreparedStatement pst;  
private String filePath="f:\\test.xls";  
  
public boolean insertDB(){  
    
   boolean flag=true;  
   db=new DBConnection();  
   con=db.getConnection();  
   try {  
    //文件流指向excel文件  
    FileInputStream fin=new FileInputStream(filePath);  
    HSSFWorkbook workbook=new HSSFWorkbook(fin);//创建工作薄  
    HSSFSheet sheet=workbook.getSheetAt(0);//得到工作表  
    HSSFRow row=null;//对应excel的行  
    HSSFCell cell=null;//对应excel的列  
     
    int totalRow=sheet.getLastRowNum();//得到excel的总记录条数  
    //以下的字段一一对应数据库表的字段  
    String bookName="";  
    String bookAuthor="";  
    String bookPublish="";  
    Date bookDate=null;  
    String bookIsbn="";  
    int bookPage=0;  
    float bookPrice=0.0f;  
     
    String sql="insert into book(ID,BOOK_NAME,BOOK_AUTHOR,BOOK_PUBLISH," +  
      "BOOK_DATE,BOOK_ISBN,BOOK_PAGE,BOOK_PRICE) " +  
      "values(SEQ_BOOK.NEXTVAL,?,?,?,?,?,?,?)"; //SEQ_BOOK.NEXTVAL为数据库表序列  
     //int = 1 因为第一行是标题,所以从1开始
    for(int i=1;i<=totalRow;i++){  
     row=sheet.getRow(i);  
     cell=row.getCell(0);  //这里列是从0开始计算的
     if(cell!=null){ //如果列是控制,cell为null,所以要判断是否为null
//这里我碰到一个异常 Cannot get a text value from a numeric cell
异常描述:在导入excel的时候在获取excel单元格数据的时候会出现Cannot get a text value from a numeric cell的异常抛出。
异常原因:poi读取excel单元格的数据,cell有不同的数据类型(CELL_TYPE_NUMERIC,CELL_TYPE_STRING,CELL_TYPE_FORMULA),如果cell中的数据是数值的话,如果你没有给他设置cell的类型的话。默认会认为是CELL_TYPE_NUMERICl类型,如果从一个NUMBER类型的Cell使用.cell.getStringCellValue()读取出一个字符串就会出错。
解决的方法:在读取数据之前,设置cell的类型为CELL_TYPE_STRING;
                         cell.setCellType(Cell.CELL_TYPE_STRING);
      cell.setCellType(cell.CELL_TYPE_STRING);  
     bookName=cell.getRichStringCellValue().toString(); 
     } 
     if(cell!=null){
     cell=row.getCell(1); 
      cell.setCellType(Cell.CELL_TYPE_STRING); 
     bookAuthor=cell.getRichStringCellValue().toString();  
   }
     if(cell!=null){
     cell=row.getCell(2);  
     cell.setCellType(Cell.CELL_TYPE_STRING); 
     bookPublish=cell.getRichStringCellValue().toString();  
}
      
     cell=row.getCell(3);  
     //格式化字符串时间  
     SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");  
     bookDate=new Date((format.parse(cell.getRichStringCellValue().toString())).getTime());  
      
     cell=row.getCell(5);  
     bookIsbn=cell.getRichStringCellValue().toString();  
     cell=row.getCell(6);  
     bookPage=Integer.parseInt(cell.getRichStringCellValue().toString());  
     cell=row.getCell(7);  
     bookPrice=Float.parseFloat(cell.getRichStringCellValue().toString());  
      
     pst=con.prepareStatement(sql);  
     pst.setString(1,bookName);  
     pst.setString(2,bookAuthor);  
     pst.setString(3,bookPublish);  
     pst.setDate(4,bookDate);  
     pst.setString(5,bookIsbn);  
     pst.setInt(6,bookPage);  
     pst.setFloat(7,bookPrice);  
      
     pst.execute();  
    }  
     
     
   } catch (FileNotFoundException e) {  
    flag=false;  
    e.printStackTrace();  
   } catch(IOException ex){  
    flag=false;  
    ex.printStackTrace();  
   } catch(SQLException exx){  
    flag=false;  
    exx.printStackTrace();  
   } catch(ParseException exxx){  
    exxx.printStackTrace();  
   }finally{  
    try {  
     if(pst!=null){
     pst.close();  
	}
     if(con!=null){
     con.close(); 
	} 
    } catch (SQLException e) {  
     e.printStackTrace();  
    }  
   }  
   return flag;  
    
}  
  
public static void main(String args[]){  
   ExcelToDB toDB=new ExcelToDB();  
   toDB.insertDB();  
}  
  
}  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值