当前位置:
文档之家› 利用JAVA如何读取EXCEL中的数据
利用JAVA如何读取EXCEL中的数据
} }
/** * 读取 Excel 中的数据.将这些数据放入到一个三维数组中. * @author Administrator * @param filePath 文件路径. * @deprecated:读取 Excel 中的数据将它放入到 ArrayList 数组中(此为 三维数组). */ public static ArrayList readExcel(String filePath) {
import jxl.Cell; import jxl.Sheet; import jxl.Workbook;
/** * <p>Title: </p> * <pre>Description: Excel 数据导入到 oracle 数据库类. * </pre> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: 易通技术有限公司</p> * @author Allen * @version 1.0 */ public class ExcelImportOracle {
/** * 将 readExcel 方法读出来的三维数组数据转换成二维数组数据. * @param al * @return 返回二维数组数据. */ public static ArrayList getDisplayData(ArrayList al) { ArrayList result = new ArrayList(); for(int i = 0;i < al.size();i ++)
/** * 用于返回三维数组的 ArrayList.
*/ private static ArrayList subdata = new ArrayList();
/** * Excel 中的表名.
*/ private static String tablename;
/** * 文件的路径
*/ private static String filePath;
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;
/** * 该方法为完成读取 Excel 中的数据并将数据插入到对应的数据库表中 的操作(在调用前需要先调用 setFilePath(String)这个方法.). * @author Administrator * @param data:读取 Excel 中的数据的数组. * @deprecated:将读取 Excel 中的数据插入到对应的数据库表 中. *
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 = StringUtil.toISO(c00.getContents().t rim()); // 获得 cell 具体类型值的方式得到内容. al.add(j, strc00); } alList.add(b, al);
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 中页的列表.
#xuewei2007 发表于 2008-02-18 14:50:07 IP: 168.160.249.* 想问你下,就是在那个 InsertData 方法 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);
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(); } }
/** * 将读取的 Excel 的三维数组数据进行对应的数据库表插入操作. * @author Administrator * @param data:读取 Excel 中的数据的数组.
* @deprecated:将读取 Excel 中的数据插入到对应的数据库表中. */ private static void InsertData(ArrayList data) {
if(file.exists()) { result = file.delete();
} else
{ System.out.println("文件没有找到,无法删除!"); }
if(result) { System.out.println("删除成功!");
} else
{ System.out.println("删除失败!"); } } return subdata; }
{ 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) { }
} WfdefineDateOperate 这个是怎么用的,它有什么作用呢?等待您的解答. 2008-02-19 08:45:20 作者回复 此类的 currecorde 方法的作用就是将 Contents 数据持久化到 tablename 表中。我写的是静态方法直接调用即可! #xuewei2007 发表于 2008-02-19 13:39:59 IP: 168.160.249.* 其实我已经想到了你这个应该是起的这么个作用的, 如果方便的话,可以将你的那个 WfdefineDateOperate 类,也就是你数据 持久化的这个过程给我看看吗? 2008-02-20 09:27:49 作者回复 此类涉及数据持久化底层操作,内容较多,请见谅! #xuewei2007 发表于 2008-02-19 13:45:26 IP: 168.160.249.* 还有我还有其他的问题,就是我发现,好象在读取数据的时候,始终是从第 二行数据开始读的,不知道你碰到这个问题没?
}
发表于 @ 2006 年 07 月 21 日 10:20:00|评论(loading... )|编辑
旧一篇: 关于 Excel 中的数据如何导入到 oracle 数据库中.
评论
#xuewei2007 发表于 2008-02-18 14:43:42 IP: 168.160.249.* 您好! 新年快乐! 看了你的这篇文章后,自己也尝试着操作,可是没有成功.请问可不可以留 下联系,便于请问. MSN :xuewei2007@ 2008-02-19 08:46:15 作者回复 我现在很少上MSN,不过 blog 我经常来,我的 MSN:apicescn@
JAVA EXCEL API 简介
Java Excel 是一开放源码项目,通过它 Java 开发人员可以读取 Excel 文件的内 容、创建新的 Excel 文件、更新已经存在的 Excel 文件。使用该 API 非 Windows 操作系统也可以通过纯 Java 应用来处理 Excel 数据表。因为是使用 Java 编写的, 所以我们在 Web 应用中可以通过 JSP、Servlet 来调用 API 实现对 Excel 数据表 的访问。
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;