看了一天,感觉自己有点笨。。。
public class Test {
public static void main(String args[]) {
Test test = new Test();
test.extract();
}
public static void extract() {
try {
InputStream is = null;
Workbook workbook = getWorkbook(new File("D:\\1.xlsx"));
Sheet sheet = workbook.getSheetAt(0);
for(int i=0;i<sheet.getPhysicalNumberOfRows();i++){
Row row= sheet.getRow(i);
for(int j=0;j<row.getPhysicalNumberOfCells();j++){
System.out.println(row.getCell(j));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static Workbook getWorkbook(File file) throws IOException {
Workbook wb = null;
FileInputStream in = new FileInputStream(file);
if (file.getName().endsWith("xls")) { //Excel 2003
wb = new HSSFWorkbook(in);
} else if (file.getName().endsWith("xlsx")) { // Excel 2007/2010
wb = new XSSFWorkbook(in);
}
return wb;
}
}
就这个写了一天。。。
最重要的是编程的思想!整天看是没用的,还是要自己想然后动手写代码!