p

ublic void export(List<WechatUser> wechatUserList, HttpServletResponse response) throws IOException, URISyntaxException {
File newFile = createNewFile(getClass().getClassLoader().getResource("../../resources/excel/wechatUserInfoTemplate.xls").toURI().getPath(), new URI("../../resources/xls").getPath());//防止路径中的空格转义为%20 InputStream is = null;
HSSFWorkbook workbook = null;
HSSFSheet sheet = null; try {
is = new FileInputStream(newFile);// 将excel文件转为输入流
workbook = new HSSFWorkbook(is);// 创建个workbook,
sheet = workbook.getSheetAt(0);
} catch (Exception e1) {
e1.printStackTrace();
} if (sheet != null) {
try {
// 写数据
FileOutputStream fos = new FileOutputStream(newFile);
HSSFRow row = sheet.createRow(1);
HSSFCell cell = row.createCell(0);
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setWrapText(true);
int i = 1;
for (WechatUser wechatUser : wechatUserList) {
List<Children> childrenList = wechatUser.getChildrenList();
int size = childrenList.size(); row = sheet.getRow(i);
if (row == null) {
row = sheet.createRow(i);
}
cell = getCell(row,0,cell);
cell.setCellStyle(style);
cell.setCellValue(wechatUser.getNickname()); cell = getCell(row,1,cell);
cell.setCellStyle(style);
cell.setCellValue(wechatUser.getName()); cell = getCell(row,2,cell);
cell.setCellStyle(style);
cell.setCellValue(wechatUser.getPhone()); cell = getCell(row,3,cell);
cell.setCellStyle(style);
cell.setCellValue((null == wechatUser.getSex() ? "" :
(0 == wechatUser.getSex() ? "男" : "女"))); cell = getCell(row,4,cell);
cell.setCellStyle(style);
cell.setCellValue(wechatUser.getGrade().getName()); cell = getCell(row,5,cell);
cell.setCellStyle(style);
cell.setCellValue(wechatUser.getDefaultAddress()); if (1 <= size) {
cell = getCell(row,6,cell);
cell.setCellStyle(style);
cell.setCellValue(childrenList.get(0).getName()); cell = getCell(row,7,cell);
cell.setCellStyle(style);
cell.setCellValue((null == childrenList.get(0).getGender() ? "" :
(0 == childrenList.get(0).getGender() ? "男" : "女"))); cell = getCell(row,8,cell);
cell.setCellStyle(style);
cell.setCellValue(
(null == childrenList.get(0).getBirthDay() ? "" :
DateUtil.dateToStr(
childrenList.get(0).getBirthDay(),
"yyyy-MM-dd")));
} if (2 == size) {
cell = getCell(row,9,cell);
cell.setCellStyle(style);
cell.setCellValue(childrenList.get(1).getName()); cell = getCell(row,10,cell);
cell.setCellStyle(style);
cell.setCellValue((null == childrenList.get(1).getGender() ? "" :
(0 == childrenList.get(1).getGender() ? "男" : "女"))); cell = getCell(row,11,cell);
cell.setCellStyle(style);
cell.setCellValue((null == childrenList.get(1).getBirthDay() ? "" :
DateUtil.dateToStr(
childrenList.get(1).getBirthDay(),
"yyyy-MM-dd")));
}
i++;
}
//内容
workbook.write(fos);
fos.flush();
fos.close(); // 下载
InputStream fis = new BufferedInputStream(new FileInputStream(
newFile));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
response.setContentType("text/html;charset=UTF-8");
OutputStream toClient = new BufferedOutputStream(
response.getOutputStream());
response.setContentType("application/x-msdownload");
String fileName = URLEncoder.encode(
"会员信息" + ".xls",
"UTF-8");
fileName = new String(fileName.getBytes(), "ISO8859-1");
response.addHeader("Content-Disposition",
"attachment;filename=" + fileName);
response.addHeader("Content-Length", "" + newFile.length());
toClient.write(buffer);
toClient.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 删除创建的新文件
this.deleteFile(newFile);
}

java后台动态生成导出excel的更多相关文章

  1. java使用poi生成导出Excel(新)

    导出样式: java代码: import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStre ...

  2. Java中导入、导出Excel

    原文:Java中导入.导出Excel 一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已 ...

  3. 我是陌生人 Java中导入、导出Excel

    我是陌生人 Java中导入.导出Excel 一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是: ...

  4. Java利用POI导入导出Excel中的数据

         首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...

  5. 如何用poi生成导出excel

    import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Sheet; import java. ...

  6. Java后台直接生成二维码介绍

    Java后台直接生成二维码 1.其实jquery也可以直接生成二维码的,但我测试的时候,二维码生成后太模糊,难以识别.所以在这里介绍在后来生成二维码的方式. 2.不善于文字描述,直接上代码了. imp ...

  7. [转]Java中导入、导出Excel

    原文地址:http://blog.csdn.net/jerehedu/article/details/45195359 一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样 ...

  8. Java的导入与导出Excel

    使用Jakarta POI导入.导出Excel Jakarta POI 是一套用于访问微软格式文档的Java API.Jakarta POI有很多组件组成,其中有用于操作Excel格式文件的HSSF和 ...

  9. java中使用poi导出excel表格数据并且可以手动修改导出路径

    在我们开发项目中,很多时候会提出这样的需求:将前端的某某数据以excel表格导出,今天就给大家写一个简单的模板. 这里我们选择使用poi导出excel: 第一步:导入需要的jar包到 lib 文件夹下

随机推荐

  1. Mha-Atlas-MySQL高可用

    Mha-Atlas-MySQL高可用 一.MHA简介 1.软件介绍 MHA在MySQL高可用是一个相对成熟的解决方案,是一套优秀的作为mysql高可用环境下故障切换和主从提升的高可用软件,在MySQL ...

  2. 京东饭粒捡漏V1.0.7

    20180614 更新 V1.0.71.修改捡漏策略 功能介绍1.京东商城专用,支持饭粒模式下单,自己获得京豆返利 2.捡漏模式:帮助用户监控抢购商品,有库存的时候进行抢单,主要是通过添加商品ID - ...

  3. 服务器变更IP地址后SSH链接失败的解决办法

    客户端未变,服务器端变更IP地址,导致客户端链接失败,这种情况提示如下: 原因是服务器端更改IP地址后,秘钥也需更新 在客户端输入以下格式的命令: ssh-keygen-f"/home/用户 ...

  4. 关于使用format()方法格式化字符串,读这一篇就够了!

    从Python 2.6开始,又出现了另外一种格式化字符串的方法——format()方法.format()方法是字符串众多方法中的一个,调用这个方法时要使用点操作符(.),该方法返回一个格式化好的字符串 ...

  5. MySQL MGR+ Consul之数据库高可用方案

    背景说明:     基于目前存在很多MySQL数据库单点故障,传统的MHA,PXC等方案用VIP或者DNS切换的方式可以实现.基于数据库的数据强一致性考虑,采用MGR集群,采用consul服务注册发现 ...

  6. mybatis缓存的设计

    继续用提问的方式来看Mybatis的缓存设计. 1.Mybatis如何开启缓存 Mybatis对查询结果进行缓存,所以缓存的对象为具体的Statement 通过在Statement上是否使用缓存来启用 ...

  7. .net core2.2

    GetCurrentDirectory returns the worker directory of the process started by IIS rather than the app's ...

  8. Unity Post-Processing的一些分享

    讲些什么? 绝大多数的游戏或多或少都会使用些后处理效果. 早期版本中,Unity在提供的接口有限,优化空间不大,属于放任自流.官方推出了Post-Processing(下文简称PP)并在Github上 ...

  9. Jquery 正则式验证

    // 验证中文名称 function isChinaName(name) { var pattern = /^[\u4E00-\u9FA5]{1,6}$/; return pattern.test(n ...

  10. GPIO输入输出各种模式(推挽、开漏、准双向端口)详解

    转自:https://blog.csdn.net/techexchangeischeap/article/details/72569999 概述 能将处理器的GPIO(General Purpose ...