【工具】Java Excel转图片

news/2024/9/23 7:06:49/

【工具】Java Excel转图片

java">package com.yj.luban.modules.office.excel;import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;public class ExcelToImg {public static void main(String[] args) throws IOException {// Excel 文件路径String excelFilePath = "D:\\WORK\\workspace_tools\\Office\\excelToImg\\工时.xlsx";FileInputStream excelFile = new FileInputStream(new File(excelFilePath));// 创建 Workbook 对象Workbook workbook = new XSSFWorkbook(excelFile);Sheet sheet = workbook.getSheetAt(0);  // 获取第一个工作表// 创建一个临时的 BufferedImage 用于测量文本宽度BufferedImage tempImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);Graphics2D tempGraphics = tempImage.createGraphics();Font font = new Font("Arial", Font.PLAIN, 12);tempGraphics.setFont(font);FontMetrics fontMetrics = tempGraphics.getFontMetrics();// 动态计算每列的宽度int totalColumns = getMaxColumns(sheet);int[] columnWidths = new int[totalColumns];int rowHeight = fontMetrics.getHeight() + 10;  // 行高根据字体高度动态调整// 遍历所有单元格内容,计算最大列宽for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {Row row = sheet.getRow(rowIndex);if (row == null) continue;for (int colIndex = 0; colIndex < totalColumns; colIndex++) {Cell cell = row.getCell(colIndex);if (cell != null) {String cellValue = cell.toString();int textWidth = fontMetrics.stringWidth(cellValue) + 10;  // 加 10 像素边距columnWidths[colIndex] = Math.max(columnWidths[colIndex], textWidth);} else {columnWidths[colIndex] = Math.max(columnWidths[colIndex], 100);  // 设置默认最小宽度}}}// 计算图像总宽度和总高度int imageWidth = 50;  // 初始边距for (int colWidth : columnWidths) {imageWidth += colWidth;}int imageHeight = (sheet.getLastRowNum() + 1) * rowHeight + 100;  // 加上顶部和底部边距// 创建最终的 BufferedImageBufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);Graphics2D graphics = image.createGraphics();// 设置白色背景graphics.setColor(Color.WHITE);graphics.fillRect(0, 0, imageWidth, imageHeight);// 设置字体graphics.setColor(Color.BLACK);graphics.setFont(font);// 起始坐标int startX = 50;int startY = 50;// 绘制每个单元格的内容和样式for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {Row row = sheet.getRow(rowIndex);if (row == null) continue;int x = startX;for (int colIndex = 0; colIndex < totalColumns; colIndex++) {Cell cell = row.getCell(colIndex);String cellValue = (cell != null) ? cell.toString() : "";// 绘制单元格内容graphics.drawString(cellValue, x + 5, startY + rowIndex * rowHeight + rowHeight / 2);// 绘制单元格边框if (cell != null) {CellStyle cellStyle = cell.getCellStyle();drawCellBorders(graphics, x, startY + rowIndex * rowHeight, columnWidths[colIndex], rowHeight, cellStyle);}// 移动到下一个单元格的位置x += columnWidths[colIndex];}}// 释放资源graphics.dispose();workbook.close();tempGraphics.dispose();// 保存图片ImageIO.write(image, "png", new File("D:\\WORK\\workspace_tools\\Office\\excelToImg\\a.png"));System.out.println("Excel 样式和边框转换为图片成功!");}// 获取最大列数private static int getMaxColumns(Sheet sheet) {int maxColumns = 0;for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {Row row = sheet.getRow(rowIndex);if (row != null && row.getLastCellNum() > maxColumns) {maxColumns = row.getLastCellNum();}}return maxColumns;}// 绘制单元格边框private static void drawCellBorders(Graphics2D graphics, int x, int y, int width, int height, CellStyle style) {// 设置边框颜色为黑色graphics.setColor(Color.BLACK);// 绘制顶部边框if (style.getBorderTop() != BorderStyle.NONE) {graphics.drawLine(x, y, x + width, y);}// 绘制底部边框if (style.getBorderBottom() != BorderStyle.NONE) {graphics.drawLine(x, y + height, x + width, y + height);}// 绘制左侧边框if (style.getBorderLeft() != BorderStyle.NONE) {graphics.drawLine(x, y, x, y + height);}// 绘制右侧边框if (style.getBorderRight() != BorderStyle.NONE) {graphics.drawLine(x + width, y, x + width, y + height);}}
}

