2010-03-18
import java.io.*;
import java.util.*;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
/** */
/**
* <p>
* Title:
* </p>
* <div>Description: Excel数据导入到oracle数据库类. </div>
* <p>
* Copyright: Copyright (c) 2004
* </p>
* <p>
* Company: 易通技术有限公司
* </p>
*
* @author Allen
* @version 1.0
*/
public class ExcelImportOracle {
/** */
/**
* 用于返回三维数组的ArrayList.
*/
private static ArrayList subdata = new ArrayList();
/** */
/**
* Excel中的表名.
*/
private static String tablename;
/** */
/**
* 文件的路径
*/
private static String filePath;
/** */
/**
* 该方法为完成读取Excel中的数据并将数据插入到对应的数据库表中的操作(在调用前需要先调用 setFilePath(String)这个方法.).
*
* @author Administrator
* @param data:读取Excel中的数据的数组.
* @dedivcated:将读取Excel中的数据插入到对应的数据库表中. *
*/
public static void ExcelDataImportOracle(String filePath) throws Exception {
try {
ArrayList al = readExcel(filePath);
InsertData(al);
} catch (Exception e) {
e.printStackTrace();
}
}
/** */
/**
* 读取Excel中的数据.将这些数据放入到一个三维数组中.
*
* @author Administrator
* @param filePath
* 文件路径.
* @dedivcated:读取Excel中的数据将它放入到ArrayList数组中(此为三维数组).
*/
public static ArrayList readExcel(String filePath) {
try {
subdata.clear();// 将静态ArrayList数组清空.(如果不清空原数据会不断累加)
InputStream is = new FileInputStream(filePath);
Workbook rwb = Workbook.getWorkbook(is);
// Sheet st = rwb.getSheet(0);//这里有两种方法获取sheet表,1为名字,而为下标,从0开始
// Sheet st = rwb.getSheet("Book1");// Excel中第一页的页名称.
Sheet st[] = rwb.getSheets();// 得到所有Excel中页的列表.
for (int a = 0; a < st.length; a++) {
ArrayList alList = new ArrayList();
ArrayList tablenames = new ArrayList();
ArrayList tableAndContents = new ArrayList();
tablename = st[a].getName().trim();
int b = 0;
for (int i = 1; i < st[a].getRows(); i++) {
ArrayList al = new ArrayList();
for (int j = 0; j < st[a].getColumns(); j++) {
Cell c00 = st[a].getCell(j, i);
// 通用的获取cell值的方式,返回字符串
String strc00 = c00.getContents().trim();
// 获得cell具体类型值的方式得到内容.
al.add(j, strc00);
}
alList.add(b, al);
b++;
}
tablenames.add(tablename);
tableAndContents.add(0, tablenames);
tableAndContents.add(1, alList);
subdata.add(a, tableAndContents);
}
rwb.close();
// 关闭
// System.out.println(subdata);// 输出
} catch (Exception e) {
e.printStackTrace();
} finally {
// //删除上传文件
// File file = new File(filePath);
// boolean result = false;
//
// if(file.exists())
// {
// result = file.delete();
// }
// else
// {
// System.out.println("文件没有找到,无法删除!");
// }
//
// if(result)
// {
// System.out.println("删除成功!");
// }
// else
// {
// System.out.println("删除失败!");
// }
}
return subdata;
}
/** */
/**
* 将读取的Excel的三维数组数据进行对应的数据库表插入操作.
*
* @author Administrator
* @param data:读取Excel中的数据的数组.
* @dedivcated:将读取Excel中的数据插入到对应的数据库表中.
*/
private static void InsertData(ArrayList data) {
try {
String tablename;
ArrayList Contents = new ArrayList();
for (int i = 0; i < data.size(); i++) {
tablename = ((ArrayList) ((ArrayList) data.get(i)).get(0)).get(
0).toString();
Contents = (ArrayList) ((ArrayList) data.get(i)).get(1);
// WfdefineDateOperate.currecorde(tablename,Contents);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/** */
/**
* 得到文件路径;
*
* @return filePath
*/
public String getFilePath() {
return filePath;
}
/** */
/**
* 设置文件路径的位置;
*
* @param filePath
*/
public void setFilePath(String filePath) {
ExcelImportOracle.filePath = filePath;
}
/** */
/**
* 将readExcel方法读出来的三维数组数据转换成二维数组数据.
*
* @param al
* @return 返回二维数组数据.
*/
public static ArrayList getDisplayData(ArrayList al) {
ArrayList result = new ArrayList();
for (int i = 0; i < al.size(); i++) {
ArrayList tmp = (ArrayList) ((ArrayList) al.get(i)).get(1);
for (int j = 0; j < tmp.size(); j++) {
result.add(tmp.get(j));
}
}
return result;
}
/** */
/**
* @param args
*/
public static void main(String[] args) {
List<List> list = getDisplayData(readExcel("D:/aaa.xls"));
for(List<String> l:list){
for (String s : l) {
System.out.println("-----+"+s+"+-----");
}
System.out.println("");
}
}
}
jxl.jar官方下载