*转载*
用三个循环遍历就行了: 第一层是sheet循环,然后是行循环,最后是单元格循环.
package com.jie.java;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelRead {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
FileInputStream is = new FileInputStream(new File("c:/最终成绩.xls"));
HSSFWorkbook wb=new HSSFWorkbook(is);
int sheetNum=wb.getNumberOfSheets();
for(int i=0;i<sheetNum;i++)
{
HSSFSheet childSheet = wb.getSheetAt(i);
int rowNum = childSheet.getLastRowNum();
for(int j=0;j<rowNum;j++)
{
HSSFRow row = childSheet.getRow(j);
int cellNum=row.getLastCellNum();
for(int k=0;k<cellNum;k++)
{
System.out.print(row.getCell(k).toString()+" ");
}
System.out.println();
}
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
用三个循环遍历就行了: 第一层是sheet循环,然后是行循环,最后是单元格循环.
package com.jie.java;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelRead {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
FileInputStream is = new FileInputStream(new File("c:/最终成绩.xls"));
HSSFWorkbook wb=new HSSFWorkbook(is);
int sheetNum=wb.getNumberOfSheets();
for(int i=0;i<sheetNum;i++)
{
HSSFSheet childSheet = wb.getSheetAt(i);
int rowNum = childSheet.getLastRowNum();
for(int j=0;j<rowNum;j++)
{
HSSFRow row = childSheet.getRow(j);
int cellNum=row.getLastCellNum();
for(int k=0;k<cellNum;k++)
{
System.out.print(row.getCell(k).toString()+" ");
}
System.out.println();
}
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}