了解springPOI导出excel
话不多,放码过来
application.yml配置文件
zipfilepath:
#path: /export/data/www/zip
path: E:\2345Downloads
http: http://devh5.warehouseadmin.chujiayoupin.com/zip
配置zipFilePath
package com.warehouse.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Component
@Primary
@ConfigurationProperties(prefix = "zipfilepath")//貌似zipfilepath这里只能小写
@Data
public class zipFilePath {
private String path;
private String http;
}
主要代码:
@Autowired
private zipFilePath zipFilPath;
/**
* 批量导出分拣单
* @param request
* @param result
* @return
*/
@CrossOrigin(origins = "*", maxAge = 3600)//跨域标签
@ApiOperation(nickname = "batchExportPickMenu", value = "批量导出分拣单", httpMethod = "POST")
@PostMapping(value = "/batchExportPickMenu", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseData batchExportPickMenu(@Valid @RequestBody UpdatePickMemuRequest request,HttpServletResponse response, BindingResult result)throws IOException {
String url= null;
try{
//先创建生成excel目录
File ff = new File(zipFilPath.getPath());
if (!ff.exists()) {
ff.mkdirs();
}
ArrayList<File> files = new ArrayList<>();
List<Long> ids = request.getIds();
for (Long id: ids)
{
//商品详细信息列表
List<SortingDetailListResponse> sortDetListRep = new ArrayList<>();
//根据id查询主分拣单
SortingList sortingList = sortingListService.selectById(id);
//根据id查询分拣详细单列表信息
HashMap<String , Object> paraMap = new HashMap<>();
paraMap.put("slist_id",id);
List<SortingListDetail> sortingListDetailList = sortingListDetailService.selectByMap(paraMap);
System.out.println("sortingListDetailList="+sortingListDetailList);
int productQuantity = 0;
int coloneNum = 0;
List<Integer> coloneIdsList = new ArrayList<>();
//循环获取到线路下的分拣详细单下的specId(规格id)
for (SortingListDetail sortingListDetail : sortingListDetailList) {
productQuantity = addOperation(sortingListDetail,productQuantity,sortDetListRep);
//团长数
HashMap<String , Object> coloneMap = new HashMap<>();
coloneMap.put("slist_detail_id",sortingListDetail.getId());
List<ColoneSortingListDetail> coloneSortingListDetailList = coloneSortingListDetailService.selectByMap(coloneMap);
System.out.println("coloneSortingListDetailList="+coloneSortingListDetailList);
if(coloneSortingListDetailList != null && !coloneSortingListDetailList.isEmpty()){
for (ColoneSortingListDetail coloneSortListDetail:coloneSortingListDetailList) {
coloneIdsList.add(coloneSortListDetail.getColonelId());
}
}
}
//根据团长id进行去重
List coloneList = removeDuplicate(coloneIdsList);
SinglePrintSortingList singlePrintSortingList = new SinglePrintSortingList();
singlePrintSortingList.setSortDetRespList(sortDetListRep);
singlePrintSortingList.setEndTime(getCurrentTime());
singlePrintSortingList.setSortingListId(id);
singlePrintSortingList.setSortingListRoute(sortingList.getName());
singlePrintSortingList.setProductQuantity(productQuantity);
singlePrintSortingList.setColoneNum(coloneList.size());
System.out.println("singlePrintSortingList---"+singlePrintSortingList);
//只才是主要的代码,以上是我copy文杰同志代码拿数据
//生成Excel表格对象
HSSFWorkbook workbook = new HSSFWorkbook();
//创建一个sheet对象
HSSFSheet sheet = workbook.createSheet(sortingList.getLine());
//获取单元格样式对象
HSSFCellStyle cellStyle = setBorderHssfCellStyle(workbook);
// 设置列的宽度(第3,6列)
sheet.setColumnWidth(2, 20 * 256);
sheet.setColumnWidth(5, 20 * 256);
//Excel头部信息
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
String SortingName = sortingList.getName();
cell.setCellValue("分拣单线路:" + SortingName);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));//合并单元格
SetExcelHeaderCellStyle(workbook, cell);
//以下也是合并单元格
SetExcelHeaderInfoHssfCell(sheet, 1, sortingList.getId() + "", "单号:", 5);
SetExcelHeaderInfoHssfCell(sheet, 2, singlePrintSortingList.getProductQuantity() + "", "货品总数:", 5);
SetExcelHeaderInfoHssfCell(sheet, 3, singlePrintSortingList.getColoneNum() + "", "团长数:", 5);
SetHssCellTime(workbook, sheet, 4, 5);//设置导出时间
//表头信息
String[] headers = {"序号","商品编码","商品名称","规格","数量","备注"};
HSSFRow row5 = sheet.createRow(5);
//设置行高
row5.setHeightInPoints(18);
for (int i = 0; i < headers.length; i++) {
HSSFCell cell6 = row5.createCell(i);
SetHssfCellStyle(workbook, cell6);
cell6.setCellValue(headers[i]);
}
//循环输出商品信息
int a = 5;//单元格递增变量
int b = 0;//序号递增变量
for (SortingListDetail so:sortingListDetailList) {
a++;
b++;
HSSFRow row6 = sheet.createRow(a);
setProductInfo(workbook,row6,0,b+"");//序号
setProductInfo(workbook,row6,1,so.getMerchantCode());//商品编码
setProductInfo(workbook,row6,2,so.getProductName());//商品名称
setProductInfo(workbook,row6,3,so.getSpec()+"");//规格
setProductInfo(workbook,row6,4,so.getTotalNum()+"");//数量
setProductInfo(workbook,row6,5,"");//备注不设值,客户填写
}
SetRow(workbook,sheet, a, 1, "仓库交接人:");
SetRow(workbook,sheet, a, 2, "司机:");
SetRow(workbook,sheet, a, 3, "交接时间:");
//excel下载名字
String filename=sortingList.getName()+".xls";
//指定目录创建Excel
FileOutputStream fileOutputStream = new FileOutputStream(zipFilPath.getPath() + "/" + filename);
workbook.write(fileOutputStream);
File file = new File(zipFilPath.getPath() + "/" + filename);
//压缩文件列表集合
files.add(file);
workbook.close();
}
//压缩包存放路径
FileOutputStream fileOutputStream = new FileOutputStream(zipFilPath.getPath()+"/"+"分拣单"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+ ".zip");
ZipUtils.toZip(files,fileOutputStream);
url =zipFilPath.getHttp()+"/"+"分拣单"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+ ".zip";
System.out.println("压缩包地址:"+url);
if (result.hasErrors()) {
return new ResponseData<>(ResponseCodeEnum.ERROR_NOT_FOUND.getValue(), result.getFieldError().getDefaultMessage());
}
}catch (Exception e){
e.printStackTrace();
}
return ResponseData.build(ResponseCodeEnum.SUCCESS.getValue(), ResponseCodeEnum.SUCCESS.getMessage(),url);
}
/**
* 设置产品信息输出
* @param workbook
* @param row
* @param no
* @param info
*/
private void setProductInfo(HSSFWorkbook workbook, HSSFRow row, int no, String info){
HSSFCell cell = row.createCell(no);
row.setHeightInPoints(15);//设置行高
cell.setCellStyle(setBorderHssfCellStyle(workbook));//设置单元格边框
cell.setCellValue(info);
}
/**
* 设置单元格上下左右边框,并居中显示
* @param workbook
* @return
*/
private HSSFCellStyle setBorderHssfCellStyle(HSSFWorkbook workbook) {
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
return cellStyle;
}
/**
* 设置打印时间,并格式化时间样式 例如:2019/1/9 18:21:37
* @param workbook
* @param sheet
* @param i2
* @param i3
*/
private void SetHssCellTime(HSSFWorkbook workbook, HSSFSheet sheet, int i2, int i3) {
HSSFRow row4 = sheet.createRow(i2);
HSSFCell cell5 = row4.createCell(0);
cell5.setCellValue("打印时间:");
HSSFCell cell4 = row4.createCell(1);
cell4.setCellValue(new Date());
HSSFCellStyle cellStyle1 = workbook.createCellStyle();
cellStyle1.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
cell4.setCellStyle(cellStyle1);
sheet.addMergedRegion(new CellRangeAddress(i2, i2, 1, i3));
}
/**
* 设置页眉信息
* @param sheet
* @param i2 行数
* @param line 数据名
* @param s 数据源
* @param i3 每行的单元格数-1
*/
private void SetExcelHeaderInfoHssfCell(HSSFSheet sheet, int i2, String line, String s, int i3) {
HSSFRow row = sheet.createRow(i2);
HSSFCell cell = row.createCell(0);
cell.setCellValue(s + line);
sheet.addMergedRegion(new CellRangeAddress(i2, i2, 0, i3));
}
/**
* 设置Excel页眉第一行样式
* @param workbook
* @param cell
*/
private void SetExcelHeaderCellStyle(HSSFWorkbook workbook, HSSFCell cell) {
HSSFFont font = workbook.createFont();//获取字体设置对象
font.setBold(true);//加粗
font.setFontHeightInPoints((short)20);//设置字号
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直居中
cell.setCellStyle(cellStyle);
}
/**
* 设置Excel页脚信息
* @param sheet
* @param a 自增变量
* @param i2 ++数
* @param s 输出的文字
*/
private void SetRow(HSSFWorkbook workbook,HSSFSheet sheet, int a, int i2, String s) {
HSSFRow row8 = sheet.createRow(a + i2);
row8.setHeightInPoints(30);
HSSFCell cell = row8.createCell(0);
cell.setCellValue(s);
sheet.addMergedRegion(new CellRangeAddress(a + i2, a + i2, 0, 5));
}
/**
* 设置Excel 表头<th>样式
* @param workbook
* @param cell
*/
private void SetHssfCellStyle(HSSFWorkbook workbook, HSSFCell cell) {
HSSFFont font1 = workbook.createFont();
font1.setBold(true);
HSSFCellStyle cellStyle2 = workbook.createCellStyle();
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
cellStyle2.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直居中
cellStyle2.setFont(font1);
cellStyle2.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
cellStyle2.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
cellStyle2.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
cellStyle2.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
cell.setCellStyle(cellStyle2);
}