excel表生成截图并添加水印
CreatedUpdated
Post View: 常德
java数据处理excel表生成截图并添加水印
幽导包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.4</version> </dependency>
|
实现代码
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
| public static void main(String[] args) { String excelFilePath = "F:\\info.xlsx"; String imageFilePath = "F:\\image.png";
try (FileInputStream fis = new FileInputStream(new File(excelFilePath)); Workbook workbook = new XSSFWorkbook(fis)) {
Sheet sheet = workbook.getSheetAt(0); int rowCount = sheet.getLastRowNum() + 1; int columnCount = 0;
for (Row row : sheet) { if (row != null && row.getLastCellNum() > columnCount) { columnCount = row.getLastCellNum(); } }
int cellWidth = 100; int cellHeight = 30; BufferedImage image = new BufferedImage(columnCount * cellWidth, rowCount * cellHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
g2d.setColor(Color.BLACK); for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { Row row = sheet.getRow(rowIndex); if (row == null) { continue; } for (int colIndex = 0; colIndex < columnCount; colIndex++) { Cell cell = row.getCell(colIndex); String cellValue = getCellValue(cell);
g2d.drawRect(colIndex * cellWidth, rowIndex * cellHeight, cellWidth, cellHeight);
g2d.drawString(cellValue, colIndex * cellWidth + 5, rowIndex * cellHeight + 20); } }
drawWatermark(g2d, image.getWidth(), image.getHeight());
ImageIO.write(image, "png", new File(imageFilePath));
g2d.dispose(); System.out.println("Excel文件已成功转换为图片。");
} catch (IOException e) { e.printStackTrace(); } }
private static String getCellValue(Cell cell) { if (cell == null) { return ""; } switch (cell.getCellType()) { case STRING: return cell.getStringCellValue(); case NUMERIC: return String.valueOf(cell.getNumericCellValue()); case BOOLEAN: return String.valueOf(cell.getBooleanCellValue()); case FORMULA: return cell.getCellFormula(); default: return ""; } }
private static void drawWatermark(Graphics2D g2d, int width, int height) { g2d.setFont(new Font("宋体", Font.BOLD, 24)); g2d.setColor(new Color(255, 0, 0, 128));
String watermarkText = "水印";
FontMetrics metrics = g2d.getFontMetrics(); int textWidth = metrics.stringWidth(watermarkText); int textHeight = metrics.getHeight();
double angle = Math.toRadians(-20);
int spacing = 150;
for (int x = -textWidth; x < width; x += spacing) { for (int y = -textHeight; y < height; y += spacing) { AffineTransform originalTransform = g2d.getTransform(); g2d.translate(x + textWidth / 2, y + textHeight / 2); g2d.rotate(angle); g2d.drawString(watermarkText, -textWidth / 2, -textHeight / 2); g2d.setTransform(originalTransform); } } }
|
效果
注:我excel对比上一篇文章添加过数据,数据量太少,水印间隔太远导致不显示了,我懒得改代码了
