Action的method写法:(SmartUpload)用法:http://www.codeweblog.com/smartupload-file-upload/
function submitForm(type){
if(type==2){
$("#type").val(2);
}else{
$('#p_curPage').val(1);
$("#type").val(0);
}
$('#form1').submit();
$("#type").val(0);//解决点击下一页触发导出excel
}
/*判断是否点击导出excel*/
if (model.getType() != null && model.getType() != 0) {
map.put("type", 2);
} else {
map.put("type", 0);
}
accGoodsList = opensqlmanage.selectForListByMap_drug(map,"t_leader_stay_money.selectAccountGoodsList");
/*导出excel*/
if (model.getType() != null && model.getType() != 0) {
String path = getRequest().getSession().getServletContext().getRealPath("");
path = path + "/excel_temp/" + UtilDate.getDate()
+ System.currentTimeMillis() + ".xls";
List lists = orderExport();
String s[][] = getOrderArray(accGoodsList);
ExcelUtil.createExcel(path, lists, s);
getRequest().setAttribute("xlsname", path);
url = "/WEB-INF/pages/down/down.jsp"; //见附件
return "xls";
} else {//普通查询
return "getGoodsList";
}
createExcel:http://www.cnblogs.com/linjiqin/p/3540266.html
//把水平对齐方式指定为居中 format1.setAlignment(jxl.format.Alignment.CENTRE); //把垂直对齐方式指定为居中 format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //设置自动换行 format1.setWrap(true);
//设置行高
sheet.setRowView(0, 200);
//设置列宽
sheet.setColumnView(0, 30);
//获取第一行,第二列的值
Cell c = rs.getCell(列索引, 行索引); //(索引都是从0开始)获取某个单元格
String str = c.getContents(); //获取单元格的内容(值)
// 设置表头
public List<String> orderExport() {
List list = new ArrayList();
list.add("海典编码");
list.add("商品名称");
list.add("商品数量");
list.add("返佣比例(排序)");
list.add("商品金额");
list.add("商品佣金");
return list;
}
扩展:
1:调用方式==>excelExport("海典编码#商品名称#商品数量#返佣比例(排序)#商品金额#商品佣金")
2:excelExport(StringUtil.spaceSplit(title));//spaceSplit是com.rc.portal.util.StringUtil;
在action中定义一个titile并且生成setter/getter方法,在对应页面中加入一个name="title"的隐藏域,然后在点击导出excle按钮出发form表单时加入如下代码:
var title = $("#exceltitle").text();//exceltitle是表头tr的id
$("#title").val(title);//title是隐藏域的id
public List<String> excelExport(String paramStr) {
String str[] = paramStr.split("#");
List<String> list = new ArrayList<String>();
for (String string : str) {
list.add(string);
}
return list;
}
最后还要操作具体SQL的limit语句:导出所有的而不是当前页面
<isNotNull property="type">
<isEqual property="type" compareValue="0">
LIMIT $pageInfo.start$,$pageInfo.end$
</isEqual>
</isNotNull>
/**
* 设置表头(传入以#分割的表头字符串)
* 方法名:excelExport<BR>
* 创建人:Marlon <BR>
* 时间:2016-8-3-上午9:51:27 <BR>
* @param paramStr
* @return List<String><BR>
* @exception <BR>
* @since 1.0.0
*/
public List<String> excelExport(String paramStr) {
String str[] = paramStr.split("#");
List<String> list = new ArrayList<String>();
for (String string : str) {
list.add(string);
}
return list;
}
/**
*
* 方法名:getArray<BR>
* 创建人:Marlon <BR>
* 时间:2016-8-4-下午3:22:49 <BR>
* @param couponList
* @param paramStr
* @return String[][]<BR>
* @exception <BR>
* @since 1.0.0
*/
public String[][] getArray(List couponList,String paramStr){
String str[] = paramStr.split("#");
String[][] s = new String[couponList.size()][14];
for (int i = 0; i < couponList.size(); i++) {
Map m = (Map) couponList.get(i);
for (int j = 0; j < str.length; j++) {
if (m.get(str[j]) == "create_dt") {
if (m.get("create_dt") != null) {
s[i][j] = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) m.get("create_dt"));
} else {
s[i][j] = "";
}
}else {
if (m.get(str[j]) != null) {
s[i][j] = m.get(str[j]).toString();
} else {
s[i][j] = "";
}
}
}
}
return s;
}
附件:down.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<%@page import="java.util.*,com.jspsmart.upload.*,java.lang.*" %>
<%@page import="java.io.File" %>
<%@page import="java.lang.NullPointerException" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Excel 导出下载页面</title>
</head>
<body>
<%
String xlsname=(String)request.getAttribute("xlsname");
SmartUpload su=new SmartUpload();
su.initialize(pageContext);
su.setContentDisposition(null);
su.downloadFile(xlsname);
out.clear();
out = pageContext.pushBody();
File f=new File(xlsname);
f.delete();//删除原(垃圾)文件
%>
</body>
</html>