java读取excel

来源:互联网 发布:js do while循环 编辑:程序博客网 时间:2024/06/06 13:13
package edu.qdgxy.util;import jxl.Cell;import jxl.Sheet;import java.io.File;import java.io.InputStream;import java.io.Serializable;import java.util.Vector;import java.io.FileInputStream;import org.apache.struts2.ServletActionContext;import jxl.Workbook;/** * 耗时315.469秒 jxl.jar excel导入SQL 包 耗时131.469秒 耗时125.448秒(系数6000) */public class ExcelRead2 implements Serializable{public static void main(String[] args) {ExcelRead2 cr = new ExcelRead2();long start = System.currentTimeMillis();// 记录开始时间cr.readExcel();// 开始执行long end = System.currentTimeMillis();System.out.println("============耗時:====" + ((end - start) / 1000.0));}public  Vector<Cell[]> readExcel() {String path="E:\\test";File fileDir = new File(path);File files[] = fileDir.listFiles();String filePath=files[0].getAbsolutePath();System.out.println(filePath);Vector<Cell[]> v = new Vector<Cell[]>();Workbook rwb = null;try {InputStream is = new FileInputStream(filePath);rwb = Workbook.getWorkbook(is);// 获取第一张Sheet表Sheet rs = rwb.getSheet(0);System.out.println("共"+rs.getRows()+"行");for (int i = 0; i < rs.getRows(); i++) {boolean bool=false;for (int j = 0; j < rs.getColumns(); j++) {if(rs.getCell(j, i).getContents().trim().isEmpty()==false){bool=true;//只要有一个单元非null即有效行System.out.println(rs.getCell(j, i).getContents().trim());break;}}if(bool){v.add(rs.getRow(i));// 放入一行记录}}is.close();rwb.close();System.gc();} catch (Exception e) {e.printStackTrace();}return v;}}

原创粉丝点击