List<File> fileList = FileExportImportUtil.bufferImage(pictureList, picDir, nameList);
下面为完成代码
excel 导出学员证件照并以手机号和姓名
@RequestMapping("/userGroup/export/{groupId}")
public void userGroupInfoExport(HttpServletRequest request,
@PathVariable("groupId") Long groupId,
@ModelAttribute("studentGroupDto") StudentGroupDto studentGroupDto,
HttpServletResponse response) {
try {
// 指定文件生成路径
String dir = request.getSession().getServletContext().getRealPath("/excelfile/user");
String picDir = request.getSession().getServletContext().getRealPath("/certificate/picture");
// 文件名
String expName = "学员信息_" + DateUtils.getStringDateShort();
// 表头信息
String[] headName = {"学员ID", "真实姓名", "性别", "工作单位", "身份证号","省","市","县", "地址", "邮编", "电话", "邮箱", "学员职务", "行政级别", "民族", "学员分类", "学员单位性质", "学历", "项目名称", "组名", "期末考试成绩", "考试时间", "出生时间"};
// 拆分为一万条数据每Excel,防止内存使用太大
PageEntity page = new PageEntity();
page.setPageSize(10000);
studentGroupDto.setId(groupId);
page.setCurrentPage(1);
studentGroupService.getStudentGroupListPage(studentGroupDto, page);
int num = page.getTotalPageSize();// 总页数
List<File> srcfile = new ArrayList<File>();// 生成的excel的文件的list
for (int i = 1; i <= num; i++) {// 循环生成num个xls文件
page.setCurrentPage(i);
List<StudentGroupDto> studentGroupDtos2 = studentGroupService.getStudentGroupListPage(studentGroupDto, page);
List<List<String>> list = studentGroupDeal(studentGroupDtos2);
File file = FileExportImportUtil.createExcel(headName, list, expName + "_" + i, dir);
//学生的图片
List<String> pictureList = new ArrayList<>();
List<Long> userIdList = new ArrayList<>();
List<String> nameList = new LinkedList<>();
//存名字和手机号后四位的结合
Set<String> nameSet = new HashSet<>();
Set<String> sameNameSet = new HashSet<>();
if (ObjectUtils.isNotNull(studentGroupDto)) {
// 取出相同的姓名的集合
for (StudentGroupDto studentGroupDto1 : studentGroupDtos2) {
String realName = studentGroupDto1.getUserName();
if (StringUtils.isEmpty(realName)) {
continue;
}
System.out.println(nameSet.contains(realName));
if (!nameSet.contains(realName)) {
nameSet.add(realName.replace(" ", ""));
} else {
sameNameSet.add(realName);
}
}
for (StudentGroupDto studentGroupDto1 : studentGroupDtos2) {
Long userId = studentGroupDto1.getUserId();
//真实姓名
userIdList.add(userId);
if (StringUtils.isNotEmpty(String.valueOf(userId))) {
User user = userService.getUserById(userId);
if (StringUtils.isNotEmpty(String.valueOf(user.getCertificateId()))) {
UserCertificate userCertificate = userCertificateService.getUserCertificateById(user.getCertificateId());
if (ObjectUtils.isNotNull(userCertificate)) {
if (StringUtils.isNotEmpty(userCertificate.getIdentityPhoto())) {
// 导出证件照为姓名+手机号后四位
String realName = studentGroupDto1.getUserName();
if (StringUtils.isEmpty(realName)) {
continue;
}
if (!sameNameSet.contains(realName)) {
nameList.add(realName.replace(" ", ""));
// nameSet.add(realName);
} else {
// nameList.add(realName + userId);
if (StringUtils.isNotEmpty(studentGroupDto1.getMobile()) && studentGroupDto1.getMobile().length() == 11) {
nameList.add(realName + studentGroupDto1.getMobile().substring(7,11));
}
}
pictureList.add(CommonConstants.staticImageServer + userCertificate.getIdentityPhoto());
}
}
// else {
// pictureList.add(CommonConstants.contextPath + "/static/edu/images/zhengjian.jpg");
// }
}
}
}
}
srcfile.add(file);
//导出图片
List<File> fileList = FileExportImportUtil.bufferImage(pictureList, picDir, nameList);
if (ObjectUtils.isNotNull(fileList)) {
for (File file1 : fileList) {
srcfile.add(file1);
}
}
}
FileExportImportUtil.createRar(response, dir, srcfile, expName);// 生成的多excel的压缩包
} catch (Exception e) {
logger.error("StudentGroupController.userGroupInfoExport", e);
e.printStackTrace();
}
}
List<File> fileList = FileExportImportUtil.bufferImage(pictureList, picDir, nameList);
if (ObjectUtils.isNotNull(fileList)) {
for (File file1 : fileList) {
srcfile.add(file1);
}
}
导出png/jpeg格式的图片,如果png的不单独处理容易造成图片的模糊和失真
/**
* @Description:生成图片
*
* @Param: [picList, dir]
* @Return: java.io.File
*
*/
public static List<File> bufferImage(List<String> picList, String dir, List<String> nameList) throws Exception {
List<File> fileList = new ArrayList<>();
for (int i = 0; i < picList.size(); i++) {
File file2 = new File(picList.get(i).toString());
String tile = file2.getName();//获取图片名称
String picUrl = dir + "/" +i+ tile;
URL url = new URL(picList.get(i).toString());//声明url对象
URLConnection connection = url.openConnection();
String message = connection.getHeaderField(0);
boolean b = false;
if (StringUtils.isNotEmpty(message)) {
if (message.indexOf("200") > -1) {
b = true;
URL url1 = new URL(picList.get(i).toString());//声明url对象
System.out.println("url =======================" + picList.get(i).toString());
URLConnection connection1 = url1.openConnection();
connection1.setDoOutput(true);
BufferedImage src = ImageIO.read(connection1.getInputStream());
File file1 = new File(picUrl);
if (picUrl.contains(".png") || picUrl.contains(".PNG")) {
ImageIO.write(src, "PNG", file1);
} else {
ImageIO.write(src, "JPEG", file1);
}
String path = file1.getAbsolutePath();
for (int j = 0; j < nameList.size(); j++) {
if (i == j) {
String name = file1.getName();
String path1 = path.substring(0, path.length() - name.length() - 1);
File file3;
if (picUrl.contains(".png") || picUrl.contains(".PNG")) {
file3 = new File(path1 + "/" + nameList.get(j) + ".png");
} else {
file3 = new File(path1 + "/" + nameList.get(j) + ".jpg");
}
if (file3.exists()) {
file3.delete();
}
file1.renameTo(file3);
fileList.add(file3);
}
}
}
}
if (!b) {
continue;
}
}
return fileList;
}
引入依赖
<!--图片压缩-->
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-jpeg</artifactId>
<version>3.3.2</version>
</dependency>