|
|
package com.sunyo.wlpt.cgonms.arrival.excle;
|
|
|
|
|
|
|
|
|
import com.sunyo.wlpt.cgonms.arrival.domain.ResultArrivalData;
|
|
|
import com.sunyo.wlpt.cgonms.arrival.response.ResultJson;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.poi.hssf.usermodel.*;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.File;
|
|
|
import java.io.FileNotFoundException;
|
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* @author 子诚
|
|
|
* Description:导入Excel到静态资源文件夹excel(与src同级)
|
|
|
* 时间:2020/5/29 10:04
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@RestController
|
|
|
@RequestMapping("/arrival")
|
|
|
public class ExitExcel {
|
|
|
|
|
|
@Value("${path.dir}")
|
|
|
private String dir;
|
|
|
|
|
|
@PostMapping("/excel")
|
|
|
public ResultJson getExit(@RequestBody List<ResultArrivalData> exitInfoList, HttpServletResponse httpServletResponse) {
|
|
|
ResultJson result = new ResultJson();
|
|
|
String title = exportExcel(exitInfoList, httpServletResponse);
|
|
|
result.setMsg(title);
|
|
|
result.setCode("200");
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
private String exportExcel(List<ResultArrivalData> exitInfoList, HttpServletResponse response) {
|
|
|
// 设置文件名
|
|
|
String title = "";
|
|
|
try {
|
|
|
// 创建工作簿
|
|
|
HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
// 创建表
|
|
|
HSSFSheet sheet = workbook.createSheet("export");
|
|
|
// 创建行
|
|
|
HSSFRow row = sheet.createRow(0);
|
|
|
// 创建单元格样式
|
|
|
HSSFCellStyle cellStyle = workbook.createCellStyle();
|
|
|
// 表头
|
|
|
String[] head = {
|
|
|
"运单号",
|
|
|
"件数",
|
|
|
"重量",
|
|
|
"计费重量",
|
|
|
"体积",
|
|
|
"品名",
|
|
|
"二级品类",
|
|
|
"一级品类",
|
|
|
"货物目的站",
|
|
|
"货物目的国家/地区",
|
|
|
"所属洲",
|
|
|
"航空公司",
|
|
|
"航班号",
|
|
|
"航班日期",
|
|
|
"航班时间",
|
|
|
"航班计划日期",
|
|
|
"航班计划时间",
|
|
|
"航段",
|
|
|
"机号",
|
|
|
"机型",
|
|
|
"最大业载",
|
|
|
"运单发货人",
|
|
|
"运单收货人",
|
|
|
"订舱代理人",
|
|
|
"结算代理人",
|
|
|
"自定义项"
|
|
|
};
|
|
|
HSSFCell cell;
|
|
|
// 设置表头
|
|
|
for (Integer i = 0; i < head.length; i++) {
|
|
|
cell = row.createCell(i);
|
|
|
cell.setCellValue(head[i]);
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
// 设置单元格宽度
|
|
|
// 256*width+184
|
|
|
sheet.setColumnWidth(i, 4000);
|
|
|
if (i == 14 || i == 16) {
|
|
|
sheet.setColumnWidth(i, 6000);
|
|
|
}
|
|
|
if (i == 21 || i == 22) {
|
|
|
sheet.setColumnWidth(i, 10000);
|
|
|
}
|
|
|
}
|
|
|
// 设置表格内容
|
|
|
// for (Integer i = 0; i < exitInfoList.size(); i++) {
|
|
|
// row = sheet.createRow(i + 1);
|
|
|
// ResultArrivalData resultExitData = exitInfoList.get(i);
|
|
|
// // 这里是内容设置,替换则自己的数据即可
|
|
|
// String[] excelTitle = new String[27];
|
|
|
//
|
|
|
// //取出数据,运单号
|
|
|
// excelTitle[0] = resultExitData.getWaybillNoMaster();
|
|
|
// //件数
|
|
|
// excelTitle[1] = resultExitData.getTallyTotalPiece();
|
|
|
// //重量
|
|
|
// excelTitle[2] = resultExitData.getTallyTotalWeight();
|
|
|
// //计费重量
|
|
|
// excelTitle[3] = String.valueOf(resultExitData.getTotalGrossWeightMeasure());
|
|
|
// //体积
|
|
|
// excelTitle[4] = String.valueOf(resultExitData.getVolumeMeasure());
|
|
|
// //品名
|
|
|
// excelTitle[5] = resultExitData.getSdCargoName();
|
|
|
// //二级品类
|
|
|
// excelTitle[6] = resultExitData.getTwoTypeName();
|
|
|
// //一级品类
|
|
|
// excelTitle[7] = resultExitData.getTypeName();
|
|
|
// //货物目的站
|
|
|
// excelTitle[8] = resultExitData.getAimStation();
|
|
|
// //货物目的国家/地区
|
|
|
// excelTitle[9] = resultExitData.getCountry();
|
|
|
// //所属洲
|
|
|
// excelTitle[10] = resultExitData.getAreaDescChn();
|
|
|
// //航空公司
|
|
|
// excelTitle[11] = resultExitData.getAirCompany();
|
|
|
// //航班号
|
|
|
// excelTitle[12] = resultExitData.getFlightNo();
|
|
|
// //航班日期
|
|
|
// excelTitle[13] = isNullShort(resultExitData.getFlightDate());
|
|
|
// //航班时间
|
|
|
// excelTitle[14] = isNullLong(resultExitData.getFlightTime());
|
|
|
// //航班计划日期
|
|
|
// excelTitle[15] = isNullShort(resultExitData.getFlightPlanDate());
|
|
|
// //航班计划时间
|
|
|
// excelTitle[16] = isNullLong(resultExitData.getFlightPlanTime());
|
|
|
// //航段
|
|
|
// excelTitle[17] = resultExitData.getSegment();
|
|
|
// //机号
|
|
|
// excelTitle[18] = resultExitData.getCfNo();
|
|
|
// //机型
|
|
|
// excelTitle[19] = resultExitData.getCfTp();
|
|
|
// //最大业载
|
|
|
// excelTitle[20] = "";
|
|
|
// //运单发货人
|
|
|
// excelTitle[21] = resultExitData.getShipperName();
|
|
|
// //运单收货人
|
|
|
// excelTitle[22] = resultExitData.getConsigneeName();
|
|
|
// //订舱代理人(代理人全称)
|
|
|
// excelTitle[23] = resultExitData.getOrderName();
|
|
|
// //结算代理人(代理人全程)
|
|
|
// excelTitle[24] = resultExitData.getCountName();
|
|
|
// //自定义项
|
|
|
// excelTitle[25] = "";
|
|
|
//
|
|
|
// for (Integer j = 0; j < excelTitle.length; j++) {
|
|
|
// row.createCell(j).setCellValue(excelTitle[j]);
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
String file_name = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
// 设置文件名
|
|
|
title = file_name + ".xls";
|
|
|
|
|
|
String filePath = dir + "/" + title;
|
|
|
/**
|
|
|
* 创建文件夹
|
|
|
*/
|
|
|
File testFile = new File(filePath);
|
|
|
File fileParent = testFile.getParentFile();
|
|
|
if (!fileParent.exists()) {
|
|
|
// 能创建多级目录
|
|
|
fileParent.mkdirs();
|
|
|
}
|
|
|
try {
|
|
|
FileOutputStream fos = new FileOutputStream(filePath);
|
|
|
workbook.write(fos);
|
|
|
fos.flush();
|
|
|
fos.close();
|
|
|
} catch (FileNotFoundException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return title;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 时间戳转换成字符串
|
|
|
*/
|
|
|
public static String getDateToLong(long time) {
|
|
|
Date d = new Date(time);
|
|
|
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
return sf.format(d);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 时间戳转换成字符串
|
|
|
*/
|
|
|
public static String getDateToShort(long time) {
|
|
|
Date d = new Date(time);
|
|
|
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
return sf.format(d);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 判断传递来的时间,是否为空,为null,则返回"",不为空,则规范格式 "yyyy-MM-dd"
|
|
|
*
|
|
|
* @param time Date类型时间
|
|
|
* @return
|
|
|
*/
|
|
|
public static String isNullShort(Date time) {
|
|
|
if (null == time) {
|
|
|
return "";
|
|
|
} else {
|
|
|
return getDateToShort(time.getTime());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 判断传递来的时间,是否为空,为null,则返回"",不为空,则规范格式 "yyyy-MM-dd HH:mm:ss"
|
|
|
*
|
|
|
* @param time Date类型时间
|
|
|
* @return
|
|
|
*/
|
|
|
public static String isNullLong(Date time) {
|
|
|
if (null == time) {
|
|
|
return "";
|
|
|
} else {
|
|
|
return getDateToLong(time.getTime());
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|