java采用POI读取excel的问题(在线)
我从网上拷了下面的代码 运行时出错说
E:\> javac POITest.java
Note: POITest.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
(jdk1.4 POI)
代码如下
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
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;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class POITest {
public static Connection conn = null;
public static Statement stmt = null;
public static boolean connectDB2() {
try {
Class.forName( "oracle.jdbc.driver.OracleDriver ");
conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:CF132 ", "CHENYU ", "123456 ");
stmt = conn.createStatement();
}
//捕获加载驱动程序异常
catch (ClassNotFoundException cnfex) {
System.err.println( "装载JDBC驱动程序失败。 ");
cnfex.printStackTrace();
return false;
}
//捕获连接数据库异常
catch (SQLException sqlex) {
System.err.println( "无法连接数据库 ");
sqlex.printStackTrace();
//System.exit(1); // terminate program
return false;
}
return true;
}
public static boolean readExcelToDB2() {
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
try {
fs = new POIFSFileSystem(new FileInputStream( "e:\\test.xls "));
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
return false;
}
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = null;
HSSFCell cell = null;
String name = " ";
String aa = " ";
int rowNum, cellNum;
int i;
rowNum = sheet.getLastRowNum();
for (i = 0; i <= rowNum; i++) {
row = sheet.getRow(i);
//cellNum = row.getLastCellNum();
cell = row.getCell((short) 0);
name = cell.getStringCellValue();
cell = row.getCell((short) 1);
aa = cell.getStringCellValue();
String sql = "insert into test(ID, NAME) values( ' "+aa+ " ', ' "+name+ " ') ";
try {
stmt.executeUpdate(sql);
} catch (SQLException e1) {
e1.printStackTrace();
return false;
}
}
return true;
}
public static void main(String[] args) {
if (connectDB2()==true){
if (readExcelToDB2()==true)
System.out.println( "数据导入成功 ");
else
System.out.println( "数据导入失败 ");
}
else{
System.out.println( "数据库连接失败 ");
}
}
}
[解决办法]
你不是包没引对,就是JDK有问题,用JDK5.0吧
[解决办法]
用 cell.setCellValue(new HSSFRichTextString( "jhljkjljklj "))