1.先创建execl:sample.xls
2.Java部分:先下载导入poi-*.jar架包。楼主使用的是poi-3.9.jar
package test; import java.io.FileInputStream; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; public class ExeclDemo { public static void main(String[] args) { ExeclDemo execlDemo = new ExeclDemo(); try { execlDemo.sheet1(); } catch (Exception e) { e.printStackTrace(); } } public void sheet1(){ String filePath = "C:/Users/it_zhaojie/Desktop/sample.xls"; FileInputStream stream; try { stream = new FileInputStream(filePath); HSSFWorkbook workbook = new HSSFWorkbook(stream);// 读取现有的Excel HSSFSheet sheet = workbook.getSheet("Sheet1");// 得到指定名称的Sheet for (Row row : sheet) { for (Cell cell : row) { System.out.print(cell + " "); } System.out.println(); } } catch (Exception e) { e.printStackTrace(); } }
}
2.1不读取第一行只读取内容
public void sheet2(){ String filePath = "C:/Users/it_zhaojie/Desktop/sample.xls"; FileInputStream stream; try { stream = new FileInputStream(filePath); HSSFWorkbook workbook = new HSSFWorkbook(stream);// 读取现有的Excel HSSFSheet sheet = workbook.getSheet("Sheet1");// 得到指定名称的Sheet List<Row> list = new ArrayList<Row>(); for (Row row : sheet) { list.add(row); } for (int i = 1; i < list.size(); i++) { for (Cell cell : list.get(i)) { System.out.print(cell + " "); } System.out.println(); } } catch (Exception e) { e.printStackTrace(); } }
3.运行效果