03版excel,需要用到jxl.jar这个jar包
package test.poi; import java.io.File; import java.io.IOException; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; //03版excel public class getExcel { public static void main(String[] args){ show03Excel(); } public static void show03Excel(){ Sheet sheet; Workbook book = null; try { book= Workbook.getWorkbook(new File("F:\\Book1.xls")); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // System.out.println(book); //获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....) sheet=book.getSheet(0); int rows = 0; int r=0,l=0; String value=null; while(sheet.getCell(0,r).getContents()!=""&&sheet.getCell(0,r).getContents()!=null){ r++; } while(sheet.getCell(l,0).getContents()!=""&&sheet.getCell(l,0).getContents()!=null){ l++; } for(int i=0;i<(r);i++){ for(int j=0;j<(l);j++) System.out.print(sheet.getCell(j,i).getContents()+"\t\t"); System.out.println(); } } }
07版EXCEL,需要用到poi这个jar包
package test.poi; import java.io.IOException; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class get07Excel { public static void main(String[] arg){ show07excel(); } public static void show07excel(){ XSSFWorkbook xwb =null; try { xwb= new XSSFWorkbook("F:\\Book1.xlsx"); } catch (IOException e) { e.printStackTrace(); } XSSFSheet sheet=xwb.getSheetAt(0); XSSFRow row; String cell; int r=0,l=0; row = sheet.getRow(0); while(row.getCell(l).toString()!=""&&row.getCell(l).toString()!=null){ l++; } while(row.getCell(0).toString()!=""&&row.getCell(0).toString()!=null){ r++; row = sheet.getRow(r); } System.out.println("the rows is "+r+" and the number of one row is "+l); // 循环输出表格中的内容 for (int i = sheet.getFirstRowNum(); i < r; i++) { row = sheet.getRow(i); for (int j = row.getFirstCellNum(); j < l; j++) { cell = row.getCell(j).toString(); System.out.print(cell + "\t"); } System.out.println(""); } } }