public class Excl {

public static void writeFile() throws WriteException, IOException {
File file = Excl.createFile();
// 创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(file);
// 创建新的一页
WritableSheet sheet = workbook.createSheet("First Sheet", 0); AgentDAO ad = new AgentDAO();
List<Agent> Agents = ad.findAll();
for (Agent agent : Agents) {
Label cont;
cont = new Label(0, agent.getId(), agent.getName());
sheet.addCell(cont);
cont = new Label(1, agent.getId(), agent.getCompany());
sheet.addCell(cont);
cont = new Label(2, agent.getId(), agent.getTel());
sheet.addCell(cont);
if (agent.getTrRecord() != null) {
cont = new Label(3, agent.getId(), agent.getTrRecord()
.toString());
sheet.addCell(cont);
}
cont = new Label(4, agent.getId(), agent.getCreateDate().toString());
sheet.addCell(cont);
}
// 把创建的内容写入到输出流中,并关闭输出流
workbook.write();
workbook.close();
} private static File createFile() {
// path表示你所创建文件的路径
String path = "D:";
File f = new File(path);
if (!f.exists()) {
f.mkdirs();
}
// fileName表示你创建的文件名;;
String fileName = "gather.xls";
File file = new File(f, fileName);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
}