java Excel的导入导出学习(1)
今天学习了java的Jxl API简单做了下导入导出功能,做起来还是很简单的,下面做了一个导入导出的例子,可以看看,代码如下:
JxlTest 类:
package jxl;
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
/**
*
*@author: zhangjp
*@功能:jxlTest
*@date:Mar 29, 2010 2:24:19 PM
*
*/
public class JxlTest {
public List<Student> getExcelData(){
List <Student> list = new ArrayList<Student>();
File scoureFile = new File("d:\\javaTest.xls");
InputStream is =null;
try {
is = new FileInputStream(scoureFile);
jxl.Workbook rwb = Workbook.getWorkbook(is);
Sheet sheet = rwb.getSheet(0);
for(int i=0;i<sheet.getRows();i++){
Student s = new Student();
s.setOid(new Long(sheet.getCell(0, i).getContents()));
s.setName(sheet.getCell(1,i).getContents());
s.setSex(Integer.parseInt(sheet.getCell(2, i).getContents()));
s.setAge(Integer.parseInt(sheet.getCell(3, i).getContents()));
s.setHobby(sheet.getCell(4, i).getContents());
list.add(s);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
public void createExcel(List<Student> list){
File targetFile = new File("d:\\javaCreateExcel.xls");
try {
OutputStream os = new FileOutputStream(targetFile);
WritableWorkbook wwb = Workbook.createWorkbook(os);
WritableSheet sheet = wwb.createSheet("123", 0);
for(int i=0;i<list.size();i++){
Label label = new Label(0,i,String.valueOf(list.get(i).getOid()));
sheet.addCell(label);
Label label1 =new Label(1,i,list.get(i).getName());
sheet.addCell(label1);
Label label2 =new Label(2,i,String.valueOf(list.get(i).getSex()));
sheet.addCell(label2);
Label label3 =new Label(3,i,String.valueOf(list.get(i).getAge()));
sheet.addCell(label3);
Label label4 =new Label(4,i,list.get(i).getHobby());
sheet.addCell(label4);
}
wwb.write();
wwb.close();
os.close();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
JxlTest jsl = new JxlTest();
List <Student> list = jsl.getExcelData();
for(Student s :list){
System.out.println(s);
}
List <Student>l = CreateBD.getData();
jsl.createExcel(l);
}
}
CreateDB类:
package bo;
imp
imp
imp
public class CreateBD {
public static List<Student> getData(){
List <Student>list = new ArrayList<Student>();
Student s1=new Student();
s1.setOid(1);
s1.setName("段誉");
s1.setAge(11);
s1.setSex(1);
s1.setHobby("titi");
list.add(s1);
Student s2=new Student();
s2.setOid(2);
s2.setName("乔峰");
s2.setAge(28);
s2.setSex(1);
s2.setHobby("降龙十八掌");
list.add(s2);
return list;
}
}