<!-- poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.14</version>
</dependency>

controller.java

@RequestMapping("/exportFile")
public void exportFile(TbCdeShAgentRoadRateInBean form,HttpServletResponse response){ OutputStream os=null;
try {
Workbook wb=wbglService.exportFile(form);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
wb.write(bos);
response.setBufferSize(512);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
SimpleDateFormat dfs=new SimpleDateFormat("yyyy年mm月dd日HH时mm分ss秒");
//Date end=dfs.parse();
String fileName="委办公路"+dfs.format(new Date())+".xlsx";
response.addHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1"));
response.addHeader("Content-Length", String.valueOf(bos.toByteArray().length));
os=response.getOutputStream();
IOUtils.write(bos.toByteArray(),os);
} catch (Exception e) {
e.printStackTrace();
}finally{
IOUtils.closeQuietly(os);
}
}

service.java

public Workbook exportFile(TbCdeShAgentRoadRateInBean form) throws Exception{
TbCdeShAgentRoadRateInBean param=getSearchParam(form);
int count=iwbglDao.getCount(param);
int pageSize=1000;
List<TbCdeShAgentRoadRateOutBean> list=null;
//防止一次查询的数据量过大,导出服务器内存崩了
int totalPage=(new Double(Math.ceil(count/(pageSize*1.0)))).intValue();
Workbook wb=new SXSSFWorkbook();
Sheet sheet=wb.createSheet("委办公路");
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 15); // 生成一个样式
CellStyle style =wb.createCellStyle(); //workbook.createCellStyle();
// 设置这些样式
Font font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(font);
//标题行
Row rowTitle=sheet.createRow(0);
Cell cell=rowTitle.createCell(0);
cell.setCellStyle(style);
cell.setCellValue("序号");
//rowTitle.createCell(1).setCellValue("邮路名称"); cell=rowTitle.createCell(1);
cell.setCellStyle(style);
cell.setCellValue("邮路名称"); //rowTitle.createCell(2).setCellValue("邮路代码");
cell=rowTitle.createCell(2);
cell.setCellStyle(style);
cell.setCellValue("邮路代码");
//rowTitle.createCell(3).setCellValue("开行方式"); cell=rowTitle.createCell(3);
cell.setCellStyle(style);
cell.setCellValue("开行方式");
//rowTitle.createCell(4).setCellValue("代理商"); cell=rowTitle.createCell(4);
cell.setCellStyle(style);
cell.setCellValue("代理商");
//rowTitle.createCell(5).setCellValue("5T(元/车)"); cell=rowTitle.createCell(5);
cell.setCellStyle(style);
cell.setCellValue("5T(元/车)");
//rowTitle.createCell(6).setCellValue("8T(元/车)"); cell=rowTitle.createCell(6);
cell.setCellStyle(style);
cell.setCellValue("8T(元/车)");
//rowTitle.createCell(7).setCellValue("15T(元/车)"); cell=rowTitle.createCell(7);
cell.setCellStyle(style);
cell.setCellValue("15T(元/车)");
//rowTitle.createCell(8).setCellValue("25T(元/车)"); cell=rowTitle.createCell(8);
cell.setCellStyle(style);
cell.setCellValue("25T(元/车)");
//rowTitle.createCell(9).setCellValue("起始日期"); cell=rowTitle.createCell(9);
cell.setCellStyle(style);
cell.setCellValue("起始日期");
//rowTitle.createCell(10).setCellValue("结束日期"); cell=rowTitle.createCell(10);
cell.setCellStyle(style);
cell.setCellValue("结束日期");
int rowNum=1;
for(int currentPage=1;currentPage<=totalPage;currentPage++){
param.setSqlCurrentPage(currentPage);
param.setSqlPageSize(pageSize);
param.setDet(1L);
list=iwbglDao.selectList(param);
createExcelDataRow(sheet,list,rowNum);
}
return wb;
}
private int createExcelDataRow(Sheet sheet,List<TbCdeShAgentRoadRateOutBean> list,int rowNum){ for(TbCdeShAgentRoadRateOutBean b:list){
Row dataRow = sheet.createRow(rowNum);
dataRow.createCell(0).setCellValue(b.getRn());
dataRow.createCell(1).setCellValue(getDataValue(b.getMwayName()));
dataRow.createCell(2).setCellValue(getDataValue(b.getMwayCode()));
dataRow.createCell(3).setCellValue(getDataValue(b.getTransType()));
dataRow.createCell(4).setCellValue(getDataValue(b.getAgentCop()));
dataRow.createCell(5).setCellValue(getDataValue(b.getFiveT()));
dataRow.createCell(6).setCellValue(getDataValue(b.getEightT()));
dataRow.createCell(7).setCellValue(getDataValue(b.getFifteenT()));
dataRow.createCell(8).setCellValue(getDataValue(b.getTwentyFiveT()));
dataRow.createCell(9).setCellValue(getDataValue(b.getStartDate()));
//dataRow.getCell(9).setCellType(Cell.CELL_TYPE_STRING);
dataRow.createCell(10).setCellValue(getDataValue(b.getEndDate()));
//dataRow.getCell(10).setCellType(Cell.CELL_TYPE_STRING);
rowNum++;
}
return rowNum;
}
private String getDataValue(Object o){
return o==null?"":o.toString();
}

