Look at the code first , Explain sentence by sentence :
Generally, there are two ways to traverse ,1: Get the total number of rows and the number of columns per row , Then the cycle .2: Use iteration
First look at the first one :
package com.golden.test;import java.io.File;import java.io.FileInputStream;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.hssf.usermodel.HSSFWorkbook;/** * @author Cui suqiang */public class PoiReadXls2 { public static void main(String[] args) { File f = new File("c:\\a.xls"); try { FileInputStream is = new FileInputStream(f); HSSFWorkbook wbs = new HSSFWorkbook(is); HSSFSheet childSheet = wbs.getSheetAt(0); // System.out.println(childSheet.getPhysicalNumberOfRows()); System.out.println(" There are lines " + childSheet.getLastRowNum()); for (int j = 0; j < childSheet.getLastRowNum(); j++) { HSSFRow row = childSheet.getRow(j); // System.out.println(row.getPhysicalNumberOfCells()); // System.out.println(" Number of columns " + row.getLastCellNum()); if (null != row) { for (int k = 0; k < row.getLastCellNum(); k++) { HSSFCell cell = row.getCell(k); if (null != cell) { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: // Numbers System.out.print(cell.getNumericCellValue() + " "); break; case HSSFCell.CELL_TYPE_STRING: // character string System.out.print(cell.getStringCellValue() + " "); break; case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean System.out.println(cell.getBooleanCellValue() + " "); break; case HSSFCell.CELL_TYPE_FORMULA: // The formula System.out.print(cell.getCellFormula() + " "); break; case HSSFCell.CELL_TYPE_BLANK: // Null value System.out.println(" "); break; case HSSFCell.CELL_TYPE_ERROR: // fault System.out.println(" "); break; default: System.out.print(" Unknown type "); break; } } else { System.out.print("- "); } } } System.out.println(); } } catch (Exception e) { e.printStackTrace(); } }}
obtain Excel And then read , This is very simple . There are two key points , Maybe you'll see some use it this way and others use it that way on the Internet .
System.out.println(" There are lines " + childSheet.getLastRowNum());System.out.println(childSheet.getPhysicalNumberOfRows());System.out.println(" Number of columns " + row.getLastCellNum());System.out.println(row.getPhysicalNumberOfCells());
If everyone copies the code for use , I don't know the difference . Too many differences do not know , But I found one thing , That is, if there are rows or columns in the middle getPhysicalNumberOfRows and getPhysicalNumberOfCells You cannot read all rows and columns .
also , Be sure to judge the format of cells switch (cell.getCellType()), Different cell formats use different methods . Finally, add the end type , In case of a one thousand .
And in the number type , It is also divided into pure numbers and time formats :
case HSSFCell.CELL_TYPE_NUMERIC: // Numerical type if (HSSFDateUtil.isCellDateFormatted(cell)) { // If it is date Type is , Get the cell Of date value value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString(); } else { // Pure number value = String.valueOf(cell.getNumericCellValue());}
There is also an iterative method :
package com.golden.test;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Iterator;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.hssf.usermodel.HSSFWorkbook;import org.apache.poi.poifs.filesystem.POIFSFileSystem;/** * * @author Cui suqiang * */public class PoiReadXls { @SuppressWarnings( { "unchecked", "deprecation" }) public static void main(String[] args) { File f = new File("c:\\a.xls"); try { InputStream input = new FileInputStream(f); POIFSFileSystem fs = new POIFSFileSystem(input); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); Iterator rows = sheet.rowIterator(); while (rows.hasNext()) { HSSFRow row = (HSSFRow) rows.next(); // System.out.print(" That's ok :" + row.getRowNum() + " "); Iterator cells = row.cellIterator(); while (cells.hasNext()) { HSSFCell cell = (HSSFCell) cells.next(); // System.out.println(" Column :" + cell.getCellNum()); switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: // Numbers System.out.print(cell.getNumericCellValue() + " "); break; case HSSFCell.CELL_TYPE_STRING: // character string System.out.print(cell.getStringCellValue() + " "); break; case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean System.out.println(cell.getBooleanCellValue() + " "); break; case HSSFCell.CELL_TYPE_FORMULA: // The formula System.out.print(cell.getCellFormula() + " "); break; case HSSFCell.CELL_TYPE_BLANK: // Null value System.out.println(" "); break; case HSSFCell.CELL_TYPE_ERROR: // fault System.out.println(" "); break; default: System.out.print(" Unknown type "); break; } } System.out.println(); } } catch (IOException ex) { ex.printStackTrace(); } }}
This method , If the data is compact , Easy to use , But what I found was that , If it is an empty row or column , He will be separated . Just try it yourself .
in addition , You can also see here to get Excel The way to file is through File, If you want to quote Struts2 in , It's very simple , because Struts2 Upload time Action As defined in File perhaps File Array .
I recommend you to read more about “ excel java Read file cell ” The article