| | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.IOException; |
| | |
| | | this.init(list, sheetName, Excel.Type.EXPORT); |
| | | exportExcel(response.getOutputStream()); |
| | | } |
| | | private static double calculateScaleForCm(byte[] imageData, double widthCm, double heightCm) throws IOException { |
| | | // 读取图片获取原始尺寸 |
| | | BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageData)); |
| | | double originalWidthPx = img.getWidth(); |
| | | double originalHeightPx = img.getHeight(); |
| | | |
| | | // Excel默认DPI(96) |
| | | double excelDpi = 96.0; |
| | | double pixelsPerCm = excelDpi / 2.54; |
| | | |
| | | // 目标像素尺寸 |
| | | double targetWidthPx = widthCm * pixelsPerCm; |
| | | double targetHeightPx = heightCm * pixelsPerCm; |
| | | |
| | | // 计算比例(取宽高中的较小比例,保持纵横比) |
| | | double scaleWidth = targetWidthPx / originalWidthPx; |
| | | double scaleHeight = targetHeightPx / originalHeightPx; |
| | | System.out.println("-=========67676"); |
| | | System.out.println(originalHeightPx); |
| | | System.out.println(originalWidthPx); |
| | | System.out.println(targetWidthPx); |
| | | System.out.println(targetHeightPx); |
| | | return Math.min(scaleWidth, scaleHeight); |
| | | } |
| | | /** |
| | | * 对list数据源将其里面的数据导入到excel表单 |
| | | * |
| | |
| | | // 创建图片锚点,设置在第2行第2列 |
| | | // 二维码显示在右上角且不填满整个格子 |
| | | // 缩小图片范围:只占用部分单元格空间 |
| | | ClientAnchor anchor = new HSSFClientAnchor(680, 0, 1000, 250, (short) 1, 2, (short) 1, 2); |
| | | |
| | | ClientAnchor anchor = new HSSFClientAnchor(699, 0, 1023, 97, (short) 1, 2, (short) 1, 3); |
| | | // 设置图片位置和大小 |
| | | anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_DONT_RESIZE); |
| | | |
| | | // 添加图片到工作表 |
| | | getDrawingPatriarch(sheet).createPicture(anchor, |
| | | org.apache.poi.ss.usermodel.Picture picture = getDrawingPatriarch(sheet).createPicture(anchor, |
| | | wb.addPicture(data, getImageType(data))); |
| | | |
| | | |
| | | int pictureIdx = wb.addPicture(data, getImageType(data)); |
| | | // Picture picture = drawing.createPicture(anchor, pictureIdx); |
| | | |
| | | // 关键:计算并应用3.15cm的缩放 |
| | | // double targetSizeCm = 3.15; |
| | | // |
| | | //// 方法1:使用精确计算 |
| | | // double scale = calculateScaleForCm(data, targetSizeCm, targetSizeCm); |
| | | // System.out.println(scale); |
| | | // picture.resize(scale); |
| | | |
| | | // 二维码添加完成后,恢复原来的列宽设置 |
| | | sheet.setColumnWidth(1, currentColumnWidth); |
| | | } |
| | |
| | | sheet.setColumnWidth(1, (int) ((51.57 + 0.72) * 256)); // 第二列:宽度51 |
| | | |
| | | CellStyle style = wb.createCellStyle(); |
| | | style.setAlignment(HorizontalAlignment.RIGHT); |
| | | style.setAlignment(HorizontalAlignment.LEFT); |
| | | style.setVerticalAlignment(VerticalAlignment.BOTTOM); // 设置垂直下对齐 |
| | | style.setWrapText(true); // 设置自动换行 |
| | | |