<!-- 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. What are long running processes?

    转自:https://blog.bernd-ruecker.com/what-are-long-running-processes-b3ee769f0a27 Some communities have ...

  2. Singer 学习五 docker 运行说明

    介绍过一个工具knots ,方便Singer 可视化开发的工具,但是默认这个工具包含的tap 以及target 比较少(可以自己扩展) 同时这个工具就是基于docker 运行的 docker 运行的几 ...

  3. oracle-rman-2

    归档日志的备份 RMAN> list archivelog all;show archivelog deletion policy;configure archivelog deletion p ...

  4. oracle-rman-1

    备份脚本 ############################# [oracle@rac02 backup_scripts]$ cat rman_bus_level_0_bk.sh######## ...

  5. FastAdmin Bootstrap-Table 自定义搜索的重写提示

    Bootstrap-Table 自定义搜索的重写提示 群友询问:这个搜索能自己写么? [群主]Karson-深圳(请勿@) "★找大神-山西 10:59:32 查看原文 [图片] 这个搜索能 ...

  6. Linux下Redis开机自启(Centos6)

    1.设置redis.conf中daemonize为yes,确保守护进程开启. 查找redis配置文件redis.conf [root@localhost /]# find / -name redis. ...

  7. HanLP用户自定义词典源码分析详解

    1. 官方文档及参考链接 l 关于词典问题Issue,首先参考:FAQ l 自定义词典其实是基于规则的分词,它的用法参考这个issue l 如果有些数量词.字母词需要分词,可参考:P2P和C2C这种词 ...

  8. Linux之用户组、文件权限详解

    用户和用户组 文件所有者(u) 一般为文件的创建者,谁创建了该文件,就天然的成为该文件的所有者 用ls ‐ahl命令可以看到文件的所有者 也可以使用chown 用户名 文件名来修改文件的所有者 用户组 ...

  9. PHP扩展开发:第一个扩展

    在上一篇文章<PHP扩展开发:安装PHP>我们已经将开发PHP扩展的PHP环境安装成功,那么接下来采用最简单直接的方式创建第一个扩展. 我们先假设业务场景,是需要有这么一个扩展,提供一个叫 ...

  10. PHP中使用sleep函数实现定时任务实例分享

    在某些程序中,有一些特殊的功能需要用到定时执行,如果熟悉Linux的朋友肯定会说这不是容易吗,直接来个计划任务crontab不久实现了吗?这的确是可以实现,但必须是提前知道具体的执行时间,然后才能写到 ...