java Excel导出功能之 固定列表格

实现列数不变的数据导出

DAO




	

	
		insert into goout_process(businessId, 
										submitTime, 
										timeSnippet, 
										destination,
										vehicle,
										reason,
										deptId,
										userId,
										status,
										title,
										result,
										finishTime
										)
		values(#{businessId}, 
				#{submitTime}, 
				#{timeSnippet}, 
				#{destination}, 
				#{vehicle}, 
				#{reason}, 
				#{deptId}, 
				#{userId}, 
				#{status},
				#{title},
				#{result},
				#{finishTime})
		
	
	
	
	
	


 	
	
		update goout_process
		set status = #{status}, result = #{result}
		where businessId = #{businessId}
	
	
	
	
	

package com.vooledingtalk.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.vooledingtalk.pojo.GoOutProcessPOJO;


@Repository
public interface GoOutProcessMapper {
	/**
	 * 添加外出审批记录
	 * @param DingCheckinRecord
	 * @return
	 */
	int insertGooutProcess(GoOutProcessPOJO goOutProcessPOJO);
	
	/**
	 * 根据businessId判断记录是否存在 
	 * @param dingUser
	 * @return
	 */
	int isExist(GoOutProcessPOJO goOutProcessPOJO);
	
	/**
	 *  根据businessId status 和 result 判断记录是否有状态更新
	 * @param goOutProcessPOJO
	 * @return
	 */
	int hasStatusResultChange(GoOutProcessPOJO goOutProcessPOJO);
	
//	List selectAll();
	
	/*
	 * 
	 * 更新已有记录的 status 和 result 字段
	 */
	int updateRecodeDate(GoOutProcessPOJO goOutProcessPOJO);
	
	List> selectShowData(HashMap map);
	
	int countShowData(HashMap map);
	
	List selectExportData(Map map);
}

Service

package com.vooledingtalk.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.vooledingtalk.pojo.GoOutProcessPOJO;

public interface GoOutProcessService {

	List> getTableData(HashMap map);
	
	int countTableData(HashMap map);
	List selectExportData(Map map);
}
package com.vooledingtalk.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vooledingtalk.dao.GoOutProcessMapper;
import com.vooledingtalk.service.GoOutProcessService;

@Service
public class GoOutProcessServiceImpl implements GoOutProcessService {
	@Autowired
	private GoOutProcessMapper goOutProcessMapper;
	
	
	public List> getTableData(HashMap map) {
		return goOutProcessMapper.selectShowData(map);
	}


	public int countTableData(HashMap map) {		
		return goOutProcessMapper.countShowData(map);
	}


	public List selectExportData(Map map) {
		// TODO Auto-generated method stub
		return goOutProcessMapper.selectExportData(map);
	}

}

Controller

package com.vooledingtalk.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vooledingtalk.common.Page;
import com.vooledingtalk.pojo.GoOutProcessPOJO;
import com.vooledingtalk.service.GoOutProcessService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


@Controller
public class GoOutProcessController {
	@Autowired
	private GoOutProcessService goOutProcessService;
	
	@RequestMapping("/goout")
	public String toGoOutPrcess(){
		return "goout";
	}
	
	@RequestMapping("/goout/list")
	@ResponseBody
	public HashMap showTableData(@RequestParam(value="deptId", required=false,defaultValue="")String deptId,
												@RequestParam(value="userIds", required=false,defaultValue="")String userIds,
												@RequestParam(value="processStatus", required=false,defaultValue="")String processStatus,
												@RequestParam(value="processResult",required=false,defaultValue="")String processResult,
												@RequestParam(value="startDate", required=false, defaultValue="")String startDate,
												@RequestParam(value="endDate", required=false, defaultValue="")String endDate,
												@RequestParam(value="page", required=false)int page,
												@RequestParam(value="rows", required=false)int rows) {

		Page dataPage = new Page(page, rows);
		HashMap map = new HashMap();
		map.put("deptId", deptId);
		map.put("userId", userIds);
		map.put("processStatus", processStatus);
		map.put("processResult", processResult);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		map.put("firstPage", dataPage.getFirstPage());
		map.put("rows", rows);
		
		int total = goOutProcessService.countTableData(map);
		System.out.println("--total---" + total);

		List> data = goOutProcessService.getTableData(map);
		HashMap resultMap = new HashMap();
		resultMap.put("rows", data);
		resultMap.put("total", total);
		
		return resultMap;
	}
	
	//导出数据
	@RequestMapping(value = "/exportgoout")
	public String exportTaskCountReportData(HttpServletRequest request) throws Exception {
		String conditionParams = java.net.URLDecoder.decode(request.getParameter("exportParams"), "UTF-8");
		Map params = new HashMap();
		JSONArray jsonArray = JSONArray.fromObject(conditionParams);
		for (int i = 0; i < jsonArray.size(); i++) {
			try {
				JSONObject jsonObject = (JSONObject) jsonArray.get(i);
				params.put(jsonObject.get("name"), jsonObject.get("value").toString().equals("[]") ? "" : jsonObject.get("value"));
			} catch (Exception e) {
				break;
			}
		}
		List list = goOutProcessService.selectExportData(params);
	    request.setAttribute("data", list);
		return "export_goout";
	}
}


前端

goout.jsp

<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"

pageEncoding="utf-8"%> 外出审批报表 <% request.setAttribute("checkin", "/vooleding/checkin"); request.setAttribute("goout", "/vooleding/goout"); request.setAttribute("supplementrecord", "/vooleding/supplementrecord"); %>

审批状态 审批结果
部门 姓名
查询开始时间 查询结束时间

goout.js

var queryParams = {};
var exportParams = [];
$(document).ready(function(){

	//初始化审批状态和审批结果
	$("#processResult").combobox({
		url:'json/processResult.json',
	    valueField:'id',
	    textField:'text'
	});
	$('#processStatus').combobox({
	    url:'json/processStatus.json',
	    valueField:'id',
	    textField:'text',
	    multiple:true//设置是否可多选
	});
	//初始化部门list
	//初始部门列表
		$("#deptId").combotree ({
			url:path+"/record/deptlist?deptid=0",  
			onBeforeExpand:function(node) {
			      $("#deptId").combotree("tree").tree("options").url = path+"/record/deptlist?deptid=" + node.id;
			},
			onSelect:function(node){
				var url = path + '/userlist?deptId=' + node.id;
				$("#userId").combobox('reload', url);
			}
		 }); 
	//初始化人员list
	$("#userId").combobox({
		url:path + '/userlist?deptId=' + $("#deptId").combotree("getValue"),
		valueField:'id',
	    textField:'text',
	    multiple:true//设置是否可多选
	});
	
	//查询按钮的单机事件
	$("#query").on('click',function(t){
		queryParams = {processStatus:$("#processStatus").combobox('getValues'),
				processResult:$("#processResult").combobox('getValue'),
				deptId:$("#deptId").combotree('getValue'),
				userIds:$("#userId").combobox('getValues'),
				startDate:$("#startDate").datebox('getValue'),
				endDate:$("#endDate").datebox('getValue')};
		$("#dataTable").datagrid('reload', queryParams);
		
		exportParams = [{"name":"processStatus","value":$("#processStatus").combobox('getValues')},
			{"name":"processResult","value":$("#processResult").combobox('getValue')},
			{"name":"deptId","value":$("#deptId").combotree('getValue')},
			{"name":"userIds","value":$("#userId").combobox('getValues')},
			{"name":"startDate","value":$("#startDate").datebox('getValue')},
			{"name":"endDate","value":$("#endDate").datebox('getValue')}];
	});
	
	$("#reset").on('click',function(t){
		$("#processStatus").combobox('clear');
		$("#processResult").combobox('clear');
		$("#userId").combobox('clear');
		$("#deptId").combotree('clear');
	//	console.log($("#startDate").datebox('getValue'));
		$("#startDate").datebox('clear');
		$("#endDate").datebox('clear');
		exportParams = [];
	});
	
	var frozenColumns = [{field:'userName',title:'姓名'},
						{field:'userId',title:'员工ID'},
						{field:'deptName',title:'部门名称'}];
	var cols = [{field:'timeSnippet',title:'外出时间段'},
				{field:'destination',title:'目的地'},
				{field:'reason',title:'外出原因'},
				{field:'processStatus',title:'审批状态'},
				{field:'processResult',title:'审批结果'}];
	//初始化数据表
	$("#dataTable").datagrid({
		url: path + "/goout/list",
		method:"post",
		pageSize:8,
		pageList: [8,16],
		pagination:true,
		queryParams: queryParams,
		title:"外出审批列表",
		columns:[cols],
		frozenColumns:[frozenColumns]
	});
	
});

function exportGooutData() {
    var c1 = JSON.stringify(exportParams);
    c1 = encodeURI(encodeURI(c1));
//	location.href = 'exportOANoteRateData?conditionParams=' + c +'&'+ 'menuParam=' + c1 ;
	location.href = 'exportgoout?exportParams=' + c1;
}
export_goout.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.text.DecimalFormat"%>
<%@ page import="java.util.*"%>
<%@ page isELIgnored="false"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<%
	response.reset();
	//response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
	response.setContentType(" application/vnd.ms-excel");
	//独立打开excel软件   
	String s = "外出审批统计报表"+"-"+new Date().toLocaleString()+".xls";
	String fileName  = new String(s.getBytes("GB2312"), "ISO_8859_1");
	response.setHeader("Content-Disposition", "attachment;filename=\""+fileName);
	//response.setHeader("Content_Length","4");
	response.setCharacterEncoding("UTF-8");
%>






	
			
姓名 员工ID 部门名称 外出时间段 目的地 外出原因 审批状态 审批结果
${p.userName} ${p.userId} ${p.deptName} ${p.timeSnippet} ${p.destination} ${p.reason} ${p.processStatus} ${p.processResult}

总结:

你可能感兴趣的:(前后端实践总结)