标签:min util cell import continue .sh tco cep ext
/**. */ 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(); } } }
标签:min util cell import continue .sh tco cep ext
原文地址:https://www.cnblogs.com/lh-masteryi/p/9087208.html