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());}}
?