页面效果
功能概述:
选中要需要导出到excel的列(也可以是获取一些id)然后后台查找数据生成excel,用户点击导出到Excel就下载excle文件
前端页面
multiselect="true" multikey="true" ondbClickRowEvent=""
cusEditUrl="constract/tmc/edit">
cssClass="nui-mainbtn" οnclick="constractGrid.create();"/>
cssClass="nui-mainbtn" οnclick="aaa();"/>
编号: | 标题: | 分类: |
特采单号" align="center">
合同编号" width="100" align="center">
担当" width="100" align="center">
创建时间" width="100" align="center">
function aaa() {
// 1.获取选中行的数据(特菜单id)
var ids = [];
var selecteds = $("tr[aria-selected=true]");
for(var i=0; i
console.log(selecteds[i]);
var tcOrderId = selecteds[i].children[3].getAttribute("title");
console.log("id[" + i + "]=" + tcOrderId);
ids.push(tcOrderId);
};
console.log(ids);
var data = {
"aaa": ids
}
var bbb = $.param(data, true)
console.log('./constract/tmc/export/excel?' + bbb);
// 发送Get请求,下载Java后端接口传输过来的excel文件
window.location = './constract/tmc/export/excel?' + bbb;
}~~~~
1.这个页面使用了前端框架的封装,普通的 也是可以定义click事件
2.window.location = url是可以发送GET请求的
Java控制器
package itsm.isperp.module.controller.constract;
import com.fr.web.core.A.E;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.module.entity.constract.TmcConstract;
import itsm.isperp.module.response.AaaRes;
import itsm.isperp.module.response.ExcelModel;
import itsm.isperp.module.service.constract.TmcConstractService;
import itsm.isperp.module.utils.ExcelUtils;
import org.apache.http.HttpRequest;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
/**
* 控制器
*
* @author lizx
* @date 2020-05-18 16:45
*/
@Controller
@RequestMapping("constract/tmc")
public class TmcConstractController extends BaseController {
@Autowired
protected TmcConstractService tmcConstractService;
@Override
public TmcConstractService getService() {
return this.tmcConstractService;
}
@Override
public String getPageTitle() {
return "TMC合同";
}
@RequestMapping(value = "export/excel", method = RequestMethod.GET)
@ResponseBody
public Map exportExcel(@RequestParam(value="aaa") String ids[],
HttpServletRequest request, HttpServletResponse response) throws NoSuchMethodException,
IllegalAccessException, InvocationTargetException, IOException {
Map map= new HashMap<>();
map.put("statusCode","200");
map.put("message","导出成功!");
String[] idss = request.getParameterValues("aaa");
System.out.println(Arrays.toString(idss));
AaaRes a2 = new AaaRes();
a2.setId("111");
a2.setName("人从众");
AaaRes a1 = new AaaRes();
a1.setId("111");
a1.setName("又双叒叕");
ExcelModel excelModel = new ExcelModel<>();
List ths = new ArrayList();
ths.add("编号");
ths.add("姓名");
List aaaRes = new ArrayList<>();
aaaRes.add(a1);
aaaRes.add(a2);
excelModel.setThs(ths);
excelModel.setTrs(aaaRes);
// TODO 调用Excel生成工具类,返回流数据
Workbook workbook = ExcelUtils.excelStream(excelModel);
String fileName = "aaa"+System.currentTimeMillis()+".xlsx";
setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
workbook.write(os);
os.flush();
os.close();~~~~
return map;
}
// 封装请求数据
public void setResponseHeader(HttpServletResponse response, String fileName) {
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
}
}
ExcelUtil
package utils;
import itsm.isperp.module.response.AaaRes;
import itsm.isperp.module.response.ExcelModel;
import itsm.isperp.module.utils.ExcelUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
/**
* @Description
* @Project isperp-itss
* @Author ZhiYue
* @Date 2020/5/27 13:27
*/
public class ExcelUtilTest {
@Test
public void test() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
AaaRes a2 = new AaaRes();
a2.setId("111");
a2.setName("人从众");
AaaRes a1 = new AaaRes();
a1.setId("111");
a1.setName("又双叒叕");
ExcelModel excelModel = new ExcelModel<>();
List ths = new ArrayList();
ths.add("编号");
ths.add("姓名");
List aaaRes = new ArrayList<>();
aaaRes.add(a1);
aaaRes.add(a2);
excelModel.setThs(ths);
excelModel.setTrs(aaaRes);
Workbook workbook = ExcelUtils.excelStream(excelModel);
}
}
ExcelModel
package itsm.isperp.module.response;
import itsm.isperp.module.base.Model;
import java.util.List;
/**
* @Description
* @Project isperp-itss
* @Author ZhiYue
* @Date 2020/5/26 17:39
*/
public class ExcelModel extends Model {
private Integer columnWidth;
private Integer columnHeight;
private List ths;
private List trs;
public List getTrs() {
return trs;
}
public void setTrs(List trs) {
this.trs = trs;
}
public List getThs() {
return ths;
}
public void setThs(List ths) {
this.ths = ths;
}
public Integer getColumnWidth() {
return columnWidth;
}
public void setColumnWidth(Integer columnWidth) {
this.columnWidth = columnWidth;
}
public Integer getColumnHeight() {
return columnHeight;
}
public void setColumnHeight(Integer columnHeight) {
this.columnHeight = columnHeight;
}
}
AaaRes
package itsm.isperp.module.response;
/**
* @Description 测试
* @Project isperp-itss
* @Author ZhiYue
* @Date 2020/5/26 17:48
*/
public class AaaRes extends ExcelModel{
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
后记: 下班了先这样