导出效果:
//使用submit或a标签访问导出方法,千万不能用ajax,不刷新就不会出现下载框哦
//省略方法名接参查询等等操作
response.setContentType("application/msexcel");
response.setHeader("Content-disposition", "attachment; filename=export.xls");
OutputStream os = response.getOutputStream(); //获取输出流
doExportStudents(os, list); //调用导出方法,list是参数值
os.flush();
os.close();
os = null;
return null;
//导出方法
public void doExportStudents(OutputStream os, List list){
try {
WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件
WritableSheet wsheet = wbook.createSheet("培训班导入",
0); // sheet名称
WritableFont bold = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
WritableCellFormat wcfFormat = new WritableCellFormat(bold);
wcfFormat.setAlignment(jxl.format.Alignment.CENTRE);//单元格中的内容水平方向居中
//把垂直对齐方式指定为居中
wcfFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcfFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
int rowIndex = 0; //行
int columnIndex = 0; //列
//第一行
columnIndex = 0;
rowIndex = 0 ;
wsheet.addCell(new Label(0, rowIndex, "培训班编号",wcfFormat));//
wsheet.addCell(new Label(1, rowIndex, "学员编号",wcfFormat));//
wsheet.addCell(new Label(2, rowIndex, "学员姓名",wcfFormat));//
//未结训状态添加批注
WritableCellFeatures cellFeatures = new WritableCellFeatures();
cellFeatures.setComment("请输入1或2\r\n1:未结训\r\n2:结训");
Label label = new Label(3, rowIndex, "学员结训状态",wcfFormat);
label.setCellFeatures(cellFeatures);
wsheet.addCell(label);//学员结训状态加批注
wsheet.addCell(new Label(4, rowIndex, "备注",wcfFormat));//
//填写数据
// lResult2 设计成序号
WritableCellFormat wcfFormat123 = new WritableCellFormat();
//单元格中的内容垂直方向居中
wcfFormat123.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcfFormat123.setBorder(Border.ALL, BorderLineStyle.THIN);
// 二行
columnIndex = 0;
rowIndex = 1;
for(int i=0;i
ITrainingClassVOModel model = list.get(i);
wsheet.addCell(new Label(columnIndex++,rowIndex
,model.getClass_id(),wcfFormat123));
wsheet.addCell(new Label(columnIndex++,rowIndex
,model.getPerson_id(),wcfFormat123));
wsheet.addCell(new Label(columnIndex++,rowIndex
,model.getPerson_name(),wcfFormat123));
columnIndex = 0;
rowIndex++;
}
wbook.write();
if (wbook != null) {
wbook.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}