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

posted on 2020-06-29 16:57  疯狂的小萝卜头  阅读(3655)  评论(1编辑  收藏  举报