今天碰到一个需求,要求将用户回答的问卷及问题导出Excel表格,问卷对应的问题数量不一致,需要动态添加列表头,简单记录。

要导出Excel需要添加poi.jar包

用户-问卷实体(固定列):

package com.lwl.bean;

import com.util.annotation.BeanField;
import lombok.Data; import java.sql.Timestamp;
import java.util.List; /**
* 问卷实体(用于导出excel)
* @author linwenli
*/
@Data
public class HyMktUserQuesBean { @BeanField("用户名")
private String wechatName;
@BeanField("联系电话")
private String telephone;
@BeanField("主题名称")
private String questionName;
@BeanField("参与时间")
private Timestamp createTime;
@BeanField("问题内容")
private List<HyMktUserQuesAnswerBean> hyMktUserQuesAnswerBeans;
}

用户-问卷问题实体(动态列):

package com.lwl.bean;

import com.util.annotation.BeanField;
import lombok.Data; /**
* 问题及用户答案
* @author linwenli
*/
@Data
public class HyMktUserQuesAnswerBean {
@BeanField("问题名称")
private String problemName;
@BeanField("答案")
private String optionName;
}

导出方法:

package com.lwl.util;

import com.lwl.bean.HyMktUserQuesAnswerBean;
import com.lwl.bean.HyMktUserQuesBean;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*; import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List; public class ExcelUtil { /**
* 根据List<HyMktUserQuesBean> 导出数据到Excel
* @author linwenli
* @date 2019/5/09 15:27
* @param response
* @param fileName
* @param hyMktUserQuesBeans
* @throws IOException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void writeExcel(HttpServletResponse response, String fileName, List<HyMktUserQuesBean> hyMktUserQuesBeans) throws IOException, IllegalArgumentException, IllegalAccessException { HSSFWorkbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet(); // 数据表头开始行
CellStyle style = wb.createCellStyle();
Font font = wb.createFont();
font.setFontName("宋体");
// 设置字体大小
font.setFontHeightInPoints((short) 12);
// 加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 设置背景色
style.setFillForegroundColor(HSSFColor.LIME.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// 让单元格居中
style.setAlignment(HSSFCellStyle.SOLID_FOREGROUND);
// 左右居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 上下居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setWrapText(true);//设置自动换行
style.setFont(font); // 添加表数据
// 取出列表中问卷问题最多的对象做动态表头
HyMktUserQuesBean problemMax = null;
int maxSize = 0;
for (int n = 0; n < hyMktUserQuesBeans.size(); n++) {
HyMktUserQuesBean hyMktUserQuesBean = hyMktUserQuesBeans.get(n);
// 记录最大问题个数
if (hyMktUserQuesBean.getHyMktUserQuesAnswerBeans().size() > maxSize) {
maxSize = hyMktUserQuesBean.getHyMktUserQuesAnswerBeans().size();
problemMax = hyMktUserQuesBean;
}
int index = 0;
// 写excel数据
for (int i = 1; i < hyMktUserQuesBean.getHyMktUserQuesAnswerBeans().size(); i++) {
// 第零行为表头行,不填充数据
Row row = sheet.createRow(n + 1);
// 用户名
Cell firstCell = row.createCell(index);
firstCell.setCellType(Cell.CELL_TYPE_STRING);
firstCell.setCellValue(hyMktUserQuesBean.getWechatName());
sheet.autoSizeColumn((short) index++);// 设置单元格自适应
// 联系电话
Cell secondCell = row.createCell(index);
secondCell.setCellType(Cell.CELL_TYPE_STRING);
secondCell.setCellValue(hyMktUserQuesBean.getTelephone());
sheet.autoSizeColumn((short) index++);// 设置单元格自适应
// 主题名称
Cell thirdCell = row.createCell(index);
thirdCell.setCellType(Cell.CELL_TYPE_STRING);
thirdCell.setCellValue(hyMktUserQuesBean.getQuestionName());
sheet.autoSizeColumn((short) index++);// 设置单元格自适应
// 参与时间
Cell forthCell = row.createCell(index);
forthCell.setCellType(Cell.CELL_TYPE_STRING);
forthCell.setCellValue(DateUtil.translateDate(hyMktUserQuesBean.getCreateTime().getTime()));
sheet.autoSizeColumn((short) index++);// 设置单元格自适应
// 动态表头
List<HyMktUserQuesAnswerBean> hyMktUserQuesAnswerBeans = hyMktUserQuesBean.getHyMktUserQuesAnswerBeans();
for(int k = 0; k < hyMktUserQuesAnswerBeans.size(); k++ ){
// 问题
Cell otherOneCell = row.createCell(index);
otherOneCell.setCellType(Cell.CELL_TYPE_STRING);
otherOneCell.setCellValue(hyMktUserQuesAnswerBeans.get(k).getProblemName());
sheet.autoSizeColumn((short) index++);// 设置单元格自适应
// 答案
Cell otherTwoCell = row.createCell(index);
otherTwoCell.setCellType(Cell.CELL_TYPE_STRING);
otherTwoCell.setCellValue(hyMktUserQuesAnswerBeans.get(k).getOptionName());
sheet.autoSizeColumn((short) index++);// 设置单元格自适应
}
}
}
//添加表头
Row row = sheet.createRow(0);
int index = 0;
// 用户名
Cell indexCell = row.createCell(index);
indexCell.setCellType(Cell.CELL_TYPE_STRING);
indexCell.setCellStyle(style);//设置表头样式
indexCell.setCellValue("用户名");
sheet.autoSizeColumn((short) index++);// 设置单元格自适应
// 联系电话
Cell indexCell2 = row.createCell(index);
indexCell2.setCellType(Cell.CELL_TYPE_STRING);
indexCell2.setCellStyle(style);//设置表头样式
indexCell2.setCellValue("联系电话");
sheet.autoSizeColumn((short) index++);// 设置单元格自适应
// 主题名称
Cell indexCell3 = row.createCell(index);
indexCell3.setCellType(Cell.CELL_TYPE_STRING);
indexCell3.setCellStyle(style);//设置表头样式
indexCell3.setCellValue("主题名称");
sheet.autoSizeColumn((short) index++);// 设置单元格自适应
// 参与时间
Cell indexCell4 = row.createCell(index);
indexCell4.setCellType(Cell.CELL_TYPE_STRING);
indexCell4.setCellStyle(style);//设置表头样式
indexCell4.setCellValue("参与时间");
sheet.autoSizeColumn((short) index++);// 设置单元格自适应
for(int j = 0; j < problemMax.getHyMktUserQuesAnswerBeans().size(); j++ ){
// 问题
Cell otherOneCell = row.createCell(index);
otherOneCell.setCellType(Cell.CELL_TYPE_STRING);
otherOneCell.setCellStyle(style);//设置表头样式
otherOneCell.setCellValue("问题" + (j + 1));
sheet.autoSizeColumn((short) index++);// 设置单元格自适应
// 答案
Cell otherTwoCell = row.createCell(index);
otherTwoCell.setCellType(Cell.CELL_TYPE_STRING);
otherTwoCell.setCellStyle(style);//设置表头样式
otherTwoCell.setCellValue("问题" + (j + 1) + "答案");
sheet.autoSizeColumn((short) index++);// 设置单元格自适应
}
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=\"" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".xls" + "\"");
OutputStream ouputStream = null;
try {
ouputStream = response.getOutputStream();
wb.write(ouputStream);
} finally {
ouputStream.close();
}
}
}

导出结果:

数据导出Excel,动态列的更多相关文章

  1. C#导出Excel动态列

    一.用StreamWrite流对象,导出Excel 1. string _sPath = GenerateSalaryMonthlyReport(dgvSalarySum); System.Diagn ...

  2. Java使用POI实现数据导出excel报表

    Java使用POI实现数据导出excel报表 在上篇文章中,我们简单介绍了java读取word,excel和pdf文档内容 ,但在实际开发中,我们用到最多的是把数据库中数据导出excel报表形式.不仅 ...

  3. Saiku导出excel指标列无线条以及0与空值显示问题(三十二)

    Saiku导出excel指标列无线条以及0与空值显示问题 描述: 数据库中字段值为0 ,与数据库中字段值为 null 时 ,saiku会将为0 以及为 null 的数据都不展示出来,但是我们其实希望数 ...

  4. 百度地图里面搜索到的公司商家电话导出表格?怎样将把百度地图里面搜索到的公司 电话 地址 等数据导出excel里?

    好多人在问:如何将百度地图里面搜索到的公司商家电话导出表格?怎样将把百度地图里面搜索到的公司 电话 地址 等数据导出excel里? 现在,很多人都在网络上找商家,联系业务. 百度地图里有很多的商家联系 ...

  5. 【asp.net】将GridView数据导出Excel

    概要: 中午睡了一会,醒来的时候看到老师叫我去办公室,需求是这样的,把excel表中的每个同学,判断图片目录中是否有对应的照片(图片的名字用的学号或身份证号码) 没有对应图片的学生记录,存入自己的数据 ...

  6. JavaScript 上万条数据 导出Excel文件(改装版)

    最近项目要js实现将数据导出excel文件,网上很多插件实现~~那个开心呀,谁知道后面数据量达到上万条时出问题:浏览器不仅卡死,导出的excel文件一直提示网络失败.... debug调试发现var  ...

  7. JavaScript 上万条数据 导出Excel文件 页面卡死

    最近项目要js实现将数据导出excel文件,网上很多插件实现~~那个开心呀,谁知道后面数据量达到上万条时出问题:浏览器不仅卡死,导出的excel文件一直提示网络失败.... debug调试发现var  ...

  8. 将页面中表格数据导出excel格式的文件(vue)

    近期由于项目需要,需要将页面中的表格数据导出excel格式的文件,折腾了许久,在网上各种百度,虽然资料不少,但是大都不全,踩了许多坑,总算是皇天不负有心人,最后圆满解决了. 1.安装相关依赖(npm安 ...

  9. yii2 数据导出 excel导出以及导出数据时列超过26列时解决办法

    作者:白狼 出处:http://www.manks.top/article/yii2_excel_extension​ 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给 ...

随机推荐

  1. Spring @requestBody

    页面提交请求参数有两种,一种是form格式,一种是json格式 jQuery的$.post方法虽然也可以传递json格式数据,但实际上是用的form格式提交,jquery会帮你把json转成form格 ...

  2. java 学习书籍

    <Effective java> <深入理解java虚拟机> <Java并发编程实践> <Java Performance> <java解惑> ...

  3. 《windows核心编程系列》五谈谈线程基础

    线程基础 与前面介绍的进程一样,线程也有两部分组成.一个是线程内核对象.它是一个数据结构,操作系统用它来管理线程以及用它来存储线程的一些统计信息.另一个是线程栈,用于维护线程执行时所需的所有函数参数和 ...

  4. 字体使用sp、dp的区别

    Android设置字体大小, 该用sp还是dp? 大部分人肯定脱口而出, 用sp啊, 傻瓜都知道要用sp而不是dp!!! 那么为什么呢? 可能有人会说, 是google官方专门定义了sp这个单位来描述 ...

  5. [转]Boosting

    1 Boosting算法的起源 Boosting方法是一种用来提高弱分类算法准确度的方法,这种方法通过构造一个预测函数系列,然后以一定的方式将他们组合成一个预测函数.Boosting是一种提高任意给定 ...

  6. windows8.1专业版 关闭ie11总是已停止工作

    该问题通常原因: 1 系统重病毒: 2 系统和安装的软件不兼容导致. 解决方案: 1 杀毒更新至最新进行杀毒,仍未解决,重新安装系统: 2 目前身边人员多数属于该情况: 1 如安装了输入法.迅雷或其它 ...

  7. 使用 JSX 描述 UI 信息

    这一节我们通过一个简单的例子讲解 React.js 描述页面 UI 的方式.把 src/index.js 中的代码改成: import React, { Component } from 'react ...

  8. javascript 笔记--变量

    用了这么久的Javascript,该总结下了!温故而知新! var 声明变量: javascript 是弱类型语言,因此无需为声明对象明确类型声明. 如:var test="字符串" ...

  9. TCP/IP 协议、地址

    (一)TCP/IP 协议是端与端之间通信的基础,计算机网络,分组报文,协议是将信息正确传递的组成. 计算机网络: 由多个客户端,中间路由组成的网络体系,每个节点都一个ip进行唯一识别.路由是作为一个转 ...

  10. [转] 以超级管理员身份运行bat

    (转自:以超级管理员身份运行bat - lishirong   原文日期:2013.07.04) 废话不多说,直接上代码: -------------------------------------- ...