数据库查询的数据导出到xls表,集合数据导出到xls表

//实体类
package com.outxls; public class Student { private Integer studentId;
private String studentName;
private String studentClass;
private String studentTel;
private String studentEmail;
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentClass() {
return studentClass;
}
public void setStudentClass(String studentClass) {
this.studentClass = studentClass;
}
public String getStudentTel() {
return studentTel;
}
public void setStudentTel(String studentTel) {
this.studentTel = studentTel;
}
public String getStudentEmail() {
return studentEmail;
}
public void setStudentEmail(String studentEmail) {
this.studentEmail = studentEmail;
}
public Student(Integer studentId, String studentName, String studentClass, String studentTel, String studentEmail) {
super();
this.studentId = studentId;
this.studentName = studentName;
this.studentClass = studentClass;
this.studentTel = studentTel;
this.studentEmail = studentEmail;
}
public Student() {
super(); }
@Override
public String toString() {
return "Student [studentId=" + studentId + ", studentName=" + studentName + ", studentClass=" + studentClass
+ ", studentTel=" + studentTel + ", studentEmail=" + studentEmail + "]";
} }
package com.outxls;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List; import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException; public class OutXlsUtil { public static void main(String[] args) {
//省去数据库查询代码 ,所有我自己直接写了一个集合代替查询到的数据
List<Student> stulist=new ArrayList<Student>();
Student stu=null;
for(int i=0;i<10;i++) {
stu=new Student();
stu.setStudentId(i+1);
stu.setStudentName("小明"+(i+1));
stu.setStudentClass("一班");
stu.setStudentTel("1337098123"+i);
stu.setStudentEmail("7632832"+i+"@qq.com");
stulist.add(stu);
} try {
outFilesUtil(stulist);
} catch (RowsExceededException e) { e.printStackTrace();
} catch (WriteException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
}
} /**
*
* @param stulist 传一个集合
* @throws IOException
* @throws RowsExceededException
* @throws WriteException
*/
public static void outFilesUtil(List<Student> stulist) throws IOException, RowsExceededException, WriteException {
//用一个string 数组来设置xls表头
String[] title = { "序号", "姓名", "班级", "手机号码", "邮箱" };
// 创建一个输出的xls文件路径
String outPath = "C:\\Student.xls";
// 创建Excel 工作
WritableWorkbook wwb;
OutputStream os = null;
//输出流
os = new FileOutputStream(outPath);
wwb = Workbook.createWorkbook(os);
// 添加第一个工作表并设置第一个Sheet的名字
WritableSheet sheet = wwb.createSheet("学生信息", 0);
Label label;
for (int i = 0; i < title.length; i++) {
//设置表头
label = new Label(i, 0, title[i]);
sheet.addCell(label);
} // 这里是将查询的数据填写到xls表
for (int j = 0; j < stulist.size(); j++) {
//1 列
label = new Label(0, j + 1, stulist.get(j).getStudentId().toString());
sheet.addCell(label);
//2 列
label = new Label(1, j + 1, stulist.get(j).getStudentName());
sheet.addCell(label);
//3 列
label = new Label(2, j + 1, stulist.get(j).getStudentClass());
sheet.addCell(label);
//4 列
label = new Label(3, j + 1, stulist.get(j).getStudentTel());
sheet.addCell(label);
//5 列
label = new Label(4, j + 1, stulist.get(j).getStudentEmail());
sheet.addCell(label);
}
// 写入数据
wwb.write();
//刷新
os.flush();
// 关闭文件
wwb.close(); } }

 需要jxt.jar

上一篇:(数字IC)低功耗设计入门(五)——RTL级低功耗设计(续)


下一篇:误删文件不用怕 grep命令帮你恢复