作者 朱兆平

后端导出excel

@@ -16,7 +16,7 @@ spring: @@ -16,7 +16,7 @@ spring:
16 static-locations: classpath:/META-INF/resources/,classpath:/static,classpath:/resources/,file:${web.upload-path} 16 static-locations: classpath:/META-INF/resources/,classpath:/static,classpath:/resources/,file:${web.upload-path}
17 17
18 application: 18 application:
19 - name: analysis-nmms 19 + name: analysis-agent
20 20
21 jackson: 21 jackson:
22 serialization: 22 serialization:
@@ -44,8 +44,8 @@ spring: @@ -44,8 +44,8 @@ spring:
44 #oracle 44 #oracle
45 driver-class-name: oracle.jdbc.OracleDriver 45 driver-class-name: oracle.jdbc.OracleDriver
46 url: jdbc:oracle:thin:@10.50.3.69:1521:CGODB 46 url: jdbc:oracle:thin:@10.50.3.69:1521:CGODB
47 - username: CGONMS  
48 - password: 1q2w3e4r 47 + username: CGOASM
  48 + password: vmvnv1v2
49 #spring datasource mysql,注意编码配置,缺少数据库编码配置容易引起中文入库乱码 49 #spring datasource mysql,注意编码配置,缺少数据库编码配置容易引起中文入库乱码
50 # url: jdbc:mysql://127.0.0.1:3307/statistics?useUnicode=true&characterEncoding=utf8 50 # url: jdbc:mysql://127.0.0.1:3307/statistics?useUnicode=true&characterEncoding=utf8
51 # username: root 51 # username: root
@@ -88,6 +88,7 @@ eureka: @@ -88,6 +88,7 @@ eureka:
88 service-url: 88 service-url:
89 # defaultZone: http://10.50.3.82:19527/eureka/ 89 # defaultZone: http://10.50.3.82:19527/eureka/
90 defaultZone: http://192.168.1.53:12345/eureka/ 90 defaultZone: http://192.168.1.53:12345/eureka/
  91 +# defaultZone: http://127.0.0.1:12345/eureka/
91 registry-fetch-interval-seconds: 30 92 registry-fetch-interval-seconds: 30
92 lease-renewal-interval-in-seconds: 15 93 lease-renewal-interval-in-seconds: 15
93 lease-expiration-duration-in-seconds: 45 94 lease-expiration-duration-in-seconds: 45
@@ -88,6 +88,12 @@ @@ -88,6 +88,12 @@
88 <artifactId>util</artifactId> 88 <artifactId>util</artifactId>
89 <version>1.0-SNAPSHOT</version> 89 <version>1.0-SNAPSHOT</version>
90 </dependency> 90 </dependency>
  91 + <!-- excel导出-->
  92 + <dependency>
  93 + <groupId>org.apache.poi</groupId>
  94 + <artifactId>poi-ooxml</artifactId>
  95 + <version>4.1.0</version>
  96 + </dependency>
91 <!--util依赖--> 97 <!--util依赖-->
92 <!--&lt;!&ndash;IMF需要的包&ndash;&gt;--> 98 <!--&lt;!&ndash;IMF需要的包&ndash;&gt;-->
93 <!--&lt;!&ndash; https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans &ndash;&gt;--> 99 <!--&lt;!&ndash; https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans &ndash;&gt;-->
@@ -4,9 +4,19 @@ import com.tianbo.imfClient.dao.AGENTSTATISTICSMapper; @@ -4,9 +4,19 @@ import com.tianbo.imfClient.dao.AGENTSTATISTICSMapper;
4 import com.tianbo.imfClient.dao.ARRIVEDSECONDARYMapper; 4 import com.tianbo.imfClient.dao.ARRIVEDSECONDARYMapper;
5 import com.tianbo.imfClient.model.ORIGINMANIFESTMASTER; 5 import com.tianbo.imfClient.model.ORIGINMANIFESTMASTER;
6 import com.tianbo.imfClient.model.ResultJson; 6 import com.tianbo.imfClient.model.ResultJson;
  7 +import com.tianbo.util.POI.ExportExcelUtil;
  8 +import lombok.extern.slf4j.Slf4j;
  9 +import oracle.sql.DATE;
  10 +import org.apache.poi.hssf.usermodel.*;
