1、Controller
@RequestMapping(value = "/pasteListExport", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
@ApiOperation(httpMethod = "POST", value = "贴标列表导出")
public void pasteListExport(@Valid @ApiParam @RequestBody DeliverListExcelQuery deliverListExcelQuery,
HttpServletRequest request, HttpServletResponse response) {
XSSFWorkbook workbook = null;
OutputStream out = null;
try {
//设置Title
Map<String , String> titleMap = new LinkedHashMap<String , String>();
titleMap.put("waybillNo","运单号");
titleMap.put("batchNo","发货批号");
titleMap.put("pasteContent","贴标备注");
titleMap.put("pastePieceNo","贴标件数");
titleMap.put("custNo","客户编号");
titleMap.put("customsTime","清关时间");
titleMap.put("warehousingTime","入库时间");
titleMap.put("warehousingBatchNo","入库批号");
titleMap.put("warehousingAccount","入库人");
titleMap.put("ladingbillNo","提单号");
titleMap.put("moveOutBatchNo","搬出批号");
titleMap.put("warehousingArea","库区");
titleMap.put("warehousingNo","库号");
List<Map<String , Object>> list = waybillWarehouseService.deliverListExport(request.getHeader("accountId"), deliverListExcelQuery);
workbook = POIUtil.createExcel(titleMap, list);
// 写入Excel文件到前端
if(null != workbook){
String fileName = "贴标列表导出.xlsx";
fileName = new String(fileName.getBytes("UTF-8"),"iso8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.setContentType("application/x-download");
response.setCharacterEncoding("UTF-8");
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
response.flushBuffer();
out = response.getOutputStream();
workbook.write(out);
out.flush();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
if (workbook != null) {
workbook.close();
}
if (out != null) {
out.close();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
2、Service层就是你从数据库中查出来的数据,根据自己实际开发业务来写
3、导出数据方法
public static XSSFWorkbook createExcel(Map<String , String> titleMap, List<Map<String,Object>> listresult){
// 1.创建HSSFWorkbook,一个HSSFWorkbook对应一个Excel文件
XSSFWorkbook wb = new XSSFWorkbook();
// 2.在workbook中添加一个sheet,对应Excel文件中的sheet
XSSFSheet sheet = wb.createSheet("sheet1");
// 3.设置表头,即每个列的列名
// 3.1创建第一行
XSSFRow row = sheet.createRow(0);
List<String> titleList = new ArrayList<String>();
// 给列写入数据,创建单元格,写入数据 创建标题
int title = 0;
for(Map.Entry<String, String> entry : titleMap.entrySet()){
titleList.add(entry.getKey());
row.createCell(title).setCellValue(entry.getValue());
title++;
}
// 写入正式数据
for (int i = 0; i < listresult.size(); i++) {
// 创建行
row = sheet.createRow(i+1);
//列当中写入数据
for(int j = 0; j < titleList.size(); j++) {
String key = titleList.get(j);
String val = CommUtil.toString(listresult.get(i).get(key));
row.createCell(j).setCellValue(val);
}
}
/**
* 上面的操作已经是生成一个完整的文件了,只需要将生成的流转换成文件即可;
* 下面的设置宽度可有可无,对整体影响不大
*/
// 设置单元格宽度
int curColWidth = 0;
for (int i = 0; i <= titleList.size(); i++) {
// 列自适应宽度,对于中文半角不友好,如果列内包含中文需要对包含中文的重新设置。
sheet.autoSizeColumn(i, true);
// 为每一列设置一个最小值,方便中文显示
curColWidth = sheet.getColumnWidth(i);
if(curColWidth<3000){
sheet.setColumnWidth(i, 3000);
}
}
return wb;
}