| 1.controller层内容 @RequestMapping("/report/download") @ResponseBody public HttpServletResponse downloadReport(String wxcid,String sdate, String edate,Integer activityid,HttpServletResponse response){
ResponseMessage report = cutPriceActivityService.report(wxcid,sdate,edate,activityid); List<CutPriceReport> cutPriceReportList = null; if(report.getData() != null) {
cutPriceReportList = (List<CutPriceReport>)report.getData().get("reportList"); } if(cutPriceReportList != null && cutPriceReportList.size() > 0){
Collections.sort(cutPriceReportList, new Comparator<CutPriceReport>() {
DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); public int compare(CutPriceReport o1, CutPriceReport o2) {
Long time = null; Long time2 = null; try {
time = sdf.parse(o1.getDate()).getTime(); time2 = sdf.parse(o2.getDate()).getTime(); } catch (ParseException e) {
e.printStackTrace(); } return time.intValue() - time2.intValue(); } }); } return excelReportService.downloadReport(cutPriceReportList, wxcid, ReportNames.CUTPRICE_DAY_NAME, 0, false, false, null, null,response); } 2.service层 import java.util.List; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.**.ErrorCode; import com.**.ResponseMessage; import com.**.excel.service.ExcelReportService; import com.**.excel.util.Excelkit; import com.**.util.ExcelException; //@Service public class ExcelReportServiceImpl implements ExcelReportService {
Logger log=Logger.getLogger(ExcelReportServiceImpl.class); //@Value("${report.url}") private String report_create_path;//生成报表文件的绝对目录路径 //@Value("${report.realmName}") private String realmName;//生成报表的文件名(不需要后缀名) @Override public <T> HttpServletResponse downloadReport(List<T> reportlist,String wxcid,String reportNames, int titleRowNo, boolean ifTop, boolean ifEnd, String topTitle, String endTitle ,HttpServletResponse response) {
if(reportlist == null || reportlist.size() <= 0) {
//return ResponseMessage.failture(ErrorCode.REPORT_DOWNLOAD_NO_DATA); } String fileurl=null; HttpServletResponse createExcelReport =null; try {
return Excelkit.createExcelReport(reportlist,realmName ,report_create_path, reportNames, wxcid,titleRowNo,ifTop,ifEnd, topTitle, endTitle,response); } catch (ExcelException e) {
e.printStackTrace(); //return ResponseMessage.failture(ErrorCode.REPORT_DOWNLOAD_SYS_ERR); } |