java读取excel表格中的数据

import java.io.FileInputStream;
import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import https://www.360docs.net/doc/105183302.html,ermodel.HSSFRow;
import https://www.360docs.net/doc/105183302.html,ermodel.HSSFSheet;
import https://www.360docs.net/doc/105183302.html,ermodel.HSSFWorkbook;
import https://www.360docs.net/doc/105183302.html,ermodel.XSSFRow;
import https://www.360docs.net/doc/105183302.html,ermodel.XSSFSheet;
import https://www.360docs.net/doc/105183302.html,ermodel.XSSFWorkbook;
import com.longrise.LEAP.Base.Global;
public class InsertExcel {

public static void readExcel2003(String fileName) {
Connection conn= null;
Statement stmt = null;
CallableStatement cs = null;
try {
String insertSql = "";
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@192.168.137.46:1521:leap"; //orcl为数据库的SID
String user="dbo";
String password="longrise";
conn= DriverManager.getConnection(url,user,password);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
//ResultSet rs = stmt.executeQuery("select * from EXCELDATA");
//https://www.360docs.net/doc/105183302.html,st();
//System.out.println("^^"+rs.getRow());
HSSFSheet sheet = null;
// 定义 row、cell
HSSFRow row = null;
// HSSFCell cell;
String cell = null;
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(fileName));
// 读取文件中的第一张表格
int sheetNum = wb.getSelectedTab()+1;
System.out.println("num**"+sheetNum);
for(int ii=0; ii sheet = wb.getSheetAt(ii);
// 循环输出表格中的内容
for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
insertSql = "insert into EXCELDATA(name,md1,md2,md3,id) values ('owner','";
row = sheet.getRow(i);
for (int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++) {
// 推荐通过 row.getCell(j).toString() 获取单元格内容,
if(row.getCell(j) != null){
cell = row.getCell(j).toString();
if(j==1 || j==2){
insertSql = insertSql+cell+"','";
}else if(j==3){
insertSql = insertSql+cell+"',";
}
}
}
insertSql = insertSql+"sys_guid())";
System.out.println("sql&&"+insertSql);
stmt.execute(insertSql);
}
}
String sql = "{call UPDATEexceldata()}";
cs = conn.prepareCall(sql);
cs.execute();
} catch (Exception e) {
System.out.println(e);
}finally{
if(conn != null){
try {
cs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void readExcel2007(String fileName) {
try {
XSSFWorkbook xwb = new XSSFWor

kbook(fileName);
// 读取第一章表格内容
XSSFSheet sheet = xwb.getSheetAt(0);
// 定义 row、cell
XSSFRow row;
String cell;
// 循环输出表格中的内容
for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
for (int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++) {
// 通过 row.getCell(j).toString() 获取单元格内容,
cell = row.getCell(j).toString();
System.out.print(cell + "\t");
}
System.out.println("");
}
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
TimeZone t = sdf.getTimeZone();
t.setRawOffset(0);
sdf.setTimeZone(t);
Long startTime = System.currentTimeMillis();
//String fileName2007 = "F:\\sad.xls";
String fileName2003 = "F:\\df\\2.xls";//路径
// 检测代码
try {
// 读取excel2007
//readExcel2007(fileName2007);
// 读取excel2003
readExcel2003(fileName2003);
} catch (Exception e) {
System.out.println(e);
}
Long endTime = System.currentTimeMillis();
System.out.println("用时:" + sdf.format(new Date(endTime - startTime)));
}
}

相关文档
最新文档