poi读取Excel2007文件
excel2007同之前excel97-2003在存储数据量上有很大差别,可以使用apache下的poi来实现 poi版本3.6以上
?
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class ReadExcel {private static XSSFWorkbook getXSSFWorkbook() {String filePath = "D:\\new.xlsx";XSSFWorkbook wordbook =null;try { // 文件流指向excel文件FileInputStream fin = new FileInputStream(filePath); wordbook = new XSSFWorkbook(fin);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}// 调用解析XSSFWordbook类的方法,解析并封装该文件return wordbook;}public static ArrayList<String[]> readXSSFAllRows() {XSSFWorkbook wordbook =getXSSFWorkbook();ArrayList<String[]> rowList = new ArrayList<String[]>();try {XSSFSheet sheet = null;XSSFRow row = null;XSSFCell cell = null;sheet = wordbook.getSheetAt(0);int rowNum = 0;for (Iterator<?> rows = sheet.iterator(); rows.hasNext(); rowNum++) {row = (XSSFRow) rows.next();int col = 0;int lastCellNum = (int) row.getLastCellNum();String[] aCells = new String[lastCellNum];while (col < lastCellNum) {try {cell = row.getCell(col);aCells[col] = row.getCell(col).toString();;} catch (Exception ex) {ex.printStackTrace();}col++;}boolean notBlankLine = false;for (int k = 0; k < aCells.length; k++) {if (aCells[k] != null && aCells[k].length() > 0) {notBlankLine = true;break;}}if (notBlankLine) {System.out.print(aCells[0]);System.out.print(aCells[1]);System.out.println(aCells[2]);//System.out.println("*******************************************");rowList.add(aCells);}}} catch (Exception ex) {ex.printStackTrace();}return rowList;}/** * @param args */public static void main(String[] args) {ReadExcel.readXSSFAllRows();}}
?
?