java中的Excel导出功能
public void exportExcel(Long activityId, HttpServletResponse response) throws IOException
{
// 获取统计报表信息
List<ProductInfo> productInfoList = reportDao.queryStatisticReport(activityId);
// 创建一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建一个工作表sheet
HSSFSheet sheet = workbook.createSheet("统计报表");
// 设置单元格每列的宽度
sheet.setColumnWidth(0, 50 * 256);
sheet.setColumnWidth(1, 50 * 256);
sheet.setColumnWidth(2, 10 * 256);
// 表格信息内容的样式
HSSFCellStyle style = workbook.createCellStyle();
// 水平居中
style.setAlignment(HorizontalAlignment.CENTER);
// 垂直居中
style.setVerticalAlignment(VerticalAlignment.CENTER);
// 行数
int rowCount = 0;
// 第一行的标题行
HSSFRow row0 = sheet.createRow(rowCount++);
// 列表格
HSSFCell cell_title = row0.createCell(0);
cell_title.setCellValue("统计报表");
// 标题样式
HSSFCellStyle style_title = workbook.createCellStyle();
style_title.setAlignment(HorizontalAlignment.CENTER);
style_title.setVerticalAlignment(VerticalAlignment.CENTER);
//设置字体样式
HSSFFont font = workbook.createFont();
//字号
font.setFontHeightInPoints((short) 16);
// 红色字体
font.setColor(HSSFFont.COLOR_RED);
style_title.setFont(font);
cell_title.setCellStyle(style_title);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
// 第二行的表头
HSSFRow row1 = sheet.createRow(rowCount++);
// 表头样式
HSSFCellStyle style_header = workbook.createCellStyle();
style_header.setAlignment(HorizontalAlignment.CENTER);
style_header.setVerticalAlignment(VerticalAlignment.CENTER);
style_header.setFillForegroundColor(new HSSFColor.SKY_BLUE().getIndex());
style_header.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// 列表格
HSSFCell cell_header0 = row1.createCell(0);
cell_header0.setCellValue("展示商品名称");
cell_header0.setCellStyle(style_header);
HSSFCell cell_header1 = row1.createCell(1);
cell_header1.setCellValue("SKU商品名称");
cell_header1.setCellStyle(style_header);
HSSFCell cell_header2 = row1.createCell(2);
cell_header2.setCellValue("数量");
cell_header2.setCellStyle(style_header);
if (!StringUtils.isEmpty(productInfoList))
{
for (ProductInfo productInfo : productInfoList)
{
List<SbomInfo> sbomInfoList = productInfo.getSbomInfoList();
for (int i = 0; i < sbomInfoList.size(); i++)
{
// 行表格
HSSFRow row = sheet.createRow(rowCount);
SbomInfo sbomInfo = sbomInfoList.get(i);
if (i == 0)
{
// 列表格
HSSFCell cell0 = row.createCell(0);
cell0.setCellValue(productInfo.getDisPrdName());
cell0.setCellStyle(style);
// 含有2个以上数据的,则需要合并单元格
if (sbomInfoList.size() > 1)
{
sheet.addMergedRegion(new CellRangeAddress(rowCount, rowCount + sbomInfoList.size() - 1, 0, 0));
}
}
// 列表格
HSSFCell cell1 = row.createCell(1);
cell1.setCellValue(sbomInfo.getSbomName());
cell1.setCellStyle(style);
// 列表格
HSSFCell cell2 = row.createCell(2);
cell2.setCellValue(sbomInfo.getNumber());
cell2.setCellStyle(style);
// 行数增1
rowCount = rowCount + 1;
}
}
}
// 设置文件名
String fileName = "统计报表.xls";
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.setHeader("Pragma", "No-cache");
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
workbook.close();
}
导出Excel表格:

