今天工作中遇到了excel表格导出的操作,还好有写好的模板,不然我也是焦头烂额,下面记录一下excel表格导出的操作步骤,同时用来给正在学习jfinal的小伙伴一些参考和学习。

  首先我们需要把表格查询出并且显示在页面上,下面是页面跳转时经过的Controller:(这里包含条件查询,所以获取信息比较复杂,全部查询只需要将获取到的pageUtils 传到前台即可,不需要添加多余的查询条件,在这里传给前台的变量名我叫他page)

public void index(){
PageUtils pageUtils = getPageParameter();
String where = " where 1=1 ";
//获取当前时间
String nowDate =DateUtils.getDate();
if(pageUtils.getMap().get("orderNo")!=null&&!pageUtils.getMap().get("orderNo").toString().trim().equals("")){
where = where + " and order_no like '%"+pageUtils.getMap().get("orderNo")+ "%' ";
}
if(pageUtils.getMap().get("customerName")!=null&&!pageUtils.getMap().get("customerName").toString().trim().equals("")){
where = where + " and customer_name like '%"+pageUtils.getMap().get("customerName")+"%'";
} if(pageUtils.getMap().get("shipmentDate")!=null&&!pageUtils.getMap().get("shipmentDate").toString().trim().equals("")){
where = where + " and shipment_date = '"+pageUtils.getMap().get("shipmentDate")+"'";
}
if(pageUtils.getMap().get("isship")!=null&&!pageUtils.getMap().get("isship").toString().trim().equals("")){
if(pageUtils.getMap().get("isship").equals("1")){
where = where + " and Tknum is not null ";
}else if (pageUtils.getMap().get("isship").equals("3")){
where = where + " and ship_type!=5 and arrive_date<'"+nowDate+"' ";
}else{
where = where + " and Tknum is null ";
}
}
if(pageUtils.getMap().get("shipType")!=null&&!pageUtils.getMap().get("shipType").toString().trim().equals("")){
if(pageUtils.getMap().get("shipType").equals("1")){
where = where + " and ship_type =5 ";
}else{
where = where + " and ship_type !=5 ";
}
}
if(pageUtils.getMap().get("ishd")!=null&&!pageUtils.getMap().get("ishd").toString().trim().equals("")){
if(pageUtils.getMap().get("ishd").equals("1")){
where = where + " and img is not null ";
}else{
where = where + " and img is null ";
}
}
where += " order by arrive_date desc ";
pageUtils.initialize(TMSOrderList.dao.paginate(pageUtils.getPageNo(),
pageUtils.getPageSize(), "select * ",
" from TMS_Order_List"+where+ pageUtils.getOrderBySql()));
if(pageUtils.getList().size()==0&&pageUtils.getPageNo()!=1){
pageUtils.setPageNo(1); pageUtils.initialize(TMSOrderList.dao.paginate(pageUtils.getPageNo(),
pageUtils.getPageSize(), "select * ",
" from TMS_Order_List "+where+pageUtils.getOrderBySql()));
} setAttr("page", pageUtils);
setAttr("nowDate", nowDate);
render("reportList.jsp");
}

  在上面我们获取到了表格信息(实体类和model我就不用写了吧),这里传到前台两个变量,和一个需要返回的页面,这里当前时间你们可能不需要,之后就是在页面接收这个值:(这里因为连表查询所以将page这个值拆开了两次,如果不

  需要链表,直接<c:foreach>循环输出就好了,不需要<c:set>)

