java後端實現Excel數據的導出、解決導出數據會在火狐瀏覽器亂碼的問題

@RestController
public class ExcelDownLoadController {

	@Resource
	private AddCustomerService addCustomerService;

@ApiOperation(value = "新增客戶數據", notes = "導出時間範圍內新增客戶數據")
	@ApiImplicitParams({
	    @ApiImplicitParam(name = "startTime",paramType = "query", value = "開始時間", required = true, dataType = "Date"),
		@ApiImplicitParam(name = "endTime",paramType = "query", value = "結束時間", required = true, dataType = "Date")
	})
	@RequestMapping(value = "/api/fm/addcustomer", method = RequestMethod.GET)
	public void addCustomer(HttpServletResponse response,@RequestParam @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date startTime,@RequestParam @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date endTime) throws IOException {
		 
		List<HashMap<String, Object>> userinfos = addCustomerService.getNewUserInfo(startTime, endTime);
		List<HashMap<String, Object>> merchantinfos = addCustomerService.getNewMerchantInfo(startTime, endTime);
		
		Integer index = 1;
		InputStream is = XlsDownLoadController.class.getClassLoader().getResourceAsStream("export/新增客戶數據.xls");	
		Workbook workbook = new HSSFWorkbook(is); 
		Sheet sheetOne = workbook.getSheet("往來單位");
		Sheet sheetTwo = workbook.getSheet("聯繫方式");
		
		for (HashMap<String, Object> userinfo : userinfos) {
			Row row = sheetOne.createRow(index);
			row.createCell(0).setCellValue(getCellValue(userinfo,"user_id"));
			row.createCell(1).setCellValue(getCellValue(userinfo,"real_name"));
			row.createCell(3).setCellValue("客戶");
			row.createCell(4).setCellValue("1");
			 
			row = sheetTwo.createRow(index);
			row.createCell(0).setCellValue(getCellValue(userinfo,"user_id"));
			row.createCell(1).setCellValue(getCellValue(userinfo,"real_name"));
			row.createCell(8).setCellValue(getCellValue(userinfo,"mobile"));
			index++;
		}

		for (HashMap<String, Object> merchantinfo : merchantinfos) {
			Row row = sheetOne.createRow(index);
			row.createCell(0).setCellValue(getCellValue(merchantinfo,"merchant_no"));
			row.createCell(1).setCellValue(getCellValue(merchantinfo,"merchant_name"));
			row.createCell(3).setCellValue("客戶");
			row.createCell(4).setCellValue("1");
			row = sheetTwo.createRow(index);
			row.createCell(0).setCellValue(getCellValue(merchantinfo,"merchant_no"));
			row.createCell(1).setCellValue(getCellValue(merchantinfo,"merchant_name"));
			row.createCell(8).setCellValue(getCellValue(merchantinfo,"mobile"));
			index++;
		}
		
		String fileName = "("+sdf.format(startTime)+"~"+sdf.format(endTime)+").xls";
		//解決火狐瀏覽器亂碼的問題:Header中填寫這個Content-Disposition很關鍵
		response.setHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'"  + URLEncoder.encode("新增客戶數據", "UTF-8")+fileName);
		workbook.write(response.getOutputStream());
		workbook.close();
		response.getOutputStream().close();
	}





    private String getCellValue(HashMap<String, Object> map,String key) {
        if(null==map.get(key)) {
            return "";
        }
        if(map.get(key).getClass().equals(String.class)) {
            return map.get(key).toString();
        }

        if(map.get(key).getClass().equals(Integer.class)) {
            Integer value = (Integer)map.get(key);
            return value.toString();
        }
        if(map.get(key).getClass().equals(Long.class)) {
            Long value = (Long)map.get(key);
            return value.toString();
        }
        if(map.get(key).getClass().equals(Integer.class)) {
            Double value = (Double)map.get(key);
            DecimalFormat df= new DecimalFormat("######0.00");
            return df.format(value);
        }
        if(map.get(key).getClass().equals(Date.class)) {
            Date value = (Date)map.get(key);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            return df.format(value);
        }


        return map.get(key).toString();
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章