JavaJavaPOI
2025-04-03 15:28阅读:82评论:0
java poi 导出Excel单元格等比例缩放内边距
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import lombok.AllArgsConstructor;import lombok.Builder;import lombok.Data;import lombok.NoArgsConstructor;import org.apache.poi.ss.usermodel.*;import org.apache.poi.util.IOUtils;import org.apache.poi.util.Units;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook; import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.util.List; /** * @author wency * @description: Excel导出处理图片 **/public class ExcelHandleImageUtils { /** * @param inputStream 图片转换的输入流 * @param sheet * @param workbook * @param col 图片所在的列 * @param row 图片所在的行 * @throws IOException */ public static void excelHandleImage(InputStream inputStream, XSSFSheet sheet, XSSFWorkbook workbook, int col, int row) throws IOException { float rowHeightInPoints = 54f; float rowHeightInPixels = rowHeightInPoints * Units.PIXEL_DPI / Units.POINT_DPI; sheet.setColumnWidth(col, 26 * 256); // 50 default characters width float colWidthInPixels = sheet.getColumnWidthInPixels(col); byte[] imageBytes = IOUtils.toByteArray(inputStream); BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageBytes)); if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) { throw new RuntimeException("is not img"); } int pictureureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_JPEG); inputStream.close(); CreationHelper helper = workbook.getCreationHelper(); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = helper.createClientAnchor(); //set start position of picture's anchor to top left B2 anchor.setRow1(row); anchor.setCol1(col); //create picture Picture pict = drawing.createPicture(anchor, pictureureIdx); //get picture's original size// int pictOriginalWidthInPixels = pict.getImageDimension().width;// int pictOriginalHeightInPixels = pict.getImageDimension().height; //set picture's wanted size float pictWidthInCm = 3f; float pictWidthInPixels = pictWidthInCm / 2.54f * 72f * Units.PIXEL_DPI / Units.POINT_DPI; //want scaling in aspect ratio?是否需要缩放纵横比?// float scale = pictWidthInPixels / pictOriginalWidthInPixels;// float pictHeightInPixels = pictOriginalHeightInPixels * scale; //want explicit set height too? float pictHeightInCm = 1.5f; float pictHeightInPixels = pictHeightInCm / 2.54f * 72f * Units.PIXEL_DPI / Units.POINT_DPI; //calculate the horizontal center position int horCenterPosInPixels = Math.round(colWidthInPixels / 2f - pictWidthInPixels / 2f); //set the horizontal center position as Dx1 of anchor anchor.setDx1(horCenterPosInPixels * Units.EMU_PER_PIXEL); //in unit EMU for XSSF //calculate the vertical center position int vertCenterPosInPixels = Math.round(rowHeightInPixels / 2f - pictHeightInPixels / 2f); //set the vertical center position as Dy1 of anchor anchor.setDy1(Math.round(vertCenterPosInPixels * Units.EMU_PER_PIXEL)); //in unit EMU for XSSF //set end position of picture's anchor to top left B2 anchor.setRow2(row); anchor.setCol2(col); //calculate the horizontal end position of picture int horCenterEndPosInPixels = Math.round(horCenterPosInPixels + pictWidthInPixels); //set the horizontal end position as Dx2 of anchor anchor.setDx2(horCenterEndPosInPixels * Units.EMU_PER_PIXEL); //in unit EMU for XSSF //calculate the vertical end position of picture int vertCenterEndPosInPixels = Math.round(vertCenterPosInPixels + pictHeightInPixels); //set the vertical end position as Dy2 of anchor anchor.setDy2(Math.round(vertCenterEndPosInPixels * Units.EMU_PER_PIXEL)); //in unit EMU for XSSF // 图片宽高缩放比例 pict.resize(0.4, 0.6); } @Data @Builder @AllArgsConstructor @NoArgsConstructor public static class Image { /** * 图片默认列 */ private int col; /** * 图片默认行 */ private int row; private InputStream inputStream; }}