package com.wonders.tjj.macro.web.action.importdatautil;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
public class LoadExcelToOracle {
/**
* @param args
*/
public List loadExcel() {
List list = new ArrayList();
try {
int ROW_HEAD_COUNT = 1; //Excel中表头所占的行数
int COLUMN_HEAD_COUNT = 0;//Excel中表头所占的列数
Workbook book = Workbook.getWorkbook(new File(
"f:/code_info.xls"));
Sheet sheet = book.getSheet(0);
int rowCount = sheet.getRows();// 得到行数
int columnCount = sheet.getColumns();// 得到列数
//添加或更新数据
for (int row = ROW_HEAD_COUNT; row < rowCount; row++) {
Cell cell = sheet.getCell(COLUMN_HEAD_COUNT, row);
List excelDateList = new ArrayList();
//从第1栏开始读取数据
for (int column = COLUMN_HEAD_COUNT; column < columnCount; column++) {
cell = sheet.getCell(column, row);
String strData = null;
if (cell != null && cell.getContents() != null
&& cell.getContents().length() > 0) {
strData = cell.getContents();
}
excelDateList.add(strData);
}
list.add(excelDateList);
}
book.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
/**
* @param list
*/
public void saveCodeInfo(List list) {
try {
Connection con = DBHelper.getConnection();
String sql = "insert into YDK_CODE_INFO values (?,?,?,?,?,?,?)";
PreparedStatement ps = con.prepareStatement(sql);
int count = 1;
int closeCon = 1;
for (int j = 0; j < list.size(); j++) {
List excelDateList = (List) list.get(j);
for (int i = 0; i < excelDateList.size(); i++) {
if (count == 8) {
count = 1;
}
ps.setObject(count, excelDateList.get(i));
count++;
}
closeCon++;
ps.executeQuery();
System.out.println("插入成功");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
LoadExcelToOracle leto = new LoadExcelToOracle();
List list = leto.loadExcel();
leto.saveCodeInfo(list);
}
}