-- Start
Java Excel API 是一个用来读写 excel 的开源框架。
你可以在 Google 中搜索 JExcelApi download,然后到官网去下载最新的 JExcelApi 包。然后把 jxl.jar 添加到自己的 classpath 中就可以了。
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class Test {
private static final int ID_INDEX = 0;
private static final int NAME_INDEX = 1;
private static final int BIRTHDAY_INDEX = 2;
private static final int SALARY_INDEX = 3;
private static final int ADDRESS_INDEX = 4;
private WritableCellFormat format;
private WritableCellFormat dateFormat;
public Test() {
format = new WritableCellFormat(WritableWorkbook.ARIAL_10_PT);
try {
format.setWrap(true);
format.setBorder(Border.ALL, BorderLineStyle.THIN);
dateFormat = new WritableCellFormat(new DateFormat("yyyy-MM-dd"));
dateFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
} catch (WriteException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
new Test().generateReport();
}
public void generateReport() throws Exception {
// 打开 workbook
WritableWorkbook workbook = Workbook.createWorkbook(new File("./test.xls"));
// 创建 Sheet
WritableSheet reportSheet = workbook.createSheet("report", 0);
// 设置列宽
setColumnWidth(reportSheet);
// 设置标题
int row = 0;
setTitle(reportSheet, row++);
// 设置每行
List<Employee> employees = getDataFromDB();
for (Employee employee : employees) {
setRow(reportSheet, row++, employee);
}
// 关闭 workbook
workbook.write();
workbook.close();
}
private void setColumnWidth(WritableSheet sheet) {
sheet.setColumnView(ID_INDEX, 20);
sheet.setColumnView(NAME_INDEX, 20);
sheet.setColumnView(BIRTHDAY_INDEX, 10);
sheet.setColumnView(SALARY_INDEX, 10);
sheet.setColumnView(ADDRESS_INDEX, 40);
}
private void setTitle(WritableSheet sheet, int row) throws Exception {
sheet.addCell(new Label(ID_INDEX, row, "员工号", format));
sheet.addCell(new Label(NAME_INDEX, row, "姓名", format));
sheet.addCell(new Label(BIRTHDAY_INDEX, row, "出生年月日", format));
sheet.addCell(new Label(SALARY_INDEX, row, "工资", format));
sheet.addCell(new Label(ADDRESS_INDEX, row, "地址", format));
}
private void setRow(WritableSheet sheet, int row, Employee employee) throws Exception {
sheet.addCell(new jxl.write.Number(ID_INDEX, row, employee.getID(), format));
sheet.addCell(new Label(NAME_INDEX, row, employee.getName(), format));
sheet.addCell(new DateTime(BIRTHDAY_INDEX, row, employee.getBirthday(), dateFormat));
sheet.addCell(new jxl.write.Number(SALARY_INDEX, row, employee.getSalary(), format));
sheet.addCell(new Label(ADDRESS_INDEX, row, employee.getAddress(), format));
}
private List<Employee> getDataFromDB () {
List<Employee> r = new ArrayList<Employee>();
r.add(new Employee(1, "张三", new GregorianCalendar(1979, 11, 24).getTime(), Double.valueOf(1234.56), "辽宁大连"));
r.add(new Employee(2, "李四", new GregorianCalendar(1980, 4, 4).getTime(), Double.valueOf(4321.65), "内蒙古鄂尔多斯"));
return r;
}
}
// Javabean
class Employee implements Serializable {
private static final long serialVersionUID = 7445838103191670245L;
private Integer ID;
private String name;
private Date birthday;
private Double salary;
private String address;
public Employee() {
}
public Employee(Integer ID, String name, Date birthday, Double salary, String address) {
this.ID = ID;
this.name = name;
this.birthday = (birthday == null ? null : (Date) birthday.clone());
this.salary = salary;
this.address = address;
}
public Integer getID() {
return ID;
}
public void setID(Integer iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday == null ? null : (Date) birthday.clone();
}
public void setBirthday(Date birthday) {
this.birthday = (birthday == null ? null : (Date) birthday.clone());
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Java Excel API 还有好多其他的功能,在它发行包的的 src 文件夹下可以找到更多的例子。
-- 更多参见:Open Source 精萃
-- 声 明:转载请注明出处
-- Last Updated on 2012-08-16
-- Written by ShangBo on 2012-07-22
-- End