java 利用autopoi進行xls文件(帶圖片附件)導入導出


先看效果:
在這里插入圖片描述在這里插入圖片描述

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM