java excel导出

需求 导出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);
            List<OfflineVo> list = 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<OfflineVo> 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<NameValuePair> params = new ArrayList<NameValuePair>();
        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<OfflineVo> list = JSONArray.toList(array, OfflineVo.class);
        return list;
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值