Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package POI; /** * * @author John */ import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.ss.usermodel.Cell; /** * ExcelWorkbook? Apache POI * * @author hyjiacan * */ public class Sheet { HSSFSheet sheet = null; private int firstRowNum;// ? private int lastRowNum;// ?? /** * HSSFSheetSheet * * @param sheet */ public Sheet(HSSFSheet sheet) { this.sheet = sheet; this.firstRowNum = sheet.getFirstRowNum(); this.lastRowNum = sheet.getLastRowNum(); } /** * ?? * * @return ?? */ public int getFirstRowNum() { return firstRowNum; } /** * ??? * * @return ??? */ public int getLastRowNum() { return lastRowNum; } /** * ??? * * @return ?? */ public String getName() { return sheet.getSheetName(); } /** * ?? * * @return * @see HSSFSheet */ public int getRowSize() { return sheet.getLastRowNum() - sheet.getFirstRowNum(); } /** * ?? * * @return * @see HSSFSheet */ public int getColumnSize() { HSSFRow row = sheet.getRow(this.getFirstRowNum()); return row.getLastCellNum() - row.getFirstCellNum(); } /** * ??? * * @return ?? */ public String[] getColumnNames() { ArrayList<String> h = getRowAt(this.firstRowNum); String[] head = new String[h.size()]; int i = 0; for (String o : h) { if (!o.equals("") || !o.equals(null)) { head[i++] = String.valueOf(o); } } return head; } /** * ?? * * @param index ? * @return ? * @see HSSFRow * @see HSSFCell */ public ArrayList<String> getRowAt(int index) { HSSFRow row = sheet.getRow(index); ArrayList<String> cells = new ArrayList<String>(); int i = row.getFirstCellNum(); while (i < this.getColumnSize()) { HSSFCell cell = row.getCell(i++); if (cell == null) { cells.add(""); } else { Object val = null; switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: val = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_FORMULA: val = cell.getCellFormula(); break; case Cell.CELL_TYPE_NUMERIC: val = cell.getNumericCellValue(); break; case Cell.CELL_TYPE_STRING: val = cell.getStringCellValue(); default: val = cell.getRichStringCellValue(); } cells.add(String.valueOf(val)); } } return cells; } public Object[] getObjectRowAt(int index) { return getRowAt(index).toArray(); } public Object[][] getObjectAllRow() { Object[][] ooDate = new Object[getRowSize()][]; for (int i = 0; i < getRowSize(); i++) { ooDate[i] = getObjectRowAt(i + 1); } return ooDate; } public ArrayList<ArrayList> getAllRow() { ArrayList<ArrayList> aalist = new ArrayList<ArrayList>(); ArrayList alist = new ArrayList(); int maxRowSize = getRowSize(); for (int i = 1; i <= maxRowSize; i++) { alist.add(getRowAt(i)); aalist.add((ArrayList) alist.clone()); alist.removeAll(alist); } return aalist; } // public Object[][] getAllRowToArray() { // } /** * Table * * @return table * @see Table */ public Table getAsTable() { Table table = Table.build(this.getRowSize(), this.getColumnSize()); table.setHeader(getColumnNames()); table.setName(sheet.getSheetName()); for (int i = this.getFirstRowNum() + 1; i <= this.getRowSize(); i++) { table.addRow(this.getRowAt(i)); } return table; } }