<input id="btnSubmit" class="btn btn-primary" type="button" value="导出" onclick="exportExcel()"/>
<table id="contentTable" class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th style="text-align: center;"><input id="checkAll" type="checkbox"/>序号</th>
<th style="text-align: center;">发运客户</th>
<th style="text-align: center;">订单号</th>
<th style="text-align: center;">装货日期</th>
<th style="text-align: center;">预计送达日期</th>
<th style="text-align: center;">运输类型</th>
<th style="text-align: center;">装货地址</th>
<th style="text-align: center;">装货联系人</th>
<th style="text-align: center;">装货联系方式</th>
<th style="text-align: center;">物料描述</th>
<th style="text-align: center;">装货数量</th>
<th style="text-align: center;">装货重量(KG)</th>
<th style="text-align: center;">装货体积(M3)</th>
<th style="text-align: center;">包装规格</th>
<th style="text-align: center;">到达省份</th>
<th style="text-align: center;">到达城市</th>
<th style="text-align: center;">收货客户</th>
<th style="text-align: center;">收货地址</th>
<th style="text-align: center;">收货联系人</th>
<th style="text-align: center;">收货联系方式</th>
<th style="text-align: center;">单价</th>
<th style="text-align: center;">应收运费</th>
<th style="text-align: center;">附加费用</th>
<th style="text-align: center;">合计运费</th>
<th style="text-align: center;">承运商</th>
<th style="text-align: center;">承运商联系方式</th>
<th style="text-align: center;">承运车辆</th>
<th style="text-align: center;">驾驶员</th>
<th style="text-align: center;">驾驶员联系方式</th>
<th style="text-align: center;">应付运费</th>
<th style="text-align: center;">预付运费</th>
<th style="text-align: center;">回付运费</th>
<th style="text-align: center;">附加费用</th>
<th style="text-align: center;">附加费单号</th>
<th style="text-align: center;">合同单号</th>
<th style="text-align: center;">备注</th>
</tr>
</thead>
<tbody id="pBody">
<c:forEach items="${page.list}" var="item" varStatus="index">
<c:set value="${item.client}" var="client"></c:set>
<c:set value="${item.vttkManual}" var="vttkManual"></c:set>
<tr>
<td>
<input id="checkbox${item.id}" name="checkbox" type="checkbox" value="${item.id}" onchange="selected(this.value)"/>
${index.index+1}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.customername}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.orderno}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.shipmentdate}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.arrivedate}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.shiptype}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.shipmentaddress}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.shipmentlinkman}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.shipmenttelephone}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.materialdescription}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.shipmentqty}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.shipmentweight}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.shipmentbulk}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.packagesize}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.province}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.city}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${client.clientName}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${client.clientAddress}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${client.linkman}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${client.telphone}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.unitprice}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.receivablecharge}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.additionalcharge}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${item.totalcharge}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.name1}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"></td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.signi}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"></td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"></td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.receivablefreight}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.advancefreight}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.backfreight}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.additionalfreight}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.additionalnumber}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.contract}</td>
<td style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${vttkManual.bezei}</td>
</tr>
</c:forEach>
</tbody>
</table>

  之后就是重点了,excel表格导出,我们可以看到这个页面的最顶端有个导出按钮,导出按钮监听了点击事件onclick="exportExcel()",之后我们可以在页面的上方写JavaScript写一个方法:

function exportExcel (){
$("#searchForm").attr("action","${ctx}/tms/report/exportExcel").submit();
}

  这里写的这个方法是跳转到tms下面report这个controller下的exportExcel方法,这个方法是这样写的:

/**
* 导出excel
*/
public void exportExcel(){
// List<VttkManual> list=VttkManual.dao.find("select * from L2C_VTTK");
List<TMSOrderList> list = TMSOrderList.dao.find("select * from TMS_Order_List");
//Excel生成工具
ExcelExport excelExport = new ExcelExport();
String[] listName = {"发运客户"," 订单编号 "," 装货日期","预计送达日期 ","运输类型 ","装货地址","装货联系人",
"装货联系方式","物料描述 ","装货数量","装货重量 (KG)","装货体积(m3) ","包装规格",
"到达省份","到达城市 ","收货客户","收货地址","收货联系人 ","收货联系方式",
"单价","应收运费","附加费用","合计运费","承运商","承运商联系方式 ","承运车辆",
"驾驶员 ","驾驶员联系方式 ","应付运费","预付运费","回付运费 ","附加费用","附加费单号 ",
"合同单号","备注"};
Map<Integer, Object> excelMap = excelExport.createTable("列表", listName); HSSFCellStyle style = ((HSSFWorkbook) excelMap.get(3)).createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFRow row = null;
HSSFCell cells = null;
for (int i = 0; i < list.size(); i++){
row = ((HSSFSheet)excelMap.get(2)).createRow(i + 1);
// VttkManual vm = list.get(i);
// TMSOrderList order=TMSOrderList.dao.findFirst("select * from TMS_Order_List " +
// "where order_no='"+vm.getVbeln()+"'");
TMSOrderList order = list.get(i);
VttkManual vm = VttkManual.dao.findFirst("select * from L2C_VTTK where Vbeln='"+order.getOrderno()+"'"); cells = row.createCell(0);
cells.setCellValue(order.getCustomername());
cells.setCellStyle(style); cells = row.createCell(1);
cells.setCellValue(order.getOrderno());
cells.setCellStyle(style); cells = row.createCell(2);
cells.setCellValue(order.getShipmentdate());
cells.setCellStyle(style); cells = row.createCell(3);
cells.setCellValue(order.getArrivedate());
cells.setCellStyle(style); cells = row.createCell(4);
cells.setCellValue(order.getShipmenttype());
cells.setCellStyle(style); cells = row.createCell(5);
cells.setCellValue(order.getShipmentaddress());
cells.setCellStyle(style); cells = row.createCell(6);
cells.setCellValue(order.getShipmentlinkman());
cells.setCellStyle(style); cells = row.createCell(7);
cells.setCellValue(order.getShipmenttelephone());
cells.setCellStyle(style); cells = row.createCell(8);
cells.setCellValue(order.getMaterialdescription());
cells.setCellStyle(style); cells = row.createCell(9);
cells.setCellValue(order.getShipmentqty()==null?"":order.getShipmentqty().toString());
cells.setCellStyle(style); cells = row.createCell(10);
cells.setCellValue(order.getShipmentweight()==null?"":order.getShipmentweight().toString());
cells.setCellStyle(style); cells = row.createCell(11);
cells.setCellValue(order.getShipmentbulk()==null?"":order.getShipmentbulk().toString());
cells.setCellStyle(style); cells = row.createCell(12);
cells.setCellValue(order.getPackagesize());
cells.setCellStyle(style); cells = row.createCell(13);
cells.setCellValue(order.getProvince());
cells.setCellStyle(style); cells = row.createCell(14);
cells.setCellValue(order.getCity());
cells.setCellStyle(style); if(order.getClient()!=null){
cells = row.createCell(15);
cells.setCellValue(order.getClient().getClientName());
cells.setCellStyle(style); cells = row.createCell(16);
cells.setCellValue(order.getClient().getClientAddress());
cells.setCellStyle(style); cells = row.createCell(17);
cells.setCellValue(order.getClient().getLinkman());
cells.setCellStyle(style); cells = row.createCell(18);
cells.setCellValue(order.getClient().getTelphone());
cells.setCellStyle(style);
} cells = row.createCell(19);
cells.setCellValue(order.getUnitprice()==null?"":order.getUnitprice().toString());
// cells.setCellValue("");
cells.setCellStyle(style); cells = row.createCell(20);
cells.setCellValue(order.getReceivablecharge()==null?"":order.getReceivablecharge().toString());
// cells.setCellValue("");
cells.setCellStyle(style); cells = row.createCell(21);
cells.setCellValue(order.getAdditionalcharge()==null?"":order.getAdditionalcharge().toString());
// cells.setCellValue("");
cells.setCellStyle(style); cells = row.createCell(22);
cells.setCellValue(order.getTotalcharge()==null?"":order.getTotalcharge().toString());
// cells.setCellValue("");
cells.setCellStyle(style); cells = row.createCell(23);
cells.setCellValue(vm.getName1());
cells.setCellStyle(style); cells = row.createCell(24);
cells.setCellValue(vm.getTelephone1());
cells.setCellStyle(style); cells = row.createCell(25);
cells.setCellValue(vm.getSigni());
cells.setCellStyle(style); cells = row.createCell(26);
cells.setCellValue(vm.getDriver());
cells.setCellStyle(style); cells = row.createCell(27);
cells.setCellValue(vm.getTelephone2());
cells.setCellStyle(style); cells = row.createCell(28);
cells.setCellValue(vm.getReceivablefreight());
cells.setCellStyle(style); cells = row.createCell(29);
cells.setCellValue(vm.getAdvancefreight());
cells.setCellStyle(style); cells = row.createCell(30);
cells.setCellValue(vm.getBackfreight());
cells.setCellStyle(style); cells = row.createCell(31);
cells.setCellValue(vm.getAdditionalfreight());
cells.setCellStyle(style); cells = row.createCell(32);
cells.setCellValue(vm.getAdditionalnumber());
cells.setCellStyle(style); cells = row.createCell(33);
cells.setCellValue(vm.getContract());
cells.setCellStyle(style); cells = row.createCell(34);
cells.setCellValue(vm.getBezei());
cells.setCellStyle(style);
} // 第六步,将文件存到指定位置
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
String code = sdf.format(new Date());
String fileName = "订单全部明细"+code+".xls";
String path = JFinal.me().getConstants().getFileRenderPath();//获取项目配置下载路径
path = path.replace("\\","/");
FileOutputStream fout = new FileOutputStream(path+"/"+fileName);
((HSSFWorkbook)excelMap.get(3)).write(fout);
fout.close();
addMessageSuccess("导出成功"); renderFile(fileName); }catch (Exception e){
e.printStackTrace();
addMessageError("导出失败");
redirect("/tms/report");
}
}

  之后大概就都懂了把,最上面String[] listName 是每列的字段名,下面

        cells = row.createCell(0);代表的是当前列数;

        cells.setCellValue(order.getCustomername());代表的是当前列插入的内容;

        cells.setCellStyle(style);代表当前单元格内的样式

下面配置好路径就可以道出了,导出成功之后页面会提示你保存的路径,保存之后就可以查看了,是不是很简单呢

