function ExportGHPPCS(strXlsName, exportGrid) {
var f = $('<form action="${ctx}/common/exportXlsStaffCodeQuery" method="post" id="fm1"></form>');
//var i = $('<input type="hidden" id="txtContent" name="txtContent" />');
var l = $('<input type="hidden" id="txtName" name="txtName"/>');
var j = $('<input type="hidden" id="startTimeGJ" name="startTimeGJ" />');//把時間傳到後臺,在後臺生成表格
var k = $('<input type="hidden" id="endTimeGj" name="endTimeGJ"/>');
/* var columns = exportGrid.datagrid("options").columns;
i.val(columns);//傳遞列到後臺,在後臺生成表
i.appendTo(f); */
var startTime = $('#statDateStart4').datebox('getValue');
j.val(startTime);
j.appendTo(f);
var endTime = $('#statDateEnd4').datebox('getValue');
k.val(endTime);
k.appendTo(f);
l.val(strXlsName+"-"+startTime+"到"+endTime);
l.appendTo(f);
f.appendTo(document.body).submit();
document.body.removeChild(f);
}css
以上前端js生成form html
/**
* 將報表導出爲excel
* @param request
* @param response
*/
@SuppressWarnings("all")
@RequestMapping(value = "exportXlsStaffCodeQuery")
public void exportXlsStaffCodeQuery(HttpServletRequest request,
HttpServletResponse response) {
//step.1 設置,獲取配置信息
String codedFileName = request.getParameter("txtName");
if(StringUtils.isEmpty(codedFileName)){
codedFileName="導出.xls";
}else{
codedFileName+=".xls";
}
String sheetName="導出信息";
//String txtContent=request.getParameter("txtContent");
String startTime=request.getParameter("startTimeGJ");
String endTime=request.getParameter("endTimeGJ");
UserVo user=SecurityUtils.getLoginUser();
List<Map> userPersonInfo = userStub.getPersonalInfo(user.getUserId());
String staffId=(String) userPersonInfo.get(0).get("username");
//String staffId = String.valueOf(user.getUserId());
String isAdmin = (String) userPersonInfo.get(0).get("idadmin");
if("Y".equals(isAdmin)){
staffId=null;
}
//格式化時間
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdfOld = new SimpleDateFormat("yyyy-MM-dd");
try {
if(StringUtils.isNotEmpty(startTime)){
startTime = sdf.format(sdfOld.parse(startTime));
}
if(StringUtils.isNotEmpty(endTime)){
endTime = sdf.format(sdfOld.parse(endTime));
}
} catch (ParseException e) {
e.printStackTrace();
}
/*Date d1 = new Date();
System.out.println("查找數據前"+sdf.format(d1));*/
List<Map> rows = recordStub.getStaffCodeQuery(startTime, endTime,-1,-1);
/*Date d2 = new Date();
System.out.println("查找數據後"+sdf.format(d2));*/
StringBuilder sb = new StringBuilder();
sb.append( "<table cellspacing=\"0\" class=\"pb\">\n<tr>\n");
sb.append( "<th width='100px'>CSP區域中心</th><th width='130px'>CSP臺室</th><th width='160px'>CSP班組</th><th width='80px'>CSP姓名</th>"
+ "<th width='80px'>CSP工號</th><th width='90px'>專業線</th>\n</tr>\n<tr>");
for(Map m:rows){
sb.append( "\n<td>"+m.get("csp_center_name")+"</td><td>"+m.get("csp_station_name")+"</td><td>"+m.get("csp_group_name")+"</td>"
+ "<td>"+m.get("csp_staff_name")+"</td><td>"+m.get("portal_staff_id")+"</td><td>"+m.get("specialty_line")+"</td>"
+"\n</tr>");
}
sb.append("\n</table>");
/*Date d3 = new Date();
System.out.println("拼接字符後"+sdf.format(d3));*/
/* for(Map m:rows){
}*/
//tableString += "\n</table>";
String tableString = sb.toString();
String txtContent = tableString;
response.setContentType("Application/ms-excel");
PrintWriter out = null;
try {
//step.4 根據瀏覽器進行轉碼,使其支持中文文件名
if (isIE(request)) {
codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF-8");
response.setHeader("content-disposition", "attachment;filename="
+ codedFileName);
/*response.setHeader("content-disposition", "attachment;FileFormat="
+ "-4143");*/
} else {
codedFileName = new String(codedFileName.getBytes("UTF-8"),
"ISO-8859-1");
response.setHeader("content-disposition", "attachment;filename="
+ codedFileName);
}
/*response.setHeader("content-disposition", "attachment;filename="
+ codedFileName);*/
/*response.setHeader("content-disposition", "attachment;FileFormat="
+ "-4143");*/
out = response.getWriter();
out.write("<html>\n<head>\n");
out.write("<meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=utf-8\"/>" );
out.write("<style type=\"text/css\">\n.pb{font-size:13px;border-collapse:collapse;} "+
"\n.pb th{font-weight:bold;text-align:center;border:0.5pt solid windowtext;padding:2px;} " +
"\n.pb td{border:0.5pt solid windowtext;padding:2px;}\n</style>\n</head>\n");
out.write("<body>\n" + txtContent + "\n</body>\n</html>");
out.flush();
} catch (UnsupportedEncodingException e1) {
} catch (Exception e) {
} finally {
if(out!=null){
out.close();
}
}
}前端
protected boolean isIE(HttpServletRequest request) {
return (request.getHeader("USER-AGENT").toLowerCase().indexOf("msie") > 0 || request
.getHeader("USER-AGENT").toLowerCase().indexOf("rv:11.0") > 0) ? true
: false;
}java
以上後臺生成excel導出瀏覽器