下面是代码,有部分是从网上抄的<img src="https://img-blog.youkuaiyun.com/20150508222927756?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDY3NDA0OQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
public class Test {
public static final String inPath = "G:\\ex03_demo.xls";
public static final StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws FileNotFoundException, IOException {
//读取excel文件
HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(inPath));
for (int numSheet=0;numSheet<hwb.getNumberOfSheets();numSheet++){
//读取所有的sheet,即表
HSSFSheet sheet = hwb.getSheetAt(numSheet);
if (sheet!=null){
for (int i=0;i<sheet.getLastRowNum()+1;i++){
HSSFRow row = sheet.getRow(i);
if (row!=null){
for (int j=0;j<row.getLastCellNum();j++){
//读取所有的单元格
HSSFCell cell = row.getCell(j);
if(j == row.getLastCellNum()-1){
//每行读到最后一列时,换行
System.out.print(" "+cell+"\r\n");
}else{
System.out.print(cell+" ");
sb.append(cell+" ");
}
}
}
}