设置指定字体

java">import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;public class ExcelToStyledImageWithBorders {public static void main(String[] args) throws IOException {// Excel 文件路径String excelFilePath = "example.xlsx";FileInputStream excelFile = new FileInputStream(new File(excelFilePath));// 创建 Workbook 对象Workbook workbook = new XSSFWorkbook(excelFile);Sheet sheet = workbook.getSheetAt(0);  // 获取第一个工作表// 创建一个临时的 BufferedImage 用于测量文本宽度BufferedImage tempImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);Graphics2D tempGraphics = tempImage.createGraphics();// 使用 SimSun 字体,支持中文字符Font font = new Font("SimSun", Font.PLAIN, 12);tempGraphics.setFont(font);FontMetrics fontMetrics = tempGraphics.getFontMetrics();// 动态计算每列的宽度int totalColumns = getMaxColumns(sheet);int[] columnWidths = new int[totalColumns];int rowHeight = fontMetrics.getHeight() + 10;  // 行高根据字体高度动态调整// 遍历所有单元格内容,计算最大列宽for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {Row row = sheet.getRow(rowIndex);if (row == null) continue;for (int colIndex = 0; colIndex < totalColumns; colIndex++) {Cell cell = row.getCell(colIndex);if (cell != null) {String cellValue = cell.toString();int textWidth = fontMetrics.stringWidth(cellValue) + 10;  // 加 10 像素边距columnWidths[colIndex] = Math.max(columnWidths[colIndex], textWidth);} else {columnWidths[colIndex] = Math.max(columnWidths[colIndex], 100);  // 设置默认最小宽度}}}// 计算图像总宽度和总高度int imageWidth = 50;  // 初始边距for (int colWidth : columnWidths) {imageWidth += colWidth;}int imageHeight = (sheet.getLastRowNum() + 1) * rowHeight + 100;  // 加上顶部和底部边距// 创建最终的 BufferedImageBufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);Graphics2D graphics = image.createGraphics();// 设置白色背景graphics.setColor(Color.WHITE);graphics.fillRect(0, 0, imageWidth, imageHeight);// 设置字体,确保支持中文graphics.setFont(font);graphics.setColor(Color.BLACK);// 起始坐标int startX = 50;int startY = 50;// 绘制每个单元格的内容和样式for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {Row row = sheet.getRow(rowIndex);if (row == null) continue;int x = startX;for (int colIndex = 0; colIndex < totalColumns; colIndex++) {Cell cell = row.getCell(colIndex);String cellValue = (cell != null) ? cell.toString() : "";// 绘制单元格内容graphics.drawString(cellValue, x + 5, startY + rowIndex * rowHeight + rowHeight / 2);// 绘制单元格边框if (cell != null) {CellStyle cellStyle = cell.getCellStyle();drawCellBorders(graphics, x, startY + rowIndex * rowHeight, columnWidths[colIndex], rowHeight, cellStyle);}// 移动到下一个单元格的位置x += columnWidths[colIndex];}}// 释放资源graphics.dispose();workbook.close();tempGraphics.dispose();// 保存图片ImageIO.write(image, "png", new File("excel_styled_with_borders_image.png"));System.out.println("Excel 样式和边框转换为图片成功!");}// 获取最大列数private static int getMaxColumns(Sheet sheet) {int maxColumns = 0;for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {Row row = sheet.getRow(rowIndex);if (row != null && row.getLastCellNum() > maxColumns) {maxColumns = row.getLastCellNum();}}return maxColumns;}// 绘制单元格边框private static void drawCellBorders(Graphics2D graphics, int x, int y, int width, int height, CellStyle style) {// 设置边框颜色为黑色graphics.setColor(Color.BLACK);// 绘制顶部边框if (style.getBorderTop() != BorderStyle.NONE) {graphics.drawLine(x, y, x + width, y);}// 绘制底部边框if (style.getBorderBottom() != BorderStyle.NONE) {graphics.drawLine(x, y + height, x + width, y + height);}// 绘制左侧边框if (style.getBorderLeft() != BorderStyle.NONE) {graphics.drawLine(x, y, x, y + height);}// 绘制右侧边框if (style.getBorderRight() != BorderStyle.NONE) {graphics.drawLine(x + width, y, x + width, y + height);}}
}

支持公式 固定了一个字体

java">import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;public class ExcelToStyledImageWithBordersAndFormula {public static void main(String[] args) throws IOException {// Excel 文件路径String excelFilePath = "example.xlsx";FileInputStream excelFile = new FileInputStream(new File(excelFilePath));// 创建 Workbook 对象Workbook workbook = new XSSFWorkbook(excelFile);Sheet sheet = workbook.getSheetAt(0);  // 获取第一个工作表// 创建 FormulaEvaluator 对象来解析公式FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();// 创建一个临时的 BufferedImage 用于测量文本宽度BufferedImage tempImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);Graphics2D tempGraphics = tempImage.createGraphics();// 使用 SimSun 字体,支持中文字符Font font = new Font("SimSun", Font.PLAIN, 12);tempGraphics.setFont(font);FontMetrics fontMetrics = tempGraphics.getFontMetrics();// 动态计算每列的宽度int totalColumns = getMaxColumns(sheet);int[] columnWidths = new int[totalColumns];int rowHeight = fontMetrics.getHeight() + 10;  // 行高根据字体高度动态调整// 遍历所有单元格内容,计算最大列宽for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {Row row = sheet.getRow(rowIndex);if (row == null) continue;for (int colIndex = 0; colIndex < totalColumns; colIndex++) {Cell cell = row.getCell(colIndex);if (cell != null) {String cellValue = getCellValue(cell, formulaEvaluator);  // 获取单元格值,包含公式解析int textWidth = fontMetrics.stringWidth(cellValue) + 20;  // 加 20 像素边距columnWidths[colIndex] = Math.max(columnWidths[colIndex], textWidth);} else {columnWidths[colIndex] = Math.max(columnWidths[colIndex], 100);  // 设置默认最小宽度}}}// 计算图像总宽度和总高度int imageWidth = 100;  // 初始边距,增加更多的边距以防止截断for (int colWidth : columnWidths) {imageWidth += colWidth;}int imageHeight = (sheet.getLastRowNum() + 1) * rowHeight + 100;  // 加上顶部和底部边距// 创建最终的 BufferedImageBufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);Graphics2D graphics = image.createGraphics();// 设置白色背景graphics.setColor(Color.WHITE);graphics.fillRect(0, 0, imageWidth, imageHeight);// 设置字体,确保支持中文graphics.setFont(font);graphics.setColor(Color.BLACK);// 起始坐标int startX = 50;int startY = 50;// 绘制每个单元格的内容和样式for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {Row row = sheet.getRow(rowIndex);if (row == null) continue;int x = startX;for (int colIndex = 0; colIndex < totalColumns; colIndex++) {Cell cell = row.getCell(colIndex);String cellValue = (cell != null) ? getCellValue(cell, formulaEvaluator) : "";// 绘制单元格内容graphics.drawString(cellValue, x + 5, startY + rowIndex * rowHeight + rowHeight / 2);// 绘制单元格边框if (cell != null) {CellStyle cellStyle = cell.getCellStyle();drawCellBorders(graphics, x, startY + rowIndex * rowHeight, columnWidths[colIndex], rowHeight, cellStyle);}// 移动到下一个单元格的位置x += columnWidths[colIndex];}}// 释放资源graphics.dispose();workbook.close();tempGraphics.dispose();// 保存图片ImageIO.write(image, "png", new File("excel_styled_with_borders_and_formula_image.png"));System.out.println("Excel 样式和边框、公式转换为图片成功!");}// 获取单元格值,并解析公式private static String getCellValue(Cell cell, FormulaEvaluator formulaEvaluator) {switch (cell.getCellType()) {case STRING:return cell.getStringCellValue();case NUMERIC:return String.valueOf(cell.getNumericCellValue());case BOOLEAN:return String.valueOf(cell.getBooleanCellValue());case FORMULA:// 使用 FormulaEvaluator 解析公式CellValue evaluatedValue = formulaEvaluator.evaluate(cell);switch (evaluatedValue.getCellType()) {case STRING:return evaluatedValue.getStringValue();case NUMERIC:return String.valueOf(evaluatedValue.getNumberValue());case BOOLEAN:return String.valueOf(evaluatedValue.getBooleanValue());default:return " ";}default:return " ";}}// 获取最大列数private static int getMaxColumns(Sheet sheet) {int maxColumns = 0;for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {Row row = sheet.getRow(rowIndex);if (row != null && row.getLastCellNum() > maxColumns) {maxColumns = row.getLastCellNum();}}return maxColumns;}// 绘制单元格边框private static void drawCellBorders(Graphics2D graphics, int x, int y, int width, int height, CellStyle style) {// 设置边框颜色为黑色graphics.setColor(Color.BLACK);// 绘制顶部边框if (style.getBorderTop() != BorderStyle.NONE) {graphics.drawLine(x, y, x + width, y);}// 绘制底部边框if (style.getBorderBottom() != BorderStyle.NONE) {graphics.drawLine(x, y + height, x + width, y + height);}// 绘制左侧边框if (style.getBorderLeft() != BorderStyle.NONE) {graphics.drawLine(x, y, x, y + height);}// 绘制右侧边框if (style.getBorderRight() != BorderStyle.NONE) {graphics.drawLine(x + width, y, x + width, y + height);}}
}


http://www.ppmy.cn/news/1529203.html

相关文章

Android 命令行关机

在 Android 设备上&#xff0c;可以通过以下命令行命令来关机&#xff1a; adb shell reboot -p其中&#xff1a; adb shell&#xff1a;通过 ADB 进入设备的命令行环境。reboot -p&#xff1a;执行关机操作&#xff0c;-p 表示关机而不是重启。 如果你是在设备本地的终端上而…

怎么在路由器上使用tcpdump抓包

怎么在路由器上使用tcpdump抓包 1&#xff09;下载tcpdump包到桌面&#xff0c;路由器界面进入 /tmp目录&#xff0c;/目录无法安装 2&#xff09;开启ftp服务—可以安装IPOP4.1.exe软件&#xff0c;打开服务–ftp列 3&#xff09;连接本机的ftp服务器—ftp 192.168.1.2 &#…

论文阅读:Omni-Kernel Network for Image Restoration

论文地址&#xff1a;https://ojs.aaai.org/index.php/AAAI/article/view/27907 项目地址&#xff1a;https://github.com/c-yn/OKNet 发表时间&#xff1a;2024 图像恢复的目的是从一个退化的低质量的观测中重建一个高质量的图像。最近&#xff0c;Transformer模型由于其强大…

ROS第五梯:ROS+VSCode+C++单步调试

解决问题&#xff1a;在ROS项目中进行断点调试。 第一步&#xff1a;创建一个ROS项目或者打开一个现有的ROS项目。 第二步&#xff1a;修改c_cpp_properties.json 增加一段命令: "compileCommands": "${workspaceFolder}/build/compile_commands.json"第三…

C++: 两个栈实现队列

解题思路 栈&#xff0c;一个口&#xff0c;先进后出&#xff1b;队列&#xff0c;两个口&#xff0c;先进先出&#xff1b; 两个栈就有两个口&#xff0c;一个当入口&#xff0c;另一个当出口 当stack2为空&#xff0c;将stack1元素push到stack2&#xff0c;再pop stack2 ; 当…

计算机三级网络技术总结(二)

RPR使用统计复用的方法传输IP分组IEEE802.16a用于固定结点接入ADSL技术为速率非对称型&#xff0c;上行速率为64kbps~640kbpsRAID是磁盘阵列技术在一定程度上可以提高磁盘存储容量但不能提高容错能力中继器工作在物理层VTP有三种工作模式:VTP Server、VTP Client 和VTP Transpa…

Matlab Simulink 主时间步(major time step)、子时间步(minor time step)

高亮颜色说明&#xff1a;突出重点 个人觉得&#xff0c;&#xff1a;待核准个人观点是否有误 高亮颜色超链接 文章目录 对Simulink 时间步的理解Simulink 采样时间的类型Discrete Sample Times(离散采样时间)Controllable Sample Time(可控采样时间) Continuous Sample Times(…

Linux下rpm方式部署mysql(国产化生产环境无联网服务器部署实操)

请放心观看&#xff0c;已在正式环境部署验证&#xff0c;流程无问题&#xff01; 所用系统为国产化麒麟银河 aarch64系统&#xff0c;部署时间2024年9月份&#xff01; #查看服务器信息 #涉及生产服务器&#xff0c;所以输出信息隐藏了一部分[rootecs-xxxxx hdata]# uname -…