public String toExcel(List<ResPreExamVO> list) throws Exception{
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("试题导出表");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("编号");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("试题");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("选项");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("正确答案");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("试题解析");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue("试题难度");
cell.setCellStyle(style);
cell = row.createCell((short) 6);
cell.setCellValue("关联知识点");
cell.setCellStyle(style);
cell = row.createCell((short) 7);
cell.setCellValue("所属学段");
cell.setCellStyle(style);
cell = row.createCell((short) 8);
cell.setCellValue("所属学科");
cell.setCellStyle(style);
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow((int) i + 1);
ResPreExamVO exam = list.get(i);
// 第四步,创建单元格,并设置值
if(exam.getId()!=null){
row.createCell((short) 0).setCellValue(exam.getId());
}else{
row.createCell((short) 0).setCellValue(0);
}
if(exam.getExamTitle()!=null&&exam.getExamTitle()!=""){
row.createCell((short) 1).setCellValue(exam.getExamTitle());
}else{
row.createCell((short) 1).setCellValue("暂无");
}
if(exam.getExamOption()!=null&&exam.getExamOption()!=""){
row.createCell((short) 2).setCellValue(exam.getExamOption());
}else{
row.createCell((short) 2).setCellValue("暂无");
}
if(exam.getExamKey()!=null&&exam.getExamKey()!=""){
row.createCell((short) 3).setCellValue(exam.getExamKey());
}else{
row.createCell((short) 3).setCellValue("暂无");
}
if(exam.getExamAnalysis()!=null&&exam.getExamAnalysis()!=""){
row.createCell((short) 4).setCellValue(exam.getExamAnalysis());
}else{
row.createCell((short) 4).setCellValue("暂无");
}
if(exam.getExamLevel()!=null){
row.createCell((short) 5).setCellValue(exam.getExamLevel());
}else{
row.createCell((short) 5).setCellValue("暂无");
}
if(exam.getPointName()!=null&&exam.getPointName()!=""){
row.createCell((short) 6).setCellValue(exam.getPointName());
}else{
row.createCell((short) 6).setCellValue("暂无");
}
if(exam.getLevelName()!=null&&exam.getLevelName()!=""){
row.createCell((short) 7).setCellValue(exam.getLevelName());
}else{
row.createCell((short) 7).setCellValue("暂无");
}
if(exam.getSubjectName()!=null&&exam.getSubjectName()!=""){
row.createCell((short) 8).setCellValue(exam.getSubjectName());
}else{
row.createCell((short) 8).setCellValue("暂无");
}
}
// 第六步,将文件存到指定位置
try {
String name = "试题导出表";
String url = "C:/Users/Administrator/Desktop/"+name+".xls";
while (new File(url).exists()) {
name+="-副本";
url = "C:/Users/Administrator/Desktop/"+name+".xls";
}
FileOutputStream out = new FileOutputStream(url);
wb.write(out);
out.close();
return "T";
}
catch (Exception e) {
e.printStackTrace();
return "E";
}
}
Java将数据导出到Excel
于 2016-04-19 14:49:18 首次发布