java中的Excel导出功能的更多相关文章
- Atitit.excel导出 功能解决方案 php java C#.net版总集合.doc
Atitit.excel导出 功能解决方案 php java C#.net版总集合.docx 1.1. Excel的保存格式office2003 office2007/2010格式1 1.2. 类库选 ...
- Java中导入、导出Excel
原文:Java中导入.导出Excel 一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已 ...
- 我是陌生人 Java中导入、导出Excel
我是陌生人 Java中导入.导出Excel 一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是: ...
- 利用Aspose.Cells完成easyUI中DataGrid数据的Excel导出功能
我准备在项目中实现该功能之前,google发现大部分代码都是利用一般处理程序HttpHandler实现的服务器端数据的Excel导出,但是这样存在的问题是ashx读取的数据一般都是数据库中视图的数据, ...
- 用SpringMvc实现Excel导出功能
以前只知道用poi导出Excel,最近用了SpringMvc的Excel导出功能,结合jxl和poi实现,的确比只用Poi好,两种实现方式如下: 一.结合jxl实现: 1.引入jxl的所需jar包: ...
- excel导出功能优化
先说说优化前,怎么做EXCEL导出功能的: 1. 先定义一个VO类,类中的字段按照EXCEL的顺序定义,并且该类只能用于EXCEL导出使用,不能随便修改. 2. 将查询到的结果集循环写入到这个VO类中 ...
- excel导出功能原型
本篇博客是记录自己实现的excel导出功能原型,下面我将简单介绍本原型: 这是我自制的窗体,有一个ListView和一个Button(导出)控件. 这是我在网上找到了使用exel需要引用的库. usi ...
- java(POI):基于模版的Excel导出功能,局部列写保护总结
需求描述: 1.导出的Excel中部分列包含有下拉列表,并没有尝试过用代码实现这种功能,个人感觉比较棘手,故采用了模版的形式,直接导出数据到已经创建好的Excel模版中 2.Excel的第一列需要写保 ...
- java中的数据导出到Excel表中
整个项目中导出数据到.Excel的源码 import java.io.BufferedOutputStream; import java.io.FileInputStream; import java ...
随机推荐
- RedisTemplate访问Redis数据结构(前言)
Redis五种基本数据结构 redis提供键值对的形式对数据进行存储.支持五种数据类型:String(字符串),List(链表),Hash(散列),Set(无序集合),ZSet(有序集合).下面是网上 ...
- SpringBoot属性配置-第三章
1.application.yml配置#自定义参数对象book: name: A id: 1 page: 100 2.创建实体类: /** * @Auther: youqc * @Date: 2018 ...
- ES6 对象超类
var parent = { foo() { console.log("Hello from the Parent"); } } var child = { foo() { sup ...
- IDEA中代码不小心删除,或者改了半天想回退到某个特定时间怎么办?
第一步: 第二步: 第三步: 第四步:
- 解决RHEL6 vncserver 启动 could not open default font 'fixed'错误.
https://blog.csdn.net/silencegll/article/details/51320629
- TypeScript快速笔记(二)
1) TypeScript中的判断语句,可以使用非0值代表true.如: function add1(a: number, b?:number): number{ // 注意b是可选参数 consol ...
- vue 拖动调整左右两侧div的宽度
原文链接:https://www.cnblogs.com/layaling/p/11009570.html 原文是左中右三种情况的拖动.由于项目需要,我删除掉了右边的,直接左右区域拖动调整div宽度 ...
- Linux中的NetworkManager网络管理
转载关于 NM_CONTROLLED和Network Manager Redhat在RHEL 6(Redhat Enterprise Linux),上搞了一个 Network manger 服务(同样 ...
- Stream 源码分析
Stream 支持顺序和并行聚合操作的一组元素序列. 1)operations:支持在单个元素上执行的操作,流操作分为中间操作和终止操作 1-1)中间操作: 1-1-1)无状态:unordered() ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_02 泛型_3_定义和使用含有泛型的类
创建一个类,添加一个name的属性,然后生成get和set 使用上面创建的类 使用泛型 所以我们取出来也是一个Object的类型 定义的时候规定的类型是Integer,所以这里setName设置的时候 ...