Showing
3 changed files
with
38 additions
and
20 deletions
| @@ -13,6 +13,7 @@ import javax.swing.*; | @@ -13,6 +13,7 @@ import javax.swing.*; | ||
| 13 | import java.awt.*; | 13 | import java.awt.*; |
| 14 | import java.awt.Color; | 14 | import java.awt.Color; |
| 15 | import java.awt.image.BufferedImage; | 15 | import java.awt.image.BufferedImage; |
| 16 | +import java.io.ByteArrayInputStream; | ||
| 16 | import java.io.ByteArrayOutputStream; | 17 | import java.io.ByteArrayOutputStream; |
| 17 | import java.io.IOException; | 18 | import java.io.IOException; |
| 18 | import java.math.BigDecimal; | 19 | import java.math.BigDecimal; |
| @@ -138,34 +139,51 @@ public class LatexFormulaExcelExporterUtil { | @@ -138,34 +139,51 @@ public class LatexFormulaExcelExporterUtil { | ||
| 138 | * 将LaTeX公式作为图片插入到指定单元格 | 139 | * 将LaTeX公式作为图片插入到指定单元格 |
| 139 | */ | 140 | */ |
| 140 | public static void insertLatexImageToCell(Workbook workbook, Sheet sheet, String latex, int rowIndex, int colIndex) throws IOException { | 141 | public static void insertLatexImageToCell(Workbook workbook, Sheet sheet, String latex, int rowIndex, int colIndex) throws IOException { |
| 141 | - // 1. 将LaTeX渲染为图片字节 | 142 | + // 设置固定30像素行高 |
| 143 | + Row row = sheet.getRow(rowIndex); | ||
| 144 | + if (row == null) { | ||
| 145 | + row = sheet.createRow(rowIndex); | ||
| 146 | + } | ||
| 147 | + | ||
| 148 | + row.setHeightInPoints(22.5f); // 30像素 | ||
| 149 | + | ||
| 142 | byte[] imageBytes = latexToImageBytes(latex, 48); | 150 | byte[] imageBytes = latexToImageBytes(latex, 48); |
| 151 | + ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes); | ||
| 152 | + BufferedImage image = ImageIO.read(bais); | ||
| 153 | + bais.close(); | ||
| 154 | + | ||
| 155 | + // 如果图片高度超过30像素,按比例缩小 | ||
| 156 | + double scale = 1.0; | ||
| 157 | + if (image.getHeight() > 30) { | ||
| 158 | + scale = 30.0 / image.getHeight(); | ||
| 159 | + System.out.println("图片需要缩放: " + scale); | ||
| 160 | + } | ||
| 143 | 161 | ||
| 144 | - // 2. 将图片添加到工作簿 | ||
| 145 | int pictureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG); | 162 | int pictureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG); |
| 146 | - | ||
| 147 | - // 3. 创建绘图对象 | ||
| 148 | Drawing<?> drawing = sheet.createDrawingPatriarch(); | 163 | Drawing<?> drawing = sheet.createDrawingPatriarch(); |
| 149 | 164 | ||
| 150 | - // 4. 创建锚点,确定图片位置 | ||
| 151 | ClientAnchor anchor = workbook.getCreationHelper().createClientAnchor(); | 165 | ClientAnchor anchor = workbook.getCreationHelper().createClientAnchor(); |
| 152 | - anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE); | ||
| 153 | - anchor.setCol1(colIndex); // C列 (从0开始计数) | ||
| 154 | - anchor.setRow1(rowIndex); // 当前数据行 (表头在第0行) | ||
| 155 | - anchor.setCol2(colIndex + 1); // 结束于D列 | ||
| 156 | - anchor.setRow2(rowIndex + 1); // 结束于下一行 | ||
| 157 | - | ||
| 158 | - // 设置较小的起始偏移 | ||
| 159 | - anchor.setDx1(50); // 水平偏移(建议值:10-100) | ||
| 160 | - anchor.setDy1(25); // 垂直偏移(建议值:10-50) | 166 | + anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_DONT_RESIZE); |
| 167 | + | ||
| 168 | + anchor.setCol1(colIndex); | ||
| 169 | + anchor.setRow1(rowIndex); | ||
| 170 | + anchor.setCol2(colIndex); | ||
| 171 | + anchor.setRow2(rowIndex); | ||
| 172 | + | ||
| 173 | + int scaledWidth = (int) (image.getWidth() * scale); | ||
| 174 | + int rate = 1; | ||
| 175 | + if (scaledWidth >= 60 && scaledWidth <= 150) { | ||
| 176 | + rate = 2; | ||
| 177 | + } else if (scaledWidth > 150) { | ||
| 178 | + rate = 3; | ||
| 179 | + } | ||
| 161 | 180 | ||
| 181 | + anchor.setDx1(25); | ||
| 182 | + anchor.setDy1(25); | ||
| 183 | + anchor.setDx2(330 * rate); | ||
| 184 | + anchor.setDy2(235); | ||
| 162 | 185 | ||
| 163 | - // 5. 创建图片并插入 | ||
| 164 | - Picture pict = drawing.createPicture(anchor, pictureIdx); | ||
| 165 | - | ||
| 166 | - // 6. 自动调整图片大小以适应单元格 (可选) | ||
| 167 | - // 取消注释此行使图片自动适应锚点区域 | ||
| 168 | -// pict.resize(0.95); // 缩小到90%,自然产生边距 | 186 | + drawing.createPicture(anchor, pictureIdx); |
| 169 | } | 187 | } |
| 170 | 188 | ||
| 171 | /** | 189 | /** |
No preview for this file type
No preview for this file type