关于Java读取Excel2007的文章在Google、百度上搜索一下,没有太好的例子,实现的也不算太好。查看了一下Poi,最新 的 POI 3.5 beta 4 支持读写 Excel2007和PPT2007(XLSX and PPTX),自己来实现Java读取Excel2007了。 1,下载 POI 3.5 beta 4 解压,把其中的jar包导入项目文件。以我的读取为例,导入了以下jar包。 没有配置 log4j,测试时报告警报信息,应该为加载顺序导致的初始化问题造成(暂时没有找原因)。 2,建立读取 Excel2007 方法 Java 代码 // 构造 XSSFWorkbook 对象,strPath 传入文件路径 XSSFWorkbook xwb = new XSSFWorkbook(strPath); // 读取第一章表格内容 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(""); } // 构造 XSSFWorkbook 对象,strPath 传入文件路径 XSSFWorkbook xwb = new XSSFWorkbook(strPath); // 读取第一章表格内容 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(""); } 此过程直接传入文件所在路径,其他一些过程已经被自动实现。 3,测试,文件为8列3563行数据,以文本方式读取内容, Java 代码 public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS"); TimeZone t = sdf.getTimeZone(); t.setRawOffset(0); sdf.setTimeZone(t); Long startTime = System.currentTimeMillis(); String fileName = "F:\\我的文档\\学生缴费代码.xlsx"; // 检测代码 try { PoiReadExcel er = new PoiReadExcel(); // 读取excel2007 er.testPoiExcel2007(fileName); } catch (Exception ex) { Logger.getLogger(FastexcelReadExcel.class.getName()).log(Level.SEVERE, null, ex); } Long endTime = System.currentTimeMillis(); System.out.println("用时:" + sdf.format(new Date(endTime - startTime))); } public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS"); TimeZone t = sdf.getTimeZone(); t.setRawOffset(0); sdf.setTimeZone(t); Long startTime = System.currentTimeMillis(); String fileName = "F:\\我的文档\\学生缴费代码.xlsx"; // 检测代码 try { PoiReadExcel er = new PoiReadExcel(); // 读取excel2007 er.testPoiExcel2007(fileName); } catch (Exception ex) { Logger.getLogger(FastexcelReadExcel.class.getName()).log(Level.SEVERE, null, ex); } Long endTime = System.currentTimeMillis(); System.out.println("用时:" + sdf.format(new Date(endTime - startTime))); } 读取所用时间为:4.172秒
2010-05-27 20:42 Java 操作 Excel (读取Excel2007,Poi实现)
猜你喜欢
转载自terry0501.iteye.com/blog/1729520
今日推荐
周排行