java读取excel

2018-08-27 11:16:51 浏览数 (1)

代码语言:javascript复制
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;
	}
}

0 人点赞