jfinal中excel表格导出的更多相关文章

  1. 关于Java中excel表格导出的总结(Java程序导出模板和Java根据模板导出表格两种实现方式)

    导出excel通用模板(程序定义模板导出) 转载原文:https://www.jianshu.com/p/5c7b359a159c 如下代码,本方法主要用于程序定义模板格式,并导出文件.该方法将定义和 ...

  2. 利用Apache POI 实现简单的Excel表格导出

    1.利用POI API实现简单的Excel表格导出 首先假设一个学生实体类: package com.sun.poi.domain; import java.io.Serializable; impo ...

  3. excel表格导出之后身份证号列变成了科学计数法

    excel表格导出之后身份证号列变成了科学计数法 解决:写sql查询出所有数据,并在身份证列添加字符,然后导出,将要复制的excel表格设置单元格格式问文本类型,然后复制粘贴,再把加入的字符删除,搞定 ...

  4. vue中excel导入导出组件

    vue中导入导出excel,并根据后台返回类型进行判断,导入到数据库中 功能:实现js导入导出excel,并且对导入的excel进行展示,当excel标题名称和数据库的名称标题匹配时,则对应列导入的数 ...

  5. JeeSite中Excel导入导出

    在各种管理系统中,数据的导入导出是经常用到的功能,通常导入导出以Excel.CSV格式居多.如果是学习的过程中,最好是自己实现数据导入与导出的功能,然而在项目中,还是调用现成的功能比较好.近期一直使用 ...

  6. python中excel表格的读写

    #!usr/bin/env python #-*- coding:utf-8 -*- import xlrd import xlwt from xlutils.copy import copy imp ...

  7. 在react项目中实现表格导出为Excel

    需求背景 数据表格有时需要增加导出Excel功能,大多数情况下都是后端出下载接口,前端去调用. 对于数据量少的数据,可以通过前端技术实现,减少后端工作. 实现方式 使用插件--xlsx 根据自己项目情 ...

  8. java实现excel表格导出数据

    /** * 导出清单 eb中 firstRow(EntityBean) 列表第一行数据,键值对(不包含序号)例:("name","姓名") * data(Ent ...

  9. 由Excel表格导出Latex代码

    Latex提供了不少绘制表格的宏包(参见:http://tug.org/pracjourn/2007-1/mori/),但在latex里画表并不直观,特别是在表格比较大的时候,有时候也需要先用Exce ...

随机推荐

  1. 《http权威指南》读书笔记11

    概述 最近对http很感兴趣,于是开始看<http权威指南>.别人都说这本书有点老了,而且内容太多.我个人觉得这本书写的太好了,非常长知识,让你知道关于http的很多概念,不仅告诉你怎么做 ...

  2. [Swift]Xcode标记:MARK、TODO、FIXME

    1. MARK MARK一定要大写.其又叫做代码标注,我们可以使用它来添加一些说明文字.同时可以选择其上方是否需要显示分割线. // MARK: - 说明文字,带分割线 // MARK: 说明文字,不 ...

  3. java中根据key获取resource下properties资源文件中对应的参数

    properties资源文件是放在resource目录下的: 新建工具类: package com.demo.utils; import java.io.InputStream; import jav ...

  4. golang实现参数可变的技巧

    Go 使用默认参数的技巧 Functional Options Pattern in Go golang中没有函数默认参数的设计,因此需要些特别的技巧来实现. 假如我们需要订购一批电脑,其中电脑配置c ...

  5. mysql 开发基础系列4 字符数据类型

    字符串类型 1.1 CHAR 和VARCHAR 类型 CHAR 列的长度固定为创建表时声明的长度,VARCHAR 列中的值为可变长字符串.在检索的时候,CHAR 列删除了尾部的空格,而VARCHAR ...

  6. 改善JAVA代码01:考虑静态工厂方法代替构造器

    前言 系列文章:[传送门]   每次开始新的一本书,我都会很开心.新书新心情. 正文 静态工厂方法代替构造器 说起这个,好多可以念叨的.做了一年多的项目,慢慢也有感触. 说起构造器 大家很明白,构造器 ...

  7. spring-boot(八) springboot整合shiro-登录认证和权限管理

    学习文章:springboot(十四):springboot整合shiro-登录认证和权限管理 Apache Shiro What is Apache Shiro? Apache Shiro是一个功能 ...

  8. 动态代理实现AOP

    代理 代理顾名思义:代为处理.不是对目标对象的直接操作,而是通过代理对目标对象进行包装,此时可以在目标对象的基础上添加额外的操作以满足业务需求.图示 分类:动态代理.静态代理. 代理三要素:共同接口. ...

  9. 应用负载均衡之LVS(三):ipvsadm命令

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  10. Go基础系列:为select设置超时时间

    Go channel系列: channel入门 为select设置超时时间 nil channel用法示例 双层channel用法示例 指定goroutine的执行顺序 After() 谁也无法保证某 ...