标签:poi 导出excel中
2.步入正题,看代码
// 大致思路,先看本地文件存在不,如果不存在,把查询数据导出到Excel,然后再下载 代码有点乱
String filePath = request.getParameter("filePath");
filePath = new String( filePath.getBytes("iso-8859-1"), "UTF-8");
File filetemp = null;
byte[] fileContent = null;
filetemp = new File(filePath);
// 如果文件不存在
if(!filetemp.exists()){
String title = "序号,统一社会信用代码或组织机构代码,企业名称,发证机关,获奖内容,发证日期,有效日期";
String key = ",ZZJGDM,QYMC,FZJG,HJNR,KSRQ,JSRQ";
String[] titles = title.split(",");
String[] keys = key.split(",");
//-------------------POI保存数据到Excel----------------------//
// 第一步,创建Excel 工作薄对象
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,创建Excel 工作表对象
HSSFSheet sheet = wb.createSheet("sheet1");
// 设置表格默认列宽度为 30个 字节
sheet.setDefaultColumnWidth((short)15);
// 设置样式
HSSFCellStyle style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
//合并单元格
HSSFRow rows = sheet.createRow(0);
// 0,1,0,titles.length-1 表示合并 第1,2行 从左边0列开始 到 titles.length-1 列结束
CellRangeAddress region = new CellRangeAddress(0,1,0,titles.length-1);
sheet.addMergedRegion(region);
rows.createCell((short) 0).setCellValue(fileName);
// 创建 表格 标题栏
HSSFRow row = sheet.createRow(2);
for(int i=0;i<titles.length;i++){
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(titles[i]);
cell.setCellValue(text);
}
// 第五步, 假设 list是想要导出的数据
for(int i=0;i<list.size();i++){
HashMap map =(HashMap)list.get(i); // 转换为HashMap
row = sheet.createRow(i+3); // 从第几行开始写入数据
for(int j = 0;j < keys.length;j++){
if(j==0){
row.createCell((short) 0).setCellValue(i+1);
continue;
}
// 日期格式处理 ,对日期截取后 例如: 2017-11-08
if("KSRQ".equals(keys[j])||"JSRQ".equals(keys[j])){
String str = map.get(keys[j])==null?"":map.get(keys[j])+"";
if(!"".equals(str)){
row.createCell((short) j).setCellValue(str.substring(0,10));
}
}else if("XZXKFL".equals(keys[j])&&"51".equals(lx)){
row.createCell((short) j).setCellValue("食品相关产品");
}else{
row.createCell((short) j).setCellValue(map.get(keys[j])==null?"":map.get(keys[j])+"");
}
}
}
// 文件命名
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 0);
int year1 = calendar.get(Calendar.YEAR);// 月份
int month1 = calendar.get(Calendar.MONTH) + 1;// 日期
int date1 = calendar.get(Calendar.DATE);
String d1 = year1 + "-" + month1 + "-" + date1;//2017-2-4
//拿到 事先定义好的静态变量 就是 所谓的二级目录 也是路径的一部分
//拼接路径 并且 创建文件夹
filePath = "D:\Zlxypt";
File dir = new File(filePath+year1+"/"+d1); //D:/Zlxypt/2017年/2017-2-4
if(!dir.exists()){
dir.mkdirs();
}
String uuid = year1+UUID.randomUUID().toString();
String file_Name = year1+"/"+ d1 +"/" +uuid+".xls";
filePath += file_Name; //文件全路径名称
// 更新 title 发布表 filePath
titleServices.updaTitleById(tid, file_Name);
// 第六步,将文件存到指定位置
try {
FileOutputStream fout = new FileOutputStream(filePath);
wb.write(fout); // 把数据写入 excel中
fout.close();
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
//-----------------下载
filePath= new String( filePath.getBytes("iso-8859-1"), "UTF-8");
filetemp = new File(filePath);
fileContent = FileUtil.getBytes(filetemp);
fileName += ".xls";
response.setHeader("Content-Disposition","attachment; filename=" + new String(fileName.getBytes("utf-8"), "iso-8859-1"));
response.getOutputStream().write(fileContent,0,fileContent.length);
response.getOutputStream().flush();
response.getOutputStream().close();
out.clear();
out = pageContext.pushBody();
本文出自 “永恒之光” 博客,请务必保留此出处http://zhuws.blog.51cto.com/11134439/1980048
标签:poi 导出excel中
原文地址:http://zhuws.blog.51cto.com/11134439/1980048