
正文
导出excel表格。
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
导出也做了很多遍了,还是发现好记性不如烂笔头,还是记下来。
public void exportLog(HttpServletRequest request,HttpServletResponse response,
@ModelAttribute(CommonUtils.SESSION_USER) UserContext uc,ModelMap model,String logId
) {
//获得信息列表
List<GTL> lgtgl = logManagerImpl.findLogByKspcAndDwbhAndAreaIdAndLogId(batchId, code, areaId,Id);//id是为了能够在导出以下选项时候用的,导出全部可以穿//一个空参数 areaid是为了权限验证用的
//将结果集保存到excel
//取文件存放路径并新建文件
String path = this.getClass().getClassLoader().getResource("").getPath();
PropertieFile cf = new PropertieFile(path + "/spf.properties");
String filePath=cf.getValue("global.export.student");
String tempName=GetRandomName.ConfirmId(32)+".xls";
File file = new File(filePath);//导出文件存放的位置
if(!file.exists()) {
file.mkdirs();
}
WritableWorkbook wwb;
try {
wwb = Workbook.createWorkbook(new File(filePath+tempName));
WritableFont wf = new WritableFont(WritableFont.TIMES, 18, WritableFont.BOLD , false); //对文字style设置
WritableCellFormat wff = new WritableCellFormat(wf);
Color color = Color.decode("#87CEFF"); // 自定义的颜色
wwb.setColourRGB(Colour.ORANGE, color.getRed(),color.getGreen(), color.getBlue());
wff.setBackground(Colour.ORANGE);//设置单元格颜色
wff.setAlignment(Alignment.CENTRE); // 设置对齐方式
wff.setVerticalAlignment(VerticalAlignment.CENTRE);
WritableSheet ws = wwb.createSheet("导出信息", 0);// 建立工作簿
ws.setColumnView(0, 20); // 设置列的宽度
ws.setRowView(0, 600); // 设置行的高度
ws.setColumnView(1, 20); // 设置列的宽度
ws.setRowView(1, 600); // 设置行的高度 Label label1 = new Label(0, 0, "第一列表头",wff);
Label label1 = new Label(0, 0, "第二列表头",wff);
ws.addCell(label1);// 放入工作簿 for(int k=0;k<lgtgl.size();k++){ WritableFont wf_1 = new WritableFont(WritableFont.TIMES, 12, WritableFont.NO_BOLD , false); //对文字style设置 WritableCellFormat wff_1 = new WritableCellFormat(wf_1); wff_1.setAlignment(Alignment.CENTRE); // 设置对齐方式 wff_1.setVerticalAlignment(VerticalAlignment.CENTRE); wff_1.setBorder(Border.ALL, BorderLineStyle.THIN,Colour.BLACK); label1 = new Label(0, k + 1, lgtgl.get(k).getd1l()().toString().trim(),wff_1);// 建立第一列
String type=lgtgl.get(k).getd2l().toString().trim();
if(("0").equals(type)){
type="采集";
}
if(("1").equals(type)){
type="认证";
}
if(("2").equals(type)){
type="考务";
}
if(("3").equals(type)){
type="押运";
}
label2 = new Label(1, k + 1, type,wff_1);// 建立第二列
ws.addCell(label1);// 放入工作簿
}
// 写入Exel工作表
wwb.write();
// 关闭Excel工作薄对象
wwb.close();
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
//返回文件流
response.setCharacterEncoding("UTF-8");
try {
response.setHeader("Content-Disposition", "attachment;filename=\""
+ new String("信息导出.xls".getBytes("gb2312"), "ISO8859-1")
+ "\"");
OutputStream out = response.getOutputStream();
response.setContentType("xls");
FileOperation fileOperation=new FileOperation();
IOUtils.copy(fileOperation.readFile(filePath+tempName), out);
out.flush();
out.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}







