您的位置:首页 > 编程语言

利用jxl向Excel中导入数据示例代码

2010-06-17 14:19 537 查看
// 导出专家信息到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();
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: