数据导出Excel,动态列
今天碰到一个需求,要求将用户回答的问卷及问题导出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,动态列的更多相关文章
- C#导出Excel动态列
一.用StreamWrite流对象,导出Excel 1. string _sPath = GenerateSalaryMonthlyReport(dgvSalarySum); System.Diagn ...
- Java使用POI实现数据导出excel报表
Java使用POI实现数据导出excel报表 在上篇文章中,我们简单介绍了java读取word,excel和pdf文档内容 ,但在实际开发中,我们用到最多的是把数据库中数据导出excel报表形式.不仅 ...
- Saiku导出excel指标列无线条以及0与空值显示问题(三十二)
Saiku导出excel指标列无线条以及0与空值显示问题 描述: 数据库中字段值为0 ,与数据库中字段值为 null 时 ,saiku会将为0 以及为 null 的数据都不展示出来,但是我们其实希望数 ...
- 百度地图里面搜索到的公司商家电话导出表格?怎样将把百度地图里面搜索到的公司 电话 地址 等数据导出excel里?
好多人在问:如何将百度地图里面搜索到的公司商家电话导出表格?怎样将把百度地图里面搜索到的公司 电话 地址 等数据导出excel里? 现在,很多人都在网络上找商家,联系业务. 百度地图里有很多的商家联系 ...
- 【asp.net】将GridView数据导出Excel
概要: 中午睡了一会,醒来的时候看到老师叫我去办公室,需求是这样的,把excel表中的每个同学,判断图片目录中是否有对应的照片(图片的名字用的学号或身份证号码) 没有对应图片的学生记录,存入自己的数据 ...
- JavaScript 上万条数据 导出Excel文件(改装版)
最近项目要js实现将数据导出excel文件,网上很多插件实现~~那个开心呀,谁知道后面数据量达到上万条时出问题:浏览器不仅卡死,导出的excel文件一直提示网络失败.... debug调试发现var ...
- JavaScript 上万条数据 导出Excel文件 页面卡死
最近项目要js实现将数据导出excel文件,网上很多插件实现~~那个开心呀,谁知道后面数据量达到上万条时出问题:浏览器不仅卡死,导出的excel文件一直提示网络失败.... debug调试发现var ...
- 将页面中表格数据导出excel格式的文件(vue)
近期由于项目需要,需要将页面中的表格数据导出excel格式的文件,折腾了许久,在网上各种百度,虽然资料不少,但是大都不全,踩了许多坑,总算是皇天不负有心人,最后圆满解决了. 1.安装相关依赖(npm安 ...
- yii2 数据导出 excel导出以及导出数据时列超过26列时解决办法
作者:白狼 出处:http://www.manks.top/article/yii2_excel_extension 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给 ...
随机推荐
- 传统的线程互斥技术:Synchronized关键字
多个线程操作同一个方法或变量时常常出现错误,要保证每个线程都正常运行就要通过加锁,每次只有一个能够拿到锁通过.如下: package cn.sp.thread; /** * Created by 2Y ...
- maven项目管理1
1.maven的目录结构 src -main -java -package -test -java -package resources 2.maven命令 mvn -v :查看maven版本命令 c ...
- Poj 2289 Jamie's Contact Groups (二分+二分图多重匹配)
题目链接: Poj 2289 Jamie's Contact Groups 题目描述: 给出n个人的名单和每个人可以被分到的组,问将n个人分到m个组内,并且人数最多的组人数要尽量少,问人数最多的组有多 ...
- (三)python函数式编程
一.高阶函数 高阶函数英文叫Higher-order function.什么是高阶函数?我们以实际代码为例子,一步一步深入概念. 变量可以指向函数 结论:函数本身也可以赋值给变量,即:变量可以指向函数 ...
- Permutation UVA - 11525(值域树状数组,树状数组区间第k大(离线),log方,log)(值域线段树第k大)
Permutation UVA - 11525 看康托展开 题目给出的式子(n=s[1]*(k-1)!+s[2]*(k-2)!+...+s[k]*0!)非常像逆康托展开(将n个数的所有排列按字典序排序 ...
- 找规律 Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks
题目传送门 /* 找规律,水 */ #include <cstdio> #include <iostream> #include <algorithm> #incl ...
- 题解报告:hdu 2709 Sumsets
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2709 Problem Description Farmer John commanded his co ...
- JD商家后台管理的细节
1: 宝贝主图和滚动图都是800px,只有刚好这么多时才能得到显示,否则不会显示. 2:宝贝描述图只支持750px, 只有这么多时才能得到显示, 刚开始不知道, 上传图片上去后, 发现始终无法显示, ...
- 转-AFNetwork 作用和用法详解
来自:http://www.maxiaoguo.com/clothes/269.html AFNetworking是一个轻量级的iOS网络通信类库.它建立在NSURLConnection和NSOper ...
- 215 Kth Largest Element in an Array 数组中的第K个最大元素
在未排序的数组中找到第 k 个最大的元素.请注意,它是数组有序排列后的第 k 个最大元素,而不是第 k 个不同元素.例如,给出 [3,2,1,5,6,4] 和 k = 2,返回 5.注意事项:你可以假 ...