1. 程式人生 > >Excel解析以及POI一些方法的介紹

Excel解析以及POI一些方法的介紹

setw 當前 ets nts last pan args fileinput 介紹

/**.
 */

package com.encdata.lihao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**. * * @author admin * */ public class ExcelParseTwo { public static void main(String[] args) throws IOException { File file = new File("D://123.xlsx"); try { InputStream is = new FileInputStream(file); Workbook wb = null; if (file.getName().endsWith("
xls")) { //Excel 2003 wb = new HSSFWorkbook(is); } else if(file.getName().endsWith("xlsx")) { // Excel 2007/2010 wb = new XSSFWorkbook(is); } int sheetCount = wb.getNumberOfSheets(); for (int i=0;i<sheetCount;i++) {
/* * 如果需要跳過第一行目錄的化,設置一個count */ int rowCount = 0; Sheet sheet = wb.getSheetAt(i); for (Row row : sheet) { /** * 如果存在第一行目錄的化,則跳過。 */ /*if (rowCount == 0) { rowCount++; continue; }*/ StringBuffer sb = new StringBuffer(); /** * 方式1--For循環 */ /*for (Cell cell : row) { if(cell.toString() != null) { sb.append(cell.toString() + "*"); } }*/ /** * 方式1--For循環-2 */ /*int cellCount = row.getPhysicalNumberOfCells(); for(int j=0;j<cellCount;j++){ sb.append(row.getCell(j)+"*"); }*/ /** * 方式2--叠代 */ /*Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { sb.append(cellIterator.next()+"*"); }*/ System.out.println(sb); /** * row.getRowNum() * 獲取當前row對應的下標 */ /*System.out.println(row.getRowNum());*/ /** * row.getPhysicalNumberOfCells() * 獲取當前row有多少cell * row.getCell(i) * 獲取對應下標下面的cell的value */ /*System.out.println(row.getPhysicalNumberOfCells()); System.out.println(row.getCell(i));*/ System.out.println(row.getFirstCellNum()); System.out.println(row.getLastCellNum()); } } } catch (FileNotFoundException e) { e.printStackTrace(); } } }

Excel解析以及POI一些方法的介紹