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. 关于UI自动化中元素定位常用方法的个人总结

    1.如果目标元素有id属性,优先使用id定位: 2.元素locator尽可能保证简洁,考虑locator中路径的变化频率,尽量减少后期更新和维护成本: 3.使用xpath时,不要一味的使用‘/’逐层进 ...

  2. JSON AST 生成MD

    使用 JsonLite 获取 JSON  AST class Program { static void Main(string[] args) { string fileName = $" ...

  3. Struts2国际化学习笔记

    今天在家里学习了Struts2中的国际化技术,国际化技术其实我的理解就是同一个网站项目或者同一个网页能够自由切换或者自适应本地语言,并根据本地语言进行网页展示. 实现Struts2中的国际化的方法是: ...

  4. SerialPort项目配置

    app的build.gradle下: apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultCon ...

  5. jquery fileupload

    jquery file upload示例  https://blog.csdn.net/qq_37936542/article/details/79258158

  6. S2-052 RCE漏洞 初步分析

    PS:初步分析,只是分析了Struts2 REST插件的部分,本来菜的抠脚不敢发,但看到各大中心发的也没比我高到哪里去,索性发出来做个记事! 漏洞描述 2017年9月5日,Apache Struts发 ...

  7. js长整型的失真问题解决

    遇到的问题: 后端返回的订单号是整型的,超过了Math.pow(2,53) = 9007199254740992,导致获取的数据失真. 类似问题:https://www.zhihu.com/quest ...

  8. The component and implementation of a basic gradient descent in python

    in my impression, the gradient descent is for finding the independent variable that can get the mini ...

  9. Eclipse识别不了jsp中的${pageContxt.request.contextPath }

    按计划这周系统学下Struts,但是搭完框架后jsp页面识别不了${pageContxt.request.contextPath } So这篇就记录一下我是怎么解决这个问题的 不管是tomcat7.0 ...

  10. Hibernate的查询方式

    Hibernate的查询的方式 OID检索 get或load方法进行的查询 对象导航检索 linkman.getCustomer(); HQL检索 用于接收hql语句进行查询,面向对象查询方式. 通过 ...