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

Java代码生成excel文件下载

2017-07-17 10:57 676 查看
List <IPSumCountVO> ipscList = changeStringToVO(ipSourceReport);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String fileName = "IP资源统计列表_" + sdf.format(new Date());
outputStream = WebUtils.getOutputStream(response, fileName + ".xls", "UTF-8");
public static OutputStream getOutputStream(HttpServletResponse response,
String fileName, String encode) throws IOException {
String finalFileName = "";
String userAgent = WebUtils.getRequest().getHeader("User-Agent");
// 对文件名进行编码,防止文件名乱码
// 如果是火狐或者苹果浏览器
if (userAgent.toLowerCase().indexOf("firefox".toLowerCase()) != -1
|| userAgent.toLowerCase().indexOf("safari".toLowerCase()) != -1) {
finalFileName = new String(fileName.getBytes(encode), "ISO-8859-1");
} else {
finalFileName = URLEncoder.encode(fileName,encode);
}
// 设置头信息
response.reset();
response.setHeader("Content-Disposition", "attachment; filename="+finalFileName);
response.setContentType("application/octet-stream; charset="+encode);
return response.getOutputStream();
}

WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
// 创建excel
createDeviceOrderListExcel(ipscList, workbook);
// 释放资源
workbook.write();
workbook.close();

private void createDeviceOrderListExcel(List<IPSumCountVO> ipscList, WritableWorkbook workbook) throws Exception {
try{
WritableSheet wsheet = workbook.createSheet("IP资源统计列表", 0);
wsheet.getSettings().setVerticalFreeze(1);

// 定义title单元格样式:字体 下划线 斜体 粗体 颜色
WritableFont wf_title = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
// 单元格定义
WritableCellFormat wcf_title = new WritableCellFormat(wf_title);
// 设置单元格的背景颜色
wcf_title.setBackground(Colour.GRAY_25);
// 设置对齐方式
wcf_title.setAlignment(Alignment.CENTRE);
wcf_title.setVerticalAlignment(VerticalAlignment.CENTRE);
//设置边框
wcf_title.setBorder(Border.ALL, BorderLineStyle.THIN,Colour.BLACK);

// 定义body单元格样式:定义格式 字体 下划线 斜体 粗体 颜色
WritableFont wf_table = new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat wcf_table = new WritableCellFormat(wf_table);
wcf_table.setBackground(Colour.WHITE);
wcf_table.setAlignment(Alignment.RIGHT);
wcf_table.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf_table.setBorder(Border.ALL, BorderLineStyle.THIN,Colour.BLACK);

String[] str = {"IP地址组","规划IP数量","子网数量","使用率","使用数量","预留数量","冻结数量"};
for (int i = 0; i < 7; i++) {
wsheet.setColumnView(i, 20);
Label label = new Label(i, 0, str[i], wcf_title);
wsheet.addCell(label);
}
int j = 1;
for (IPSumCountVO ipso : ipscList) {
int index = 0;
//IP地址组
wsheet.addCell(new Label(index++, j,ipso.getIpAddressGroupName()== null?"":ipso.getIpAddressGroupName(),wcf_table));
//规划IP数量
wsheet.addCell(new Label(index++, j,ipso.getPlanIpCount() + "",wcf_table));
//子网数量
wsheet.addCell(new Label(index++, j,ipso.getSubNetCount() + "",wcf_table));
//使用率
wsheet.addCell(new Label(index++, j,ipso.getUseRate()== null?"":ipso.getUseRate(),wcf_table));
//使用
wsheet.addCell(new Label(index++, j,ipso.getUseCount() + "",wcf_table));
//预留
wsheet.addCell(new Label(index++, j,ipso.getObligateCount() + "",wcf_table));
//冻结
wsheet.addCell(new Label(index++, j,ipso.getFrozenCount() + "",wcf_table));
j++;
}
}catch(Exception e){
e.printStackTrace();
BeanLog.getLogger().fatal("构造book", e);
throw e;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: