POI读取excel文件。

1) poi读取现成.xls文件,不需要自己建立.xls

====ReadExcel​类​====

package cust.com.excelToDataTest;

import java.io.FileInputStream;

import java.util.ArrayList;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import org.apache.poi.ss.usermodel.Cell;

public
class
ReadExcel {

public
List<Student> readExcel(){

List<Student> list = new ArrayList<Student>();

Student
st = null;

try {

POIFSFileSystem
ps = new POIFSFileSystem(new FileInputStream("F:\\zwTest.xls"));

HSSFWorkbook
workbook = new HSSFWorkbook(ps);//1.创建工作薄

HSSFSheet
);//2.创建工作表

for(int
; rowNum <= sheet.getLastRowNum(); rowNum++){

HSSFRow row = sheet.getRow(rowNum);

if(row == null){

continue;

}

st = new Student();

);//获取姓名

if(name == null){

continue;

}

st.setName(getStringValue(name));

);

if(age == null){

continue;

}

st.setAge(getIntValue(age));

);

if(classes == null){

continue;

}

st.setClasses(getStringValue(classes));

list.add(st);//将学生装入list中

}

}
catch
(Exception e) {

e.printStackTrace();

}

return
list;

}

public
static
String getStringValue(HSSFCell cell){

cell.setCellType(Cell.CELL_TYPE_STRING);

return
String.valueOf(cell.getStringCellValue());

}

public
static
int
getIntValue(HSSFCell cell){

cell.setCellType(Cell.CELL_TYPE_STRING);//设置单元格的数据类型

String
hcell = cell.getStringCellValue();

return
Integer.parseInt(hcell);

}

}

====Student类=====

01 package
cust.com.excelToDataTest;

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类=====

01 package
cust.com.excelToDataTest;

02

03 import
java.util.List;

04

05 public
class
WriteExcel {

06 
   

07 
   public
void
readExcel(List<Student>
list){

08 
     
 

09 
     
 ; 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测试类====

01 package
cust.com.excelToDataTest;

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());​

​​​

上一篇:jspsmart(保存文件)+poi(读取excel文件)操作excel文件


下一篇:poi读取Excel模板并修改模板内容与动态的增加行