springboot+easyexcel+⽣成并导出⼆维码图⽚导出包含⼆维码图⽚的e x ce l
新建springboot⼯程,包结构如下:
结构
slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @description
* @since 2020-11-06 15:11
*/
@Controller
@RequestMapping("/export")
@Slf4j
public class ExportController {springboot结构
// 要转化成⼆维码的url前缀
@Value("${qrcode.img.urlContent}")
private String urlContentPrefix;
// ⼆维码图⽚磁盘存放地址
@Value("${qrcode.img.baseDir}")
private String qrCodeImgBaseDir;
// ⼆维码图⽚集合,存放每次导出时⽣成的⼆维码图⽚
private List<File> qRCodeImgList = new ArrayList<>();
/**
* @description 导出带⼆维码图⽚的excel
* @author liujy
* @date 2020/11/6 15:35
*/
@GetMapping(value = "/e")
public void export(final HttpServletResponse response) {
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
List<PersonBean> beanList = mock();
if (!CollectionUtils.isEmpty(beanList)) {
beanList.forEach(bean -> {
// 将指定url转为⼆维码图⽚
String qrCodeImgPath = ateQrCode(urlContentPrefix + Name(), qrCodeImgBaseDir, Name() + ".jpg");                    File file = new File(qrCodeImgPath);
bean.setFile(file);
// 将图⽚放进⼆维码图⽚集合
qRCodeImgList.add(file);
});
}
final ClassPathResource resource = new ClassPathResource("template/person.xlsx");
EasyExcelFactory.OutputStream(), PersonBean.class)
.InputStream())
.sheet("设备编码")
.doWrite(beanList);
} catch (IOException e) {
response.setContentType("application/json");
<("导出异常");
<("" + e);
response.setCharacterEncoding("utf-8");
} finally {
// 删除磁盘中的⽂件
qRCodeImgList.forEach(img -> {
img.delete();
log.info("删除⼆维码图⽚成功:" + img);
});
qRCodeImgList.clear();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。