先看效果:
maven依賴:
<!-- AutoPoi Excel工具類-->
<dependency>
<groupId>org.jeecgframework</groupId>
<artifactId>autopoi-web</artifactId>
</dependency>
<!-- AutoPoi Excel工具類-->
<dependency>
<groupId>org.jeecgframework</groupId>
<artifactId>autopoi</artifactId>
</dependency>
PmsSupplierParam.java
@Data
public class PmsSupplierParam implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "創建時間", hidden = true)
@Excel(name = "建檔時間", orderNum = "5", format = "yyyy-MM-dd HH:mm:ss", width = 25)
private Date createTime;
@ApiModelProperty(value = "供應商名稱", example = "供應商名稱")
@Excel(name = "供應商名稱", width = 40, orderNum = "0")
private String name;
@ApiModelProperty(value = "聯繫人", example = "聯繫人")
@Excel(name = "聯繫人", width = 20, orderNum = "1")
private String contact;
@ApiModelProperty(value = "聯繫人電話", example = "聯繫人電話")
@Excel(name = "聯繫人電話", width = 20, orderNum = "2")
private String contactPhone;
@ApiModelProperty(value = "備註", example = "備註")
@Excel(name = "備註", width = 60, orderNum = "4")
private String remark;
@ApiModelProperty(value = "詳細地址", example = "詳細地址")
@Excel(name = "單位地址", width = 40, orderNum = "3")
private String address;
}
Controller新增以下代碼:
@ApiOperation(value = "導出", produces = "application/octet-stream")
@RequestMapping(value = "/exportXls", method = RequestMethod.GET)
public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
List exportListData = getListData();
// 導出文件名稱
mv.addObject(NormalExcelConstants.FILE_NAME, "供應商信息");
// 註解對象Class
mv.addObject(NormalExcelConstants.CLASS, PmsSupplierParam.class);
// 自定義表格參數
mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("供應商信息", "供應商信息"));
// 導出數據列表
mv.addObject(NormalExcelConstants.DATA_LIST, exportListData);
return mv;
}
/**
* 生成假數據,真實開發中這裏數據一般來自數據庫
*
* @return
*/
@SuppressWarnings("unused")
private List getListData() {
List<PmsSupplierParam> list = new ArrayList<>();
for (int i = 0; i < 30; i++) {
PmsSupplierParam excelEntity = new PmsSupplierParam();
excelEntity.setName("名稱" + i);
excelEntity.setContact("聯繫人" + i);
excelEntity.setContactPhone("聯繫電話" + i);
excelEntity.setAddress("地址" + i);
excelEntity.setRemark("備註" + i);
list.add(excelEntity);
}
System.out.println(list.toString());
return list;
}
@ApiOperation(value = "導入")
@PostMapping("/importXls")
@ResponseBody
public String importExcel(MultipartFile myFileNames, Principal principal)
throws Exception {
ImportParams params = new ImportParams();
// 表格標題所在行,計數從0開始
params.setTitleRows(1);
// head頭部所在行,計數從0開始
params.setHeadRows(0);
// 表格sheet數量
// params.setSheetNum(9);
// 最好不要設置爲true,否則導入一次excel服務器會重啓一次,原因不明
// params.setNeedSave(false);
InputStream inputStream = myFileNames.getInputStream();
List<PmsSupplierParam> list = ExcelImportUtil.importExcel(inputStream, PmsSupplierParam.class, params);
return list.toString();
}
帶圖片附件的特殊說明:
@Excel(name = "圖片地址",type=2,imageType=4)
private String picture;
更多請求請參照:http://doc.jeecg.com/1524938