AgentController.java 10.8 KB
package com.tianbo.imfClient.controller;

import com.tianbo.imfClient.dao.AGENTSTATISTICSMapper;
import com.tianbo.imfClient.dao.ARRIVEDSECONDARYMapper;
import com.tianbo.imfClient.model.ORIGINMANIFESTMASTER;
import com.tianbo.imfClient.model.ResultJson;
import com.tianbo.util.POI.ExportExcelUtil;
import lombok.extern.slf4j.Slf4j;
import oracle.sql.DATE;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 出港运抵服务接口
 */
@Slf4j
@RestController
@RequestMapping("/agent")
public class AgentController {

    @Autowired
    private AGENTSTATISTICSMapper agentstatisticsMapper;
    /**
     * 查询统计出港业务数据接口
     */
    @GetMapping("/analysis")
    public void analysis(@RequestParam(value = "startdate",required = true) String startdate,
                                        @RequestParam(value = "enddate",required = true) String enddate,
                               HttpServletResponse httpServletResponse){

        Map map = new HashMap();
        map.put("startdate",startdate);
        map.put("enddate",enddate);
        List<ORIGINMANIFESTMASTER> list = agentstatisticsMapper.statistics(map);
        exportExcel(list,httpServletResponse);
    }

    public void exportExcel(List<ORIGINMANIFESTMASTER> list,HttpServletResponse response){

        // 创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();

        // 创建表
        HSSFSheet sheet = workbook.createSheet("export");
        // 创建行
        HSSFRow row = sheet.createRow(0);
        // 创建单元格样式
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        // 表头
        String[] head = {
                "运单号",
                "航空托运人名称",
                "航空销售代理人名称",
                "航协编号",
                "航空公司运输证明",
                "运输条件鉴定书",
                "承运人",
                "航班号",
                "航班日期",
                "航班起始站",
                "航班目的站",
                "运单件数",
                "运单重量",
                "操作时间",
                "关区",
                "预配品名",
                "收货人公司",
                "收货人地址",
                "收货人城市",
                "收货人电话",
                "收货人国家",
                "收货人名称",
                "发货人名称",
                "发货人地址",
                "发货人城市",
                "发货人国家",
                "发货人电话",
                "发货人名称",
                "海关回执状态",
                "海关回执信息",
                "发货人编码",
                "收货人编码",
                "货物品名",
                "二级类名称",
                "一级类名称",
                "代理人三字码",
                "代理人全称",
                "代理人类别",
                "代理联系人",
                "代理联系人电话"
        };
        HSSFCell cell;
        // 设置表头
        for (int i = 0; i < head.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(head[i]);
            cell.setCellStyle(cellStyle);
            // 设置单元格宽度
            sheet.setColumnWidth(i, 4000);
        }
        // 设置表格内容
        for (int i = 0; i < list.size(); i++){
            row = sheet.createRow( i + 1);
            ORIGINMANIFESTMASTER originmanifestmaster = list.get(i);
            // 这里是内容设置,替换则自己的数据即可
            String[] excelTitle = new String[40];
            excelTitle[0] = originmanifestmaster.getAutoid();
            excelTitle[1] = originmanifestmaster.getWaybillnomaster();
            excelTitle[2] = originmanifestmaster.getSegment();
            excelTitle[3] = originmanifestmaster.getOriginatingstation();
            excelTitle[4] = originmanifestmaster.getDestinationstation();
            excelTitle[5] = originmanifestmaster.getTotalweight();
            excelTitle[6] = originmanifestmaster.getTotalpiece();
            excelTitle[7] = originmanifestmaster.getManifesttotalpiece();
            excelTitle[8] = originmanifestmaster.getManifesttotalweight();
            excelTitle[9] = originmanifestmaster.getFlightno();
            excelTitle[10] = originmanifestmaster.getProductname();
            excelTitle[11] = originmanifestmaster.getCustomsstatus();
            excelTitle[12] = originmanifestmaster.getCarrier1();
            excelTitle[13] = originmanifestmaster.getArrivalstation1();
            excelTitle[14] = originmanifestmaster.getCarrier2();
            excelTitle[15] = originmanifestmaster.getArrivalstation2();
            excelTitle[16] = originmanifestmaster.getCarrier3();
            excelTitle[17] = originmanifestmaster.getArrivalstation3();
            excelTitle[18] = originmanifestmaster.getPaymode();
            excelTitle[19] = originmanifestmaster.getCustomscode();
            excelTitle[20] = originmanifestmaster.getSpecialgoodscode();
            excelTitle[21] = originmanifestmaster.getShippername();
            excelTitle[22] = originmanifestmaster.getShipperaddress();
            excelTitle[23] = originmanifestmaster.getConsigneename();
            excelTitle[24] = originmanifestmaster.getConsigneeaddress();
            excelTitle[25] = originmanifestmaster.getReceiptinformation();
            excelTitle[26] = originmanifestmaster.getSpecificConsigneePhone();
            excelTitle[27] = originmanifestmaster.getConsigneePhone();
            excelTitle[28] = originmanifestmaster.getStatus();
            excelTitle[29] = originmanifestmaster.getIsbatch();
            excelTitle[30] = originmanifestmaster.getOriginatingstationBill();
            excelTitle[31] = originmanifestmaster.getDestinationstationBill();
            excelTitle[32] = originmanifestmaster.getReportorder();
            excelTitle[33] = originmanifestmaster.getIslast();
            excelTitle[34] = originmanifestmaster.getShipperCode();
            excelTitle[35] = originmanifestmaster.getShipperCountrycode();
            excelTitle[36] = originmanifestmaster.getShipperPhone();
            excelTitle[37] = originmanifestmaster.getShipperFax();
            excelTitle[38] = originmanifestmaster.getConsigneeCode();
            excelTitle[39] = originmanifestmaster.getConsigneeCountrycode();
            for (int j = 0; j < excelTitle.length; j++){
                row.createCell(j).setCellValue(excelTitle[j]);
            }
        }

        // 设置文件名
        String title ="export.xls";
        try {
            String fileName = new String(title.getBytes(StandardCharsets.UTF_8), "ISO-8859-1");
             fileName = URLEncoder.encode(fileName,"utf-8");
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            //默认Excel名称
            response.setHeader("Content-Disposition", "attachment;filename="+fileName+";"+"filename*=utf-8''"+fileName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        try{
            OutputStream os = response.getOutputStream();
            workbook.write(os);
            os.flush();
            os.close();
            log.info("导出完毕");
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void export(List<ORIGINMANIFESTMASTER> list,HttpServletResponse response){
        String[] header = {
                "运单号",
                "航空托运人名称",
                "航空销售代理人名称",
                "航协编号",
                "航空公司运输证明",
                "运输条件鉴定书",
                "承运人",
                "航班号",
                "航班日期",
                "航班起始站",
                "航班目的站",
                "运单件数",
                "运单重量",
                "操作时间",
                "关区",
                "预配品名",
                "收货人公司",
                "收货人地址",
                "收货人城市",
                "收货人电话",
                "收货人国家",
                "收货人名称",
                "发货人名称",
                "发货人地址",
                "发货人城市",
                "发货人国家",
                "发货人电话",
                "发货人名称",
                "海关回执状态",
                "海关回执信息",
                "发货人编码",
                "收货人编码",
                "货物品名",
                "二级类名称",
                "一级类名称",
                "代理人三字码",
                "代理人全称",
                "代理人类别",
                "代理联系人",
                "代理联系人电话"
        };
        String[] columns = {
                "autoid",
                "waybillnomaster",
                "segment",
                "originatingstation",
                "destinationstation",
                "totalweight",
                "totalpiece",
                "manifesttotalpiece",
                "manifesttotalweight",
                "flightno",
                "productname",
                "customsstatus",
                "carrier1",
                "arrivalstation1",
                "carrier2",
                "arrivalstation2",
                "carrier3",
                "arrivalstation3",
                "paymode",
                "customscode",
                "specialgoodscode",
                "shippername",
                "shipperaddress",
                "consigneename",
                "consigneeaddress",
                "receiptinformation",
                "specificConsigneePhone",
                "consigneePhone",
                "status",
                "isbatch",
                "originatingstationBill",
                "destinationstationBill",
                "reportorder",
                "islast",
                "shipperCode",
                "shipperCountrycode",
                "shipperPhone",
                "shipperFax",
                "consigneeCode",
                "consigneeCountrycode"
        };
        ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
        try{
            exportExcelUtil.export("export.xls","export",header,columns,list,response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}