-
Notifications
You must be signed in to change notification settings - Fork 0
/
JuniorCollege_src_main_java_com_java_utils_Write.java
72 lines (66 loc) · 2.65 KB
/
JuniorCollege_src_main_java_com_java_utils_Write.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.java.utils;
import com.java.pojo.User;
import org.apache.poi.hssf.usermodel.*;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;
/**
* @ClassName write
* @Description 在Excel表格中写入图片
* @Author LiYan
* @Date 2020/4/4 3:00
* @Version 1.0
*/
public class Write {
public static String write(List<User> productList, HttpServletRequest request) {
FileOutputStream fileOut = null;
BufferedImage bufferImg = null;
try {
//创建工作簿
HSSFWorkbook wb = new HSSFWorkbook();
//创建工作表
HSSFSheet sheet1 = wb.createSheet("sheet1");
sheet1.setColumnWidth(2, 256 * 40);
HSSFRow row = sheet1.createRow(0);
row.createCell(0).setCellValue("编号");
row.createCell(1).setCellValue("姓名");
row.createCell(2).setCellValue("截图");
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
String str = Long.toString(System.currentTimeMillis());
String path = request.getServletContext().getRealPath("download") + "/" + str + ".xls";
for (int i = 0; i < productList.size(); i++) {
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
HSSFRow row1 = sheet1.createRow(i + 1);
row1.setHeightInPoints(250);
row1.createCell(0).setCellValue(i + 1);
row1.createCell(1).setCellValue(productList.get(i).getUserName());
String name = productList.get(i).getImageName();
if (name == null || name == "") {
//结束本次循环
continue;
}
System.out.println(name);
bufferImg = ImageIO.read(new File(name));
ImageIO.write(bufferImg, "jpg", byteArrayOut);
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 2, i + 1, (short) 3, i + 2);
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
anchor = null;
}
fileOut = new FileOutputStream(path);
wb.write(fileOut);
fileOut.flush();
fileOut.close();
// 输出文件
return str;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}