1、 页面上加按钮与js
<t:dgToolBar title="导出托运单" icon="icon-putout"
url="carRouteController.do?ExportHjyTyd" funname="ExportHjyTyd"></t:dgToolBar>
//导出排车单
function ExportHjyTyd(title,url,gname) {
gridname=gname;
// var ids = [];
//var rows = $("#"+gname).datagrid('getSelections');
var id = $('#fid').val();
JeecgExcelExport("carRouteController.do?ExportHjyTyd&id="+id , "carRouteList");
}
2、后台写导出方法
/**
* 导出 托运单
*/
@RequestMapping(params = "ExportHjyTyd")
@ResponseBody
public void ExportHjyTyd(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response,
OutputStream output) throws UnsupportedEncodingException {
Workbook tempWorkBook = null;
String id = request.getParameter("id");//业务编号
String wheresql = " ";
* 导出 托运单
*/
@RequestMapping(params = "ExportHjyTyd")
@ResponseBody
public void ExportHjyTyd(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response,
OutputStream output) throws UnsupportedEncodingException {
Workbook tempWorkBook = null;
String id = request.getParameter("id");//业务编号
String wheresql = " ";
if(id!=null && !id.equals("")) {
wheresql += " and a.id='" + id + "'";
}
String sql = " select a.* ,b.*,sysdate() sysdate "
+ " from ldc_order a,car_route b"
+ " where a.order_code = b.cr_orderno "
+ wheresql
+ " order by b.create_date desc ";
wheresql += " and a.id='" + id + "'";
}
String sql = " select a.* ,b.*,sysdate() sysdate "
+ " from ldc_order a,car_route b"
+ " where a.order_code = b.cr_orderno "
+ wheresql
+ " order by b.create_date desc ";
String lujing = request.getSession().getServletContext().getRealPath("/");
String lujing1 = lujing + "export\\template\\exporthjytyd.xlsx";
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
InputStream in;
try {
in = new FileInputStream(new File(lujing1));
XSSFWorkbook work = null;
work = new XSSFWorkbook(in);
this.ExportHjyTydOut(request, response, work, maps);
} catch (Exception e) {
String lujing1 = lujing + "export\\template\\exporthjytyd.xlsx";
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
InputStream in;
try {
in = new FileInputStream(new File(lujing1));
XSSFWorkbook work = null;
work = new XSSFWorkbook(in);
this.ExportHjyTydOut(request, response, work, maps);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 导出托运单动作
*/
public void ExportHjyTydOut(HttpServletRequest request, HttpServletResponse response, XSSFWorkbook work, List<Map<String, Object>> list) {
XSSFSheet sheetAt = work.getSheetAt(0);
XSSFRow row1 = sheetAt.getRow(1);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString ="";
Map<String, Object> map = new HashMap<>();
map = list.get(0);
/*
* 从第三行开始赋值
*/
XSSFRow row3 = sheetAt.getRow(2);
//发货日期
if(map.get("create_date") !=null) {
Date date = (Date) map.get("create_date");
dateString = formatter.format(date);
row3.getCell(2).setCellValue(dateString);
}else {
row3.getCell(2).setCellValue("");
}
//导出日期
String qq = formatter.format(map.get("sysdate"));
row3.getCell(7).setCellValue(formatter.format(map.get("sysdate")));
//业务编号
if(map.get("order_code") !=null) {
row3.getCell(12).setCellValue(String.valueOf(map.get("order_code")));
}else {
row3.getCell(12).setCellValue("");
}
/*
* 从第四行开始赋值
*/
XSSFRow row4 = sheetAt.getRow(3);
//提货联系人
if(map.get("cr_Fromperson") !=null) {
row4.getCell(2).setCellValue(String.valueOf(map.get("cr_Fromperson")));
}else {
row4.getCell(2).setCellValue("");
}
//提货联系人电话
if(map.get("cr_Fromperson") !=null) {
row4.getCell(5).setCellValue(String.valueOf(map.get("cr_Fromperson")));
}else {
row4.getCell(5).setCellValue("");
}
//提货地址
if(map.get("cr_Fromplace_Value") !=null) {
row4.getCell(8).setCellValue(String.valueOf(map.get("cr_Fromplace_Value")));
}else {
row4.getCell(8).setCellValue("");
}
/*
* 从第5行开始赋值
*/
XSSFRow row5 = sheetAt.getRow(4);
//送货联系人
if(map.get("cr_Toperson") !=null) {
row5.getCell(2).setCellValue(String.valueOf(map.get("cr_Toperson")));
}else {
row5.getCell(2).setCellValue("");
}
//送货联系人电话
if(map.get("cr_Topersontel") !=null) {
row5.getCell(5).setCellValue(String.valueOf(map.get("cr_Topersontel")));
}else {
row5.getCell(5).setCellValue("");
}
//送货地址
if(map.get("cr_ToplaceValue") !=null) {
row5.getCell(8).setCellValue(String.valueOf(map.get("cr_Toplace_Value")));
}else {
row5.getCell(8).setCellValue("");
}
/*
* 从第7行开始赋值
*/
XSSFRow row7 = sheetAt.getRow(6);
//货物
if(map.get("order_Goods") !=null) {
row7.getCell(1).setCellValue(String.valueOf(map.get("order_Goods")));
}else {
row7.getCell(1).setCellValue("");
}
//件数
if(map.get("order_Number") !=null) {
row7.getCell(2).setCellValue(String.valueOf(map.get("order_Number")));
}else {
row7.getCell(2).setCellValue("");
}
//包装
if(map.get("order_Bz") !=null && !"".equals(map.get("order_Bz"))) {
String sql = " select typename from t_s_type "
+ " where typegroupid=(select id from t_s_typegroup where typegroupcode='orderbz')"
+ " and typecode = '"+(String)map.get("order_Bz")+"'";
row5.getCell(3).setCellValue(String.valueOf(map.get("order_Bz")));
}else {
row5.getCell(3).setCellValue("");
}
//重量(KG)
if(map.get("order_Gross_Weight") !=null) {
row5.getCell(4).setCellValue(String.valueOf(map.get("order_Gross_Weight")));
}else {
row5.getCell(4).setCellValue("");
}
//尺寸
if(map.get("order_Size") !=null) {
row5.getCell(5).setCellValue(String.valueOf(map.get("order_Size")));
}else {
row5.getCell(5).setCellValue("");
}
//前台反应
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel"); //保证不乱码
try
{
Date date=new Date();
SimpleDateFormat format=new SimpleDateFormat("MMddHHmmss");
String time="bb"+format.format(date)+".xlsx";
response.setHeader("Content-Disposition","attachment;" + " filename=" + new String(time.getBytes("utf-8"), "ISO-8859-1"));
}
catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
try
{
ByteArrayOutputStream oss =new ByteArrayOutputStream();
OutputStream os = response.getOutputStream();
work.write(oss);
byte temp[] = oss.toByteArray();
ByteArrayInputStream in1 = new ByteArrayInputStream(temp);
int n = 0;
while ((n = in1.read(temp)) >0) {
os.write(temp, 0, n);
}
os.flush();
os.close();
} catch(Exception e){
e.printStackTrace();
}
}