java操作mysql导表_java 导mysql数据为表格给浏览器接收

jar 包准备

org.apache.poi

poi

3.17

后端

@Overridepublic List selectList(Mapparams) {

SqlSession sqlSession= sqlSessionFactory.openSession(true);

List list = sqlSession.selectList("com.xwhbank.cn.config.mybatis.namespace.ordersMapper.selectList", params);returnlist;

}

select * from orders where the_super_id = #{the_super_id} and state = '2' and order_time>= #{daochu01} and order_time <= #{daochu02};

List list =ordersService.selectList(params);

Workbook workbook= newHSSFWorkbook();

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";

$('

' +

'' +

'' +

'

').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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值