1.添加依赖
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version></dependency>
2.POI工具类
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;public class PoiUtils {/*** 导出Excel* @param sheetName sheet名称* @param title 标题* @param values 内容* @param wb HSSFWorkbook对象* @return*/public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,String [][]values, HSSFWorkbook wb){// 第一步,创建一个HSSFWorkbook,对应一个Excel文件if(wb == null){wb = new HSSFWorkbook();}// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheetHSSFSheet sheet = wb.createSheet(sheetName);// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制HSSFRow row = sheet.createRow(0);// 第四步,创建单元格,并设置值表头 设置表头居中HSSFCellStyle style = wb.createCellStyle();
// style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式//声明列对象HSSFCell cell = null;//创建标题for(int i=0;i<title.length;i++){cell = row.createCell(i);cell.setCellValue(title[i]);cell.setCellStyle(style);}//创建内容for(int i=0;i<values.length;i++){row = sheet.createRow(i + 1);for(int j=0;j<values[i].length;j++){//将内容按顺序赋给对应的列对象row.createCell(j).setCellValue(values[i][j]);}}return wb;}//发送响应流方法public static void setResponseHeader(HttpServletResponse response, String fileName) {try {try {fileName = new String(fileName.getBytes(),"ISO8859-1");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}response.setContentType("application/octet-stream;charset=ISO8859-1");response.setHeader("Content-Disposition", "attachment;filename="+ fileName);response.addHeader("Pargam", "no-cache");response.addHeader("Cache-Control", "no-cache");} catch (Exception ex) {ex.printStackTrace();}}
}
3.导出
@GetMapping("/export")@ApiOperation(value = "导出设备清单信息接口", notes = "导出")public void export(@RequestParam(value = "page", required = true, defaultValue = "0") @ApiParam(required = true, defaultValue = "0", value = "当前页数,默认从0开始") Integer page,@RequestParam(value = "limit", required = true, defaultValue = "10") @ApiParam(required = true, defaultValue = "10", value = "每一页条数,默认10条") Integer limit,@RequestParam(value = "pkId", required = false) @ApiParam(required = false, value = "车场名称") String pkId, HttpServletResponse response){try {log.info("++++++++++访问/parkDevice/export接口:>>>>>>pkId:" + pkId );Page<ParkDeviceVo> requestPage = new Page<>(page, limit);Map<String, Object> requestParam = new HashMap<String, Object>();requestParam.put("pkId", pkId);Page<ParkDeviceVo> parkDeviceVoPage = parkDeviceService.getParkDevoceByPage(requestPage, requestParam);List<ParkDeviceVo> parkDeviceVoList = parkDeviceVoPage.getRecords();//excel标题String[] title = {"设备类型","设备厂家","设备型号","设备数量","备注","添加时间"};//excel文件名String fileName = "设备清单信息"+System.currentTimeMillis()+".xls";//sheet名String sheetName = "设备清单信息表";String[][] content = new String[parkDeviceVoList.size()][title.length];for(int i = 0; i < parkDeviceVoList.size();i++){//设备类型if (parkDeviceVoList.get(i).getDeviceType()!=null){//设备类型(1.相机 2.道闸 3.显示屏 4.其他)int type =parkDeviceVoList.get(i).getDeviceType();switch (type){case 1:content[i][0] = "相机";break;case 2:content[i][0] = "道闸";break;case 3:content[i][0] = "显示屏";break;case 4:content[i][0] = "其他";break;}}else {content[i][0] = "";}//设备厂家content[i][1] = parkDeviceVoList.get(i).getDeviceVender() == null ? "" :parkDeviceVoList.get(i).getDeviceVender();//设备型号content[i][2] = parkDeviceVoList.get(i).getDeviceNo() == null ? "" :parkDeviceVoList.get(i).getDeviceNo();//设备数量content[i][3] =parkDeviceVoList.get(i).getDeviceNum() == null ? "" :parkDeviceVoList.get(i).getDeviceNum().toString();//设备型号content[i][4] = parkDeviceVoList.get(i).getRemark() == null ? "" :parkDeviceVoList.get(i).getRemark();//设备型号SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");content[i][4] = parkDeviceVoList.get(i).getCreateTime() == null ? "" :formatter.format(parkDeviceVoList.get(i).getCreateTime());}//创建HSSFWorkbookHSSFWorkbook wb = PoiUtils.getHSSFWorkbook(sheetName, title, content, null);//响应到客户端PoiUtils.setResponseHeader(response, fileName);OutputStream os = response.getOutputStream();wb.write(os);os.flush();os.close();log.info("++++++++++访问/parkDevice/export接口调用成功>>>>>>");}catch (Exception e){log.error("++++++++++访问/parkDevice/export接口系统异常原因 : +++++++++" + e.getMessage());}}