1、JSP部分代码:
//========================== 导出 excel 功能 ==================================
< cas :havePerm url ="contPageEpt.do" >
// 导出功能时间控件
$ ('#eptDate' ).daterangepicker ({
"showDropdowns" : true ,
"showCustomRangeLabel" : false ,
"alwaysShowCalendars" : true ,
"startDate" : moment ().subtract ('days' , 7 ),
"endDate" : moment ().subtract ('days' , 1 ),
"opens" : "left" ,
"drops" : "down"
}, function (start , end , label ) {// 时间改变后执行该方法
//alert(label);
});
/* 搜索条件导出 */
$ ("#search_export" ).on ("click" , function () {
var dateRange1 = $ ('#eptDate' ).val ().replace (/\s/g , "" ).split (" 至 " );
var startDate1 = dateRange1 [0 ];
var endDate1 = dateRange1 [1 ];
var srcContType = $ ("#srcContType" ).val ();
var searchKey = $ ("#searchKey" ).val ();
window .location = " < c :url value ='contPageExport.do' /> " + "?startDate=" + startDate1 + "&endDate=" + endDate1 + "&srcContType=" + srcContType + "&searchKey=" + searchKey ;
})
/* 整体导出 */
$ ("#export" ).on ("click" , function () {
var dateRange1 = $ ('#eptDate' ).val ().replace (/\s/g , "" ).split (" 至 " );
var startDate1 = dateRange1 [0 ];
var endDate1 = dateRange1 [1 ];
var appId = $ ("#appId" ).val ();
var srcContType = $ ("#srcContType" ).val ();
window .location = " < c :url value ='contPageEpt.do' /> " + "?startDate=" + startDate1 + "&endDate=" + endDate1 + "&appId=" + appId + "&srcContType=" + srcContType ;
})
cas :havePerm >
//============================= 导出功能结束 ====================================
2、struts.xml 配置
name ="contPageEpt" class ="bigdata.action.ContPageDayAction" method ="export" >
name ="contPageExport" class ="bigdata.action.ContPageDayAction" method ="searchExport" >
3、action部分代码:
public void export () {
Map, String> params = getParams();
HttpServletResponse response = getResponse();
response .setContentType("application/x-download" );
try {
response .setHeader("Content-Disposition" , "attachment; filename=" + URLEncoder.encode (" 内容受访明细 " , "UTF-8" ) + ".xls" );
} catch (UnsupportedEncodingException e1 ) {
e1 .printStackTrace();
}
ServletOutputStream outputStream = null;
try {
outputStream = response .getOutputStream();
makeExcel(params , outputStream );
} catch (IOException e ) {
e .printStackTrace();
}
}
public void makeExcel (Map, String> params , ServletOutputStream outputStream ) throws IOException {
List list = this .contPageDayService .expList(params );
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook .createSheet(" 内容受访明细 " );
sheet .setColumnWidth(0 , 20 * 256 );
sheet .setColumnWidth(1 , 20 * 256 );
sheet .setColumnWidth(3 , 20 * 256 );
sheet .setColumnWidth(4 , 20 * 256 );
sheet .setColumnWidth(5 , 20 * 256 );
sheet .setColumnWidth(6 , 20 * 256 );
Row row ;
Cell cell ;
row = sheet .createRow(0 );
HSSFFont font = workbook .createFont();
font .setColor(HSSFColor.RED.index );
font .setFontHeightInPoints((short ) 12 );
font .setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL );
HSSFCellStyle style1 = workbook .createCellStyle();
style1 .setFont(font );
style1 .setAlignment(HSSFCellStyle.ALIGN_CENTER );
row .setRowStyle(style1 );
row .setHeight((short ) 350 );
cell = row .createCell(0 );
cell .setCellValue(" 日期 " );
cell .setCellStyle(style1 );
cell = row .createCell(1 );
cell .setCellValue("a 名称 " );
cell .setCellStyle(style1 );
cell = row .createCell(2 );
cell .setCellValue(" b名称 " );
cell .setCellStyle(style1 );
cell = row .createCell(3 );
cell .setCellValue(" c名称 " );
cell .setCellStyle(style1 );
cell = row .createCell(4 );
cell .setCellValue("PV 数 " );
cell .setCellStyle(style1 );
cell = row .createCell(5 );
cell .setCellValue("UV 数 " );
cell .setCellStyle(style1 );
cell = row .createCell(6 );
cell .setCellValue("IP 数 " );
cell .setCellStyle(style1 );
for (int i = 0 ; i < list .size(); i ++) {
row = sheet .createRow(i + 1 );
cell = row .createCell(0 );
cell .setCellValue(list .get(i ).getStatDate());
cell = row .createCell(1 );
cell .setCellValue(list .get(i ).getAName());
cell = row .createCell(2 );
cell .setCellValue(list .get(i ).getBName());
cell = row .createCell(3 );
cell .setCellValue(list .get(i ).getCName());
cell = row .createCell(4 );
cell .setCellValue(list .get(i ).getPv());
cell = row .createCell(5 );
cell .setCellValue(list .get(i ).getUv());
cell = row .createCell(6 );
cell .setCellValue(list .get(i ).getIp());
}
workbook .write(outputStream );
outputStream .flush();
outputStream .close();
}
/*------------------------------------ 按搜索条件导出 --start------------------------------------------------*/
public void searchExport () {
Map, String> params = getParams();
HttpServletResponse response = getResponse();
response .setContentType("application/x-download" );
try {
response .setHeader("Content-Disposition" , "attachment; filename=" + URLEncoder.encode (" 内容受访明细 " , "UTF-8" ) + ".xls" );
} catch (UnsupportedEncodingException e1 ) {
e1 .printStackTrace();
}
ServletOutputStream outputStream = null;
try {
outputStream = response .getOutputStream();
searchExcel(params , outputStream );
} catch (IOException e ) {
e .printStackTrace();
}
}
public void searchExcel (Map, String> searchParams , ServletOutputStream outputStream ) throws IOException {
List, Object>> list = this .contPageDayService .searchList(searchParams );
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook .createSheet(" 内容受访明细 " );
sheet .setColumnWidth(0 , 20 * 256 );
sheet .setColumnWidth(1 , 20 * 256 );
sheet .setColumnWidth(3 , 20 * 256 );
sheet .setColumnWidth(4 , 20 * 256 );
sheet .setColumnWidth(5 , 20 * 256 );
sheet .setColumnWidth(6 , 20 * 256 );
sheet .setColumnWidth(7 , 20 * 256 );
sheet .setColumnWidth(8 , 20 * 256 );
sheet .setColumnWidth(9 , 20 * 256 );
Row row ;
Cell cell ;
row = sheet .createRow(0 );
HSSFFont font = workbook .createFont();
font .setColor(HSSFColor.RED.index );
font .setFontHeightInPoints((short ) 12 );
font .setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL );
HSSFCellStyle style1 = workbook .createCellStyle();
style1 .setFont(font );
style1 .setAlignment(HSSFCellStyle.ALIGN_CENTER );
row .setRowStyle(style1 );
row .setHeight((short ) 350 );
cell = row .createCell(0 );
cell .setCellValue(" 日期 " );
cell .setCellStyle(style1 );
cell = row .createCell(1 );
cell .setCellValue(" a名称 " );
cell .setCellStyle(style1 );
cell = row .createCell(2 );
cell .setCellValue(" b名称 " );
cell .setCellStyle(style1 );
cell = row .createCell(3 );
cell .setCellValue(" c名称 " );
cell .setCellStyle(style1 );
cell = row .createCell(4 );
cell .setCellValue(" Android (PV 数 ) " );
cell .setCellStyle(style1 );
cell = row .createCell(5 );
cell .setCellValue(" Android (UV 数 ) " );
cell .setCellStyle(style1 );
cell = row .createCell(6 );
cell .setCellValue(" Android (IP 数 ) " );
cell .setCellStyle(style1 );
cell = row .createCell(7 );
cell .setCellValue(" IOS (PV 数 ) " );
cell .setCellStyle(style1 );
cell = row .createCell(8 );
cell .setCellValue(" IOS (UV 数 ) " );
cell .setCellStyle(style1 );
cell = row .createCell(9 );
cell .setCellValue(" IOS (IP 数 ) " );
cell .setCellStyle(style1 );
for (int i = 0 ; i < list .size(); i ++) {
row = sheet .createRow(i + 1 );
cell = row .createCell(0 );
cell .setCellValue(String.valueOf (list .get(i ).get("statDate" )));
cell = row .createCell(1 );
cell .setCellValue(String.valueOf (list .get(i ).get("aName" )));
cell = row .createCell(2 );
cell .setCellValue(String.valueOf (list .get(i ).get("bName" )));
cell = row .createCell(3 );
cell .setCellValue(String.valueOf (list .get(i ).get("cName" )));
cell = row .createCell(4 );
cell .setCellValue(String.valueOf (null == list .get(i ).get("apv" ) ? "0" : list .get(i ).get("apv" )));
cell = row .createCell(5 );
cell .setCellValue(String.valueOf (null == list .get(i ).get("auv" ) ? "0" : list .get(i ).get("auv" )));
cell = row .createCell(6 );
cell .setCellValue(String.valueOf (null == list .get(i ).get("aip" ) ? "0" : list .get(i ).get("aip" )));
cell = row .createCell(7 );
cell .setCellValue(String.valueOf (null == list .get(i ).get("bpv" ) ? "0" : list .get(i ).get("bpv" )));
cell = row .createCell(8 );
cell .setCellValue(String.valueOf (null == list .get(i ).get("buv" ) ? "0" : list .get(i ).get("buv" )));
cell = row .createCell(9 );
cell .setCellValue(String.valueOf (null == list .get(i ).get("bip" ) ? "0" : list .get(i ).get("bip" )));
}
workbook .write(outputStream );
outputStream .flush();
outputStream .close();
}
/*------------------------------------ 按搜索条件导出 --end------------------------------------------------*/
public Map, String> getParams () {
Map, String> map = new HashMap, String>();
String appId = getRequest().getParameter("appId" );
if (StringUtils.isNotBlank (appId )) {
map .put("appId" , appId );
}
String clientVersion = getRequest().getParameter("clientVersion" );
if (StringUtils.isNotBlank (clientVersion )) {
map .put("clientVersion" , clientVersion );
}
String platform = getRequest().getParameter("platform" );
if (StringUtils.isNotBlank (platform )) {
map .put("platform" , platform );
}
String clientChannel = getRequest().getParameter("clientChannel" );
if (StringUtils.isNotBlank (clientChannel )) {
map .put("clientChannel" , clientChannel );
}
String date = getRequest().getParameter("date" );
if (StringUtils.isNotBlank (date )) {
map .put("date" , date .replaceAll("-" , "" ));
}
String searchKey = getRequest().getParameter("searchKey" );
if (StringUtils.isNotBlank (searchKey )) {
map .put("searchKey" , searchKey );
}
String srcContType = getRequest().getParameter("srcContType" );
if (StringUtils.isNotBlank (srcContType )) {
map .put("srcContType" , srcContType );
}
String startDate = getRequest().getParameter("startDate" );
if (StringUtils.isNotBlank (startDate )) {
map .put("startDate" , startDate .replaceAll("-" , "" ));
}
String endDate = getRequest().getParameter("endDate" );
if (StringUtils.isNotBlank (endDate )) {
map .put("endDate" , endDate .replaceAll("-" , "" ));
}
String menuId = getRequest().getParameter("aId" );
if (StringUtils.isNotBlank (menuId )) {
map .put("aId" , menuId );
}
String areaId = getRequest().getParameter("bId" );
if (StringUtils.isNotBlank (areaId )) {
map .put("bId" , areaId );
}
String contId = getRequest().getParameter("cId" );
if (StringUtils.isNotBlank (contId )) {
map .put("cId" , contId );
}
return map ;
}
4、dao代码部分:
public interface ContPageDayDao {
public List expList (Map, String> params );
public List, Object>> searchList (Map, String> searchParams );
}
5、impl接口实现代码部分:
@Override
public List expList (Map, String> params ) {
List sqlParams = new ArrayList();
StringBuffer queryString = new StringBuffer("select statdate,aName,bName,cName,sum(pv) pv,sum(uv) uv,sum(ip) ip from cont_page_day where 1=1 " );
String startDate = params .get("startDate" );
if (StringUtils.isNotBlank (startDate )) {
queryString .append("and statdate>=? " );
sqlParams .add(Long.valueOf (startDate ));
}
String endDate = params .get("endDate" );
if (StringUtils.isNotBlank (endDate )) {
queryString .append("and statdate<=? " );
sqlParams .add(Long.valueOf (endDate ));
}
String appId = params .get("appId" );
if (StringUtils.isNotBlank (appId )) {
queryString .append("and (md = ? or md = ? or md = ? )" );
sqlParams .add(appId + "-1" );
sqlParams .add(appId + "-2" );
sqlParams .add(appId + "-3" );
}
String srcContType = params .get("srcContType" );
if (StringUtils.isNotBlank (srcContType )) {
queryString .append("and srcContType=? " );
sqlParams .add(Integer.valueOf (srcContType ));
}
queryString .append("group by aId,aName,bId,bName,cId,cName,statdate order by statdate desc" );
List dtos = new ArrayList();
List, Object>> results = query(queryString .toString(), sqlParams .toArray());
for (Map, Object> result : results ) {
ContPageDay dto = new ContPageDay();
dto .setStatDate((Long) result .get("statdate" ));
dto .setAName((String) result .get("aName" ));
dto .setBName((String) result .get("bName" ));
dto .setCName((String) result .get("cName" ));
dto .setIp(Long.valueOf (result .get("ip" ).toString()));
dto .setPv(Long.valueOf (result .get("pv" ).toString()));
dto .setUv(Long.valueOf (result .get("uv" ).toString()));
dtos .add(dto );
}
return dtos ;
}
/*-------------------------------------------------- 按条件搜索 --begin---------------------------------------------------------*/
@Override
public List, Object>> searchList (Map, String> searchParams ) {
List sqlParams = new ArrayList();
StringBuffer queryString = new StringBuffer(
"SELECT * FROM ( " +
" SELECT " +
"a.statDate," +
"a.aName," +
"a.bName," +
"a.cName," +
"a.aId," +
"a.bId," +
"a.cId," +
"a.pv apv," +
"a.uv auv," +
"a.ip aip," +
"b.pv bpv," +
"b.uv buv," +
"b.ip bip " +
"FROM ( " +
"SELECT * FROM cont_page_day " +
"WHERE statdate BETWEEN ? AND ? " +
"AND md = '3-1' " +
"AND ( aName LIKE ? " +
"OR bName LIKE ? " +
"OR cName LIKE ?)) a " +
"LEFT JOIN(" +
"SELECT * FROM cont_page_day " +
"WHERE statdate BETWEEN ? AND ? " +
"AND md = '3-2' " +
"AND (" +
"aName LIKE ? " +
"OR bName LIKE ? " +
"OR cName LIKE ? )) b " +
"ON a.statDate = b.statDate " +
"AND a.aName = b.aName " +
"AND a.bName = b.bName " +
"AND a.cName = b.cName " +
"AND a.aId = b.aId " +
"AND a.bId = b.bId " +
"AND a.cId = b.cId " +
"UNION ALL " +
"SELECT " +
"e.statDate," +
"e.aName," +
"e.bName," +
"e.cName," +
"e.aId," +
"e.bId," +
"e.cId," +
"e.pv bpv," +
"e.uv buv," +
"e.ip bip," +
"d.pv apv," +
"d.uv auv," +
"d.ip aip " +
"FROM ( SELECT * FROM cont_page_day " +
"WHERE statdate BETWEEN ? AND ? " +
"AND md = '3-1' " +
"AND ( aName LIKE ? " +
"OR bName LIKE ? " +
"OR cName LIKE ?" +
")) d " +
"RIGHT JOIN (" +
" SELECT * FROM cont_page_day " +
"WHERE statdate BETWEEN ? AND ? " +
"AND md = '3-2' " +
"AND ( aName LIKE ? " +
"OR bName LIKE ? " +
"OR cName LIKE ?" +
")) e " +
"ON d.statdate = e.statdate " +
"AND d.aName = e.aName " +
"AND d.bName = e.bName " +
"AND d.cName = e.cName " +
"AND d.aId = e.aId " +
"AND d.bId = e.bId " +
"AND d.cId = e.cId " +
"WHERE d.aName IS NULL " +
"AND d.bName IS NULL " +
"AND d.cName IS NULL " +
"AND d.aId IS NULL " +
"AND d.bId IS NULL " +
"AND d.cId IS NULL" +
") f " +
"ORDER BY " +
"statDate ASC" );
String startDateQuery = "" ;
String endDateQuery = "" ;
String searchKeyQuery = "" ;
String startDate = searchParams .get("startDate" );
if (StringUtils.isNotBlank (startDate )) {
startDateQuery = startDate ;
}
String endDate = searchParams .get("endDate" );
if (StringUtils.isNotBlank (endDate )) {
endDateQuery = endDate ;
}
String searchKey = searchParams .get("searchKey" );
if (StringUtils.isNotBlank (searchKey )) {
searchKeyQuery = searchKey ;
}
/*sql 中有几个问号对应几条数据 -4*5-*/
for (int i = 0 ; i < 4 ; i ++) {
sqlParams .add(Long.valueOf (startDateQuery ));
sqlParams .add(Long.valueOf (endDateQuery ));
for (int j = 0 ; j < 3 ; j ++) {
sqlParams .add("%" + String.valueOf (searchKeyQuery ) + "%" );
}
}
List, Object>> results = query(queryString .toString(), sqlParams .toArray());
return results ;
}