7 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.web.bind.annotation.*; 12 import org.springframework.web.bind.annotation.*;
9 13
  14 +import javax.servlet.http.HttpServletResponse;
  15 +import java.io.IOException;
  16 +import java.io.OutputStream;
  17 +import java.io.UnsupportedEncodingException;
  18 +import java.net.URLEncoder;
  19 +import java.nio.charset.StandardCharsets;
10 import java.util.HashMap; 20 import java.util.HashMap;
11 import java.util.List; 21 import java.util.List;
12 import java.util.Map; 22 import java.util.Map;
@@ -14,6 +24,7 @@ import java.util.Map; @@ -14,6 +24,7 @@ import java.util.Map;
14 /** 24 /**
15 * 出港运抵服务接口 25 * 出港运抵服务接口
16 */ 26 */
  27 +@Slf4j
17 @RestController 28 @RestController
18 @RequestMapping("/agent") 29 @RequestMapping("/agent")
19 public class AgentController { 30 public class AgentController {
@@ -24,14 +35,245 @@ public class AgentController { @@ -24,14 +35,245 @@ public class AgentController {
24 * 查询统计出港业务数据接口 35 * 查询统计出港业务数据接口
25 */ 36 */
26 @GetMapping("/analysis") 37 @GetMapping("/analysis")
27 - public ResultJson analysis(@RequestParam(value = "startdate",required = true) String startdate,  
28 - @RequestParam(value = "enddate",required = true) String enddate){ 38 + public void analysis(@RequestParam(value = "startdate",required = true) String startdate,
  39 + @RequestParam(value = "enddate",required = true) String enddate,
  40 + HttpServletResponse httpServletResponse){
29 41
30 Map map = new HashMap(); 42 Map map = new HashMap();
31 map.put("startdate",startdate); 43 map.put("startdate",startdate);
32 map.put("enddate",enddate); 44 map.put("enddate",enddate);
33 List<ORIGINMANIFESTMASTER> list = agentstatisticsMapper.statistics(map); 45 List<ORIGINMANIFESTMASTER> list = agentstatisticsMapper.statistics(map);
  46 + exportExcel(list,httpServletResponse);
  47 + }
  48 +
  49 + public void exportExcel(List<ORIGINMANIFESTMASTER> list,HttpServletResponse response){
  50 +
  51 + // 创建工作簿
  52 + HSSFWorkbook workbook = new HSSFWorkbook();
  53 +
  54 + // 创建表
  55 + HSSFSheet sheet = workbook.createSheet("export");
  56 + // 创建行
  57 + HSSFRow row = sheet.createRow(0);
  58 + // 创建单元格样式
  59 + HSSFCellStyle cellStyle = workbook.createCellStyle();
  60 + // 表头
  61 + String[] head = {
  62 + "运单号",
  63 + "航空托运人名称",
  64 + "航空销售代理人名称",
  65 + "航协编号",
  66 + "航空公司运输证明",
  67 + "运输条件鉴定书",
  68 + "承运人",
  69 + "航班号",
  70 + "航班日期",
  71 + "航班起始站",
  72 + "航班目的站",
  73 + "运单件数",
  74 + "运单重量",
  75 + "操作时间",
  76 + "关区",
  77 + "预配品名",
  78 + "收货人公司",
  79 + "收货人地址",
  80 + "收货人城市",
  81 + "收货人电话",
  82 + "收货人国家",
  83 + "收货人名称",
  84 + "发货人名称",
  85 + "发货人地址",
  86 + "发货人城市",
  87 + "发货人国家",
  88 + "发货人电话",
  89 + "发货人名称",
  90 + "海关回执状态",
  91 + "海关回执信息",
  92 + "发货人编码",
  93 + "收货人编码",
  94 + "货物品名",
  95 + "二级类名称",
  96 + "一级类名称",
  97 + "代理人三字码",
  98 + "代理人全称",
  99 + "代理人类别",
  100 + "代理联系人",
  101 + "代理联系人电话"
  102 + };
  103 + HSSFCell cell;
  104 + // 设置表头
  105 + for (int i = 0; i < head.length; i++) {
  106 + cell = row.createCell(i);
  107 + cell.setCellValue(head[i]);
  108 + cell.setCellStyle(cellStyle);
  109 + // 设置单元格宽度
  110 + sheet.setColumnWidth(i, 4000);
  111 + }
  112 + // 设置表格内容
  113 + for (int i = 0; i < list.size(); i++){
  114 + row = sheet.createRow( i + 1);
  115 + ORIGINMANIFESTMASTER originmanifestmaster = list.get(i);
  116 + // 这里是内容设置,替换则自己的数据即可
  117 + String[] excelTitle = new String[40];
  118 + excelTitle[0] = originmanifestmaster.getAutoid();
  119 + excelTitle[1] = originmanifestmaster.getWaybillnomaster();
  120 + excelTitle[2] = originmanifestmaster.getSegment();
  121 + excelTitle[3] = originmanifestmaster.getOriginatingstation();
  122 + excelTitle[4] = originmanifestmaster.getDestinationstation();
  123 + excelTitle[5] = originmanifestmaster.getTotalweight();
  124 + excelTitle[6] = originmanifestmaster.getTotalpiece();
  125 + excelTitle[7] = originmanifestmaster.getManifesttotalpiece();
  126 + excelTitle[8] = originmanifestmaster.getManifesttotalweight();
  127 + excelTitle[9] = originmanifestmaster.getFlightno();
  128 + excelTitle[10] = originmanifestmaster.getProductname();
  129 + excelTitle[11] = originmanifestmaster.getCustomsstatus();
  130 + excelTitle[12] = originmanifestmaster.getCarrier1();
  131 + excelTitle[13] = originmanifestmaster.getArrivalstation1();
  132 + excelTitle[14] = originmanifestmaster.getCarrier2();
  133 + excelTitle[15] = originmanifestmaster.getArrivalstation2();
  134 + excelTitle[16] = originmanifestmaster.getCarrier3();
  135 + excelTitle[17] = originmanifestmaster.getArrivalstation3();
  136 + excelTitle[18] = originmanifestmaster.getPaymode();
  137 + excelTitle[19] = originmanifestmaster.getCustomscode();
  138 + excelTitle[20] = originmanifestmaster.getSpecialgoodscode();
  139 + excelTitle[21] = originmanifestmaster.getShippername();
  140 + excelTitle[22] = originmanifestmaster.getShipperaddress();
  141 + excelTitle[23] = originmanifestmaster.getConsigneename();
  142 + excelTitle[24] = originmanifestmaster.getConsigneeaddress();
  143 + excelTitle[25] = originmanifestmaster.getReceiptinformation();
  144 + excelTitle[26] = originmanifestmaster.getSpecificConsigneePhone();
  145 + excelTitle[27] = originmanifestmaster.getConsigneePhone();
  146 + excelTitle[28] = originmanifestmaster.getStatus();
  147 + excelTitle[29] = originmanifestmaster.getIsbatch();
  148 + excelTitle[30] = originmanifestmaster.getOriginatingstationBill();
  149 + excelTitle[31] = originmanifestmaster.getDestinationstationBill();
  150 + excelTitle[32] = originmanifestmaster.getReportorder();
  151 + excelTitle[33] = originmanifestmaster.getIslast();
  152 + excelTitle[34] = originmanifestmaster.getShipperCode();
  153 + excelTitle[35] = originmanifestmaster.getShipperCountrycode();
  154 + excelTitle[36] = originmanifestmaster.getShipperPhone();
  155 + excelTitle[37] = originmanifestmaster.getShipperFax();
  156 + excelTitle[38] = originmanifestmaster.getConsigneeCode();
  157 + excelTitle[39] = originmanifestmaster.getConsigneeCountrycode();
  158 + for (int j = 0; j < excelTitle.length; j++){
  159 + row.createCell(j).setCellValue(excelTitle[j]);
  160 + }
  161 + }
  162 +
  163 + // 设置文件名
  164 + String title ="export.xls";
  165 + try {
  166 + String fileName = new String(title.getBytes(StandardCharsets.UTF_8), "ISO-8859-1");
  167 + fileName = URLEncoder.encode(fileName,"utf-8");
  168 + response.setContentType("application/vnd.ms-excel;charset=utf-8");
  169 + response.setCharacterEncoding("utf-8");
  170 + //默认Excel名称
  171 + response.setHeader("Content-Disposition", "attachment;filename="+fileName+";"+"filename*=utf-8''"+fileName);
  172 + } catch (UnsupportedEncodingException e) {
  173 + e.printStackTrace();
  174 + }
  175 +
  176 + try{
  177 + OutputStream os = response.getOutputStream();
  178 + workbook.write(os);
  179 + os.flush();
  180 + os.close();
  181 + log.info("导出完毕");
  182 + }catch (IOException e) {
  183 + e.printStackTrace();
  184 + }
  185 + }
34 186
35 - return new ResultJson("200","success",list); 187 + public void export(List<ORIGINMANIFESTMASTER> list,HttpServletResponse response){
  188 + String[] header = {
  189 + "运单号",
  190 + "航空托运人名称",
  191 + "航空销售代理人名称",
  192 + "航协编号",
  193 + "航空公司运输证明",
  194 + "运输条件鉴定书",
  195 + "承运人",
  196 + "航班号",
  197 + "航班日期",
  198 + "航班起始站",
  199 + "航班目的站",
  200 + "运单件数",
  201 + "运单重量",
  202 + "操作时间",
  203 + "关区",
  204 + "预配品名",
  205 + "收货人公司",
  206 + "收货人地址",
  207 + "收货人城市",
  208 + "收货人电话",
  209 + "收货人国家",
  210 + "收货人名称",
  211 + "发货人名称",
  212 + "发货人地址",
  213 + "发货人城市",
  214 + "发货人国家",
  215 + "发货人电话",
  216 + "发货人名称",
  217 + "海关回执状态",
  218 + "海关回执信息",
  219 + "发货人编码",
  220 + "收货人编码",
  221 + "货物品名",
  222 + "二级类名称",
  223 + "一级类名称",
  224 + "代理人三字码",
  225 + "代理人全称",
  226 + "代理人类别",
  227 + "代理联系人",
  228 + "代理联系人电话"
  229 + };
  230 + String[] columns = {
  231 + "autoid",
  232 + "waybillnomaster",
  233 + "segment",
  234 + "originatingstation",
  235 + "destinationstation",
  236 + "totalweight",
  237 + "totalpiece",
  238 + "manifesttotalpiece",
  239 + "manifesttotalweight",
  240 + "flightno",
  241 + "productname",
  242 + "customsstatus",
  243 + "carrier1",
  244 + "arrivalstation1",
  245 + "carrier2",
  246 + "arrivalstation2",
  247 + "carrier3",
  248 + "arrivalstation3",
  249 + "paymode",
  250 + "customscode",
  251 + "specialgoodscode",
  252 + "shippername",
  253 + "shipperaddress",
  254 + "consigneename",
  255 + "consigneeaddress",
  256 + "receiptinformation",
  257 + "specificConsigneePhone",
  258 + "consigneePhone",
  259 + "status",
  260 + "isbatch",
  261 + "originatingstationBill",
  262 + "destinationstationBill",
  263 + "reportorder",
  264 + "islast",
  265 + "shipperCode",
  266 + "shipperCountrycode",
  267 + "shipperPhone",
  268 + "shipperFax",
  269 + "consigneeCode",
  270 + "consigneeCountrycode"
  271 + };
  272 + ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
  273 + try{
  274 + exportExcelUtil.export("export.xls","export",header,columns,list,response);
  275 + }catch (Exception e){
  276 + e.printStackTrace();
  277 + }
36 } 278 }
37 } 279 }
@@ -27,11 +27,14 @@ public class NmmsAnalysisController { @@ -27,11 +27,14 @@ public class NmmsAnalysisController {
27 */ 27 */
28 @GetMapping("/analysis") 28 @GetMapping("/analysis")
29 public ResultJson analysis(@RequestParam(value = "startdate",required = true) String startdate, 29 public ResultJson analysis(@RequestParam(value = "startdate",required = true) String startdate,
30 - @RequestParam(value = "enddate",required = true) String enddate){ 30 + @RequestParam(value = "enddate",required = true) String enddate,
  31 + @RequestParam(value = "flightno",required = false) String flightno
  32 + ){
31 33
32 Map map = new HashMap(); 34 Map map = new HashMap();
33 map.put("startdate",startdate); 35 map.put("startdate",startdate);
34 map.put("enddate",enddate); 36 map.put("enddate",enddate);
  37 + map.put("flightno",flightno);
35 List<ORIGINMANIFESTMASTER> list = mapper.analysis(map); 38 List<ORIGINMANIFESTMASTER> list = mapper.analysis(map);
36 39
37 return new ResultJson("200","success",list); 40 return new ResultJson("200","success",list);
@@ -50,7 +50,7 @@ SELECT @@ -50,7 +50,7 @@ SELECT
50 SD.SD_WAYBILL "运单号", 50 SD.SD_WAYBILL "运单号",
51 SD.SD_CARGO_SHIPPER_NAME "航空托运人名称" , 51 SD.SD_CARGO_SHIPPER_NAME "航空托运人名称" ,
52 SD.SD_CARGO_AGENT_NAME "航空销售代理人名称", 52 SD.SD_CARGO_AGENT_NAME "航空销售代理人名称",
53 - SD.SD_IATA_NUMBER "航协编号 ", 53 + SD.SD_IATA_NUMBER "航协编号",
54 SD.SD_TRANSPORT_CERTIFICATE "航空公司运输证明", 54 SD.SD_TRANSPORT_CERTIFICATE "航空公司运输证明",
55 SD.SD_TRANSPORTATION_PROVE "运输条件鉴定书", 55 SD.SD_TRANSPORTATION_PROVE "运输条件鉴定书",
56 MF.CARRIER "承运人", 56 MF.CARRIER "承运人",
@@ -144,6 +144,9 @@ LEFT JOIN @@ -144,6 +144,9 @@ LEFT JOIN
144 WHERE 144 WHERE
145 trunc( T.FLIGHT_DATE ) BETWEEN to_date( #{startdate,jdbcType=VARCHAR}, 'yyyy-mm-dd' ) 145 trunc( T.FLIGHT_DATE ) BETWEEN to_date( #{startdate,jdbcType=VARCHAR}, 'yyyy-mm-dd' )
146 AND to_date(#{enddate,jdbcType=VARCHAR},'yyyy-mm-dd') 146 AND to_date(#{enddate,jdbcType=VARCHAR},'yyyy-mm-dd')
  147 + <if test="flightno != null" >
  148 + AND T.FLIGHTNO = #{flightno,jdbcType=VARCHAR}
  149 + </if>
147 ORDER BY T.FLIGHT_DATE,T.FLIGHTNO,WAYBILLNOMASTER,WAYBILLNOSECONDARY DESC 150 ORDER BY T.FLIGHT_DATE,T.FLIGHTNO,WAYBILLNOMASTER,WAYBILLNOSECONDARY DESC
148 151
149 </select> 152 </select>