需求 导出excel
package com.sky.car.controller.jt; import com.sky.car.model.OfflineVo; import com.sky.car.util.HttpUtils; import com.sky.common.Environment; import jxl.Workbook; import jxl.format.CellFormat; import jxl.read.biff.BiffException; import jxl.write.*; import net.sf.json.JSONArray; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; /** * Created by jack on 2017/5/15. */ @Controller @CrossOrigin public class StatOfflineDataExportController { @Autowired Environment environment; @RequestMapping(value = "/statOfflineData/export", produces = {"text/html;charset=UTF-8"}) public void download(HttpServletRequest request, HttpServletResponse response, String days, String ownerName, String apiKey) throws IOException, BiffException, WriteException, Exception { response.setHeader("Content-Disposition", "attachment; filename=count.xls"); WritableWorkbook writableWorkbook = ExcelUtils.createTemplate(response.getOutputStream(), environment.getJtHost(), days, ownerName, apiKey); writableWorkbook.write(); writableWorkbook.close(); } static class ExcelUtils { public static WritableWorkbook createTemplate(OutputStream output, String headerUrl, String days, String ownerName, String apiKey) throws IOException, WriteException, Exception { WritableWorkbook writableWorkbook = Workbook.createWorkbook(output); WritableSheet wsheet = writableWorkbook.createSheet("离线数据统计", 0); CellFormat cf = writableWorkbook.getSheet(0).getCell(1, 0).getCellFormat(); WritableCellFormat wc = new WritableCellFormat(); // 设置居中 // wc.setAlignment(GroupLayout.Alignment.CENTER); // 设置边框线 wc.setBorder(Border.ALL, BorderLineStyle.THIN); wc.setBackground(jxl.format.Colour.GREEN); Label nc0 = new Label(0, 0, "行政区域", wc);//Label(x,y,z)其中x代表单元格的第x+1列,第y+1行, 单元格的内容是z Label nc1 = new Label(1, 0, "单位名称", wc); Label nc2 = new Label(2, 0, "车牌号", wc); Label nc3 = new Label(3, 0, "车牌颜色", wc); Label nc4 = new Label(4, 0, "最后上线时间", wc); Label nc5 = new Label(5, 0, "离线天数", wc); Label nc6 = new Label(6, 0, "统计日期", wc); wsheet.addCell(nc0); wsheet.addCell(nc1); wsheet.addCell(nc2); wsheet.addCell(nc3); wsheet.addCell(nc4); wsheet.addCell(nc5); wsheet.addCell(nc6); Listlist = loadDate(apiKey, headerUrl, ownerName, days); for (int i = 1; i <= list.size(); i++) { OfflineVo offlineVo = list.get(i - 1); Label ncd0 = new Label(0, i, offlineVo.getDistrictName());//Label(x,y,z)其中x代表单元格的第x+1列,第y+1行, 单元格的内容是z Label ncd1 = new Label(1, i, offlineVo.getOwnerName()); Label ncd2 = new Label(2, i, offlineVo.getVname()); Label ncd3 = new Label(3, i, offlineVo.getColor()); Label ncd4 = new Label(4, i, offlineVo.getLastGPSTime()); Label ncd5 = new Label(5, i, offlineVo.getOfflineDays()); Label ncd6 = new Label(6, i, offlineVo.getStatDate()); wsheet.addCell(ncd0); wsheet.addCell(ncd1); wsheet.addCell(ncd2); wsheet.addCell(ncd3); wsheet.addCell(ncd4); wsheet.addCell(ncd5); wsheet.addCell(ncd6); } return writableWorkbook; } } static List loadDate(String apiKey, String headerUrl, String ownerName, String days) throws Exception { String url = headerUrl + "/mnt.rest/rest/plat-stat-shx-svc/statOfflineData?ownerName=@ownerName&days=@days" .replace("@ownerName", ownerName) .replace("@days", days); List params = new ArrayList (); params.add(new BasicNameValuePair("ownerName", ownerName)); params.add(new BasicNameValuePair("days", days)); String result = HttpUtils.post(url, params, apiKey); JSONArray array = JSONArray.fromObject(result); List list = JSONArray.toList(array, OfflineVo.class); return list; } }