html

//导出数据
window.location.href="${path}/wbgl/exportFile?a=b&c=d"

java下载excel,excel从数据库获取数据的更多相关文章

  1. easyui-datagrid通过action从数据库获取数据的关键代码

    实际上是结合struts2来从数据获取json格式的数据.   关键代码: GetUserAction.java代码   package com.log.control; import java.io ...

  2. Loadrunner脚本优化-参数化之关联MySQL数据库获取数据

    脚本优化-参数化之关联MySQL数据库获取数据 by:授客 QQ:1033553122 测试环境: Loadrunner 11 Win7 64位 实操: 1.   安装MySQL ODBC驱动程序 O ...

  3. 如何用asp.net MVC框架、highChart库从sql server数据库获取数据动态生成柱状图

    如何用asp.net MVC框架.highChart库从sql server数据库获取数据动态生成柱状图?效果大概是这样的,如图: 请问大侠这个这么实现呢?

  4. AngularJs从数据库获取数据并显示

    哈哈,昨天下午和今天早上,花上一些时间,学习AngularJs,仅是粗略预览一下.很好,非常好. 由于手上有开发ASP.NET MVC环境,就在这测试下,去数据库获取数据并显示数据. 数据库表创建,添 ...

  5. Python使用Flask框架,结合Highchart处理csv数据(引申-从文件获取数据--从数据库获取数据)

    参考链接:https://www.highcharts.com.cn/docs/process-text-data-file 1.javascript代码 var options = { chart: ...

  6. 从Excel、CSV文件获取数据

    #region 从Excel获取数据 /// <summary> /// 从Excel获取数据 /// </summary> /// <param name=" ...

  7. 用struts2标签如何从数据库获取数据并在查询页面显示。最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变量。

    最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变 ...

  8. Excel VBA连接MySql 数据库获取数据

    编写Excel VBA工具,连接并操作Mysql 数据库. 系统环境: OS:Win7 64位 英文版 Office 2010 32位 英文版 1.VBA连接MySql前的准备 Tools---> ...

  9. 动态从数据库获取数据,省市县三级联动,有校验,导出Excel模板

    话不多说,看效果图,直接上代码. sheet  商户表 hideSheet ,功能完成后隐藏的Sheet,用于储存下拉框中的信息,(以一定的规则将所需数据存储在表格中). 下面是代码 部分数据需要在导 ...

随机推荐

  1. HTTP长连接、短连接究竟

    HTTP长连接.短连接究竟是什么?   1. HTTP协议与TCP/IP协议的关系 HTTP的长连接和短连接本质上是TCP长连接和短连接.HTTP属于应用层协议,在传输层使用TCP协议,在网络层使用I ...

  2. Understanding how uid and gid work in Docker containers

    转自:https://medium.com/@mccode/understanding-how-uid-and-gid-work-in-docker-containers-c37a01d01cf Un ...

  3. Go语言编程 (许式伟 等 著)

    第1章 初识Go语言 1.1 语言简史 1.2 语言特性 1.2.1 自动垃圾回收 1.2.2 更丰富的内置类型 1.2.3 函数多返回值 1.2.4 错误处理 1.2.5 匿名函数和闭包 1.2.6 ...

  4. Unity项目架构设计与开发管理 学习

    视频地址:https://v.qq.com/x/page/d016340mkcu.html assetstore save manager

  5. apache HTML5 History 模式 配置

    说明 使用的  Apache/2.4.6 版本. 文档这么写的: 但是 没说  IfModule 放在哪里 . httpd.conf  里面有大量的  IfModule . 这样的.但是实测 无效.无 ...

  6. NET设计模式 第二部分 创建型模式(4):工厂方法模式(Factory Method)

    工厂方法模式(Factory Method) ——.NET设计模式系列之五 Terrylee,2004年1月2日 概述 在软件系统中,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实 ...

  7. mysql 主从复制参数slave_net_timeout

    slave_net_timeout slave_net_timeout表示slave在slave_net_timeout时间之内没有收到master的任何数据(包括binlog,heartbeat), ...

  8. Spark任务提交jar包依赖解决方案

    转载自:http://blog.csdn.net/wzq294328238/article/details/48054525                    通常我们将Spark任务编写后打包成 ...

  9. 网易云课堂Dubbo学习笔记

    可以在这里下载dubbo的原码:https://github.com/alibaba/dubbo Dubbo架构原理:

  10. S2算法应用

    需求:计算不同区域范围,X公里半径内实体店或场站覆盖率. 实现思路: 为了便于理解,将地球看成一个基于经纬度线的坐标系.将经度和纬度看成二维坐标系中的两个纬度,横轴表示经度[-180o, 0o),(0 ...