POI读取excel文件。
1) poi读取现成.xls文件,不需要自己建立.xls
====ReadExcel类====
02
03 import java.io.FileInputStream;
04 import java.util.ArrayList;
05 import java.util.List;
06
07 import org.apache.poi.hssf.usermodel.HSSFCell;
08 import org.apache.poi.hssf.usermodel.HSSFRow;
09 import org.apache.poi.hssf.usermodel.HSSFSheet;
10 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
11 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
12 import org.apache.poi.ss.usermodel.Cell;
13
14 public class ReadExcel {
15 public List<Student> readExcel(){
16
17 List<Student> list = new ArrayList<Student>();
18 Student st = null;
20 try {
21 POIFSFileSystem ps = new POIFSFileSystem(new FileInputStream("F:\\zwTest.xls"));
22 HSSFWorkbook workbook = new HSSFWorkbook(ps);//1.创建工作薄
24 HSSFSheet sheet = workbook.getSheetAt(0);//2.创建工作表
25 for(int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++){
26 HSSFRow row = sheet.getRow(rowNum);
27 if(row == null){
28 continue;
29 }
31 st = new Student();
33 HSSFCell name = row.getCell(0);//获取姓名
34 if(name == null){
35 continue;
36 }
37 st.setName(getStringValue(name));
38 HSSFCell age = row.getCell(1);
39 if(age == null){
40 continue;
41 }
42 st.setAge(getIntValue(age));
43 HSSFCell classes = row.getCell(2);
44 if(classes == null){
45 continue;
46 }
47 st.setClasses(getStringValue(classes));
48 list.add(st);//将学生装入list中
49 }
50 } catch (Exception e) {
51 e.printStackTrace();
52 }
53 return list;
54 }
57 public static String getStringValue(HSSFCell cell){
58 cell.setCellType(Cell.CELL_TYPE_STRING);
59 return String.valueOf(cell.getStringCellValue());
60 }
61 public static int getIntValue(HSSFCell cell){
62 cell.setCellType(Cell.CELL_TYPE_STRING);//设置单元格的数据类型
63 String hcell = cell.getStringCellValue();
64 return Integer.parseInt(hcell);
65 }
66 }
====Student类=====
02
03 public class Student {
04
05 private String name;//姓名
06 private int age;//年龄
07 private String classes;//班级
08 public String getName() {
09 return name;
10 }
11 public void setName(String name) {
12 this.name = name;
13 }
14 public int getAge() {
15 return age;
16 }
17 public void setAge(int age) {
18 this.age = age;
19 }
20 public String getClasses() {
21 return classes;
22 }
23 public void setClasses(String classes) {
24 this.classes = classes;
25 }
26 }
=====WriteExcel类=====
02
03 import java.util.List;
04
05 public class WriteExcel {
06
07 public void readExcel(List<Student> list){
08
09 for(int i = 0; i < list.size(); i++){
10 if(list.get(i) == null){
11 continue;
12 }
13 System.out.print("姓名:" + list.get(i).getName() + " ");
14 System.out.print("年龄:" + list.get(i).getAge() + " ");
15 System.out.println("班级:" + list.get(i).getClasses());
16 }
19 }
20 }
====JUnit测试类====
02
03 import java.util.List;
04 import org.junit.Test;
05
06 public class DomeTest {
08 @Test
09 public void Test1(){
10 ReadExcel read = new ReadExcel();
11 List<Student> list = read.readExcel();
12
13 WriteExcel write = new WriteExcel();
14 write.readExcel(list);
15 }
16 }
2)自己创建.xls文件,并读出
---创建---
HSSFWorkbook workbook = new HSSFWorkbook(); //创建新的Excel工作簿
HSSFSheet sheet = workbook.createSheet("sheet");//在EXcel工作薄中建一工作表,其名为缺省值
HSSFRow row =
sheet.createRow((short)0);//在索引为0的位置创建行
HSSFCell cell = row.createCell((short)0);//创建单元格
cell.setCellType(HSSFCell.CELL_TYPE_STRING);//定义单元格为字符串类型
cell.setCellValue("单元格内容");
FileOutputStream fOut = new FileOutputStream("F:\\excelCreate.xls");//把相应的Excel工作簿存盘
workbook.write(fOut);
fOut.flush();
fOut.close();//操作结束,关闭文件
---读出---
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(“F:\\excelCreate.xls”));
HSSFSheet sheet = workbook.getSheet("sheet");
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell((short)1);
System.out.println("左上端单元是:" + cell.getStringCellValue());