数据导出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 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给 ...
随机推荐
- 图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces
题目传送门 /* 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) */ #include <cstdio> #include <cs ...
- DP Codeforces Round #260 (Div. 1) A. Boredom
题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...
- 转-关于UIView的autoresizingMask属性的研究
在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. 1 2 3 4 5 6 7 8 9 enum ...
- 转】用Mahout构建职位推荐引擎
原博文出自于: http://blog.fens.me/hadoop-mahout-recommend-job/ 感谢! 用Mahout构建职位推荐引擎 Hadoop家族系列文章,主要介绍Hadoop ...
- jdk 1.8下 java ArrayList 添加元素解析
转载请注明http://www.cnblogs.com/majianming/p/8006452.html 有人问我,java ArrayList底层是怎么实现的?我就回答数组,他再问我,那它是怎么实 ...
- [转]Windows Azure安全概述
本文转自:http://blogs.msdn.com/b/azchina/archive/2011/03/06/windows_5f00_azure_5f00_security_5f00_overvi ...
- 不重启IIS修改dotnet framework版本
因为公司现在存在.net站点和asp站点共同运行的情况,所以需要对IIS进行一些修改,运行环境Win2003+IIS6 一.起因 原来的老站是asp开发的,用的是.net 2.0运行环境; 新站是.n ...
- Redis和SpringDataRedis
一.Redis简介 Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库,运行在内存中,由ANSI C编写.企业开发通常采用Redis来实现缓存.同类的产品还有memcac ...
- 12 DOM操作应用
1.创建子元素oLi=document.creatElement('li') 2.将元素附给父级元素oUl.appendChild(oLi) 3.将元素插入到父级元素里的第一位子元素之前oUl.ins ...
- R in action读书笔记(8)-第八章:回归(上)
8.1回归的多面性 8.2 OLS回归 OLS回归拟合模型形式: 为了能够恰当地解释oLs模型的系数,数据必须满足以下统计假设. 口正态性对于固定的自变量值,因变量值成正态分布. 口独立性Yi值之间相 ...