java 导mysql数据为表格给浏览器接收
jar 包准备
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
后端
@Override
public List<Orders> selectList(Map<String,String> params) { SqlSession sqlSession = sqlSessionFactory.openSession(true); List<Orders> list = sqlSession.selectList("com.xwhbank.cn.config.mybatis.namespace.ordersMapper.selectList", params); return list; }
<select id="selectList" parameterType="java.util.Map" resultType="orders">
select * from orders where the_super_id = #{the_super_id} and state = '2' and order_time >= #{daochu01} and order_time <= #{daochu02};
</select>
List<Orders> list = ordersService.selectList(params); Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("stuDB");//stuDB 改为什么都行
Row row = sheet.createRow((short)0);//0 Row 此行设置表格的头部
Cell cell = null;
cell = row.createCell((short)0);
cell.setCellValue("tenant_order_id");//表格某一列头部的名字
cell = row.createCell((short)1);
cell.setCellValue("money");
cell = row.createCell((short)2);
cell.setCellValue("pay_way");
cell = row.createCell((short)3);
cell.setCellValue("order_time");
cell = row.createCell((short)4);
cell.setCellValue("schlep"); int i=1;
for (Orders o:list) {
row = sheet.createRow(i);
cell = row.createCell(0);
cell.setCellValue(o.getTenant_order_no());
cell = row.createCell(1);
cell.setCellValue(o.getMoney_amount());
cell = row.createCell(2);
cell.setCellValue(o.getPay_way());
cell = row.createCell(3);
cell.setCellValue(o.getOrder_time());
cell = row.createCell(4);
cell.setCellValue(o.getSchlep());
i++;
}
//HttpServletResponse 第一个头部必须设置
//第二个可以先不设置 只设置第一个头部运行后 可以确定浏览器导出的是什么文件后缀
//接上面 若有自定义命名需要 可以加上第二行 并带上确定的文件名后缀
response.setHeader("content-Type", "application/vnd.ms-excel");
//下面设置浏览器下载的文件名 包含文件后缀
response.setHeader("Content-Disposition", "attachment;filename="+the_super_id+"-"+new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date())+".xls");
workbook.write(response.getOutputStream());
response.getOutputStream().flush();
System.out.println("Success");
前端
//#dddc 任意可点击的东西 ,#daochu01 需要带给服务器的参数
$("#dddc").on('click', function () {
var url = "http://" + window.location.host + "/ddgl/dddc";
$('<form method="get" action=' + url + '>' +
'<input name="daochu01" type="text" value='+$("#daochu01").val()+'/>' +
'<input name="daochu02" type="text" value='+$("#daochu02").val()+'/>' +
'</form>').appendTo('body').submit().remove();
});
//ajax请求服务器,是不能返回excel表格的(这个不确定),我们需要把请求转换为页面请求,即转换为form表单发送请求,这样就可以把数据导出为表格了
结果
5001263243584372792-2018_05_04 22_50_10.xls

参考:https://www.testwo.com/blog/7800
https://www.cnblogs.com/yansj1997/p/4814213.html
java 导mysql数据为表格给浏览器接收的更多相关文章
- Java调用MySql数据库函数
Java调用MySql数据库函数 /** * 调用mysql的自定义函数 * */ private void test() { logger.info("show task start &q ...
- Spark使用Java读取mysql数据和保存数据到mysql
原文引自:http://blog.csdn.net/fengzhimohan/article/details/78471952 项目应用需要利用Spark读取mysql数据进行数据分析,然后将分析结果 ...
- java连接mysql数据查询数据
package com.cn.peitest.connectDatabase; import java.sql.Connection; import java.sql.DriverManager; i ...
- java操作MySQL数据事务的简单学习
在执行数据更改操作前使用数据库连接对象调用setAutoCommit方法(conn.setAutoCommit(false)),其参数true或false区别: true:sql命令的提交(commi ...
- Java实现Mysql数据导入导出
package com.backup; import java.io.BufferedReader;import java.io.FileInputStream;import java.io.File ...
- java实现mysql数据备份
/** * @param hostIP ip地址,可以是本机也可以是远程 * @param userName 数据库的用户名 * @param password 数据库的密码 * @param sav ...
- 利用PHP实现登录与注册功能以及使用PHP读取mysql数据库——以表格形式显示数据
登录界面 <body><form action="login1.php" method="post"><div>用户名:&l ...
- Java使用Mysql数据库实现批量添加数据
EmployeeDao.java //批处理添加数据 public int saveEmploeeBatch(){ int row = 0; try{ con = DBCon.getConn(); S ...
- 原生PHP和MYSQL练习登陆验证和查询数据到表格
直接上代码吧 <?php header("Content-type: text/html; charset=utf-8"); //数据量链接 $conn=mysqli_con ...
随机推荐
- Centos6.8 编译安装Apache2.4
cetos6.8源码安装apache2.4.29 apache官网:http://httpd.apache.org 具体安装步骤: 1 配置安装apache的基础环境2 下载想要安装的版本源码包3 解 ...
- ubuntu18关闭系统自动更新
ubuntu18.04关闭系统自动更新有两个方法:1.修改配置文件 修改配置文件/etc/apt/apt.conf.d/10periodic#0是关闭,1是开启,将所有值改为0vi etc/apt/a ...
- icomoon:生成字体图标的方法并应用
字体图标任意缩放不会失真,也大大减少请求数量,非常好用. 在线生成工具:https://icomoon.io/app/#/select 在线SVG图库(阿里), 用于导入:http://www.ic ...
- 黄聪:xampp启动后mysql报Error
2013-08-04 13:48:22 760 [ERROR] InnoDB: Attempted to open a previously opened tablespace. Previous t ...
- 对中断interrupt的理解
一.中断 线程的几种状态:新建.就绪.运行.阻塞.死亡.参考:线程的几种状态转换 线程的可运行状态并不代表线程一定在运行(runnable != running ) . 大家都知道:所有现代桌面和服务 ...
- html动态元素点击事件添加
很多时候,页面的元素是后期异步动态添加在页面上.页面点击事件无效. 非动态的元素直接$().click();便可以直接触发点击事件,而动态元素需要事先注册事件. $(document).on('cli ...
- google的protobuf简单介绍
google的protobuf是一种轻便高效的结构化数据存储格式,在通信协议和数据存储等领域中使用比较多.protobuf对于结构中的每个成员,会提供set系列函数和get系列函数. 但是,对于使用来 ...
- Ali流量控制中间件Sentinel
原文链接: https://blog.csdn.net/u012190514/article/details/81383698 Sentinel 是什么 随着微服务的流行,服务和服务之间的稳定性变得越 ...
- django 多线程下载图片
example1: from multiprocessing.dummy import Pool as ThreadPool #多线程 import time import urllib2 urls ...
- [UE4]字体材质
一.准备好一个字体文件,直接拖放到内容浏览器 二.创建一个名为testFontMaterial的UserWidget,添加一个TextBlock到默认的CanvasPanel.Font Family: ...