// 导出专家信息到Excel中
public void testExportExpertsToExcel() throws RowsExceededException,
WCMException, BiffException, WriteException, IOException {
exportExpertsToExcel();
}
private void exportExpertsToExcel() throws IOException, WCMException,
BiffException, RowsExceededException, WriteException {
User loginUser = ContextHelper.getLoginUser();
Experts allExperts = Experts.openWCMObjs(loginUser, null);
// 1.检查文件格式是否正确,并检查文件是否存在
String sExcelPath = "D://temp//专家信息表.xls";
File excelFile = new File(sExcelPath);
if (!excelFile.exists()) {
excelFile.createNewFile();
}
// 创建工作薄
OutputStream os = new FileOutputStream(excelFile);
WritableWorkbook wwb = Workbook.createWorkbook(os);
WritableSheet ws = wwb.createSheet("sheet1", 0);
// 创建写入位置和内容
jxl.write.Label labelExerptName = new jxl.write.Label(0, 0, "专家名称");
jxl.write.Label labelDept = new jxl.write.Label(1, 0, "单位");
ws.addCell(labelExerptName);
ws.addCell(labelDept);
try {
// 写入专家数据,从第二行开始写数据,第一行是导航
for (int i = 0; i < allExperts.size(); i++) {
Expert aExpert = (Expert) allExperts.getAt(0);
if (aExpert == null) {
continue;
}
jxl.write.Label aExertNameLabel = new jxl.write.Label(0, i + 1,
aExpert.getExpertName());
jxl.write.Label aOrganizeLabel = new jxl.write.Label(1, i + 1,
CMyString.showNull(aExpert.getOrganization()));
ws.addCell(aExertNameLabel);
ws.addCell(aOrganizeLabel);
}
// 写数据
wwb.write();
www.close();//必须关闭流,不然写不进excel
} catch (Exception e) {
e.printStackTrace();
}finally{
if (wwb != null) {
// 关闭流
wwb.close();
}
if (os != null) {
os.close();
}
}
}