【工具】Java Excel转图片

【工具】Java Excel转图片

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);}}
}

设置指定字体

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);}}
}

支持公式 固定了一个字体

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.mzph.cn/bicheng/54839.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

深度学习03-神经网络01-什么是神经网络?

神经网络的基本概念 人工神经网络&#xff08;Artificial Neural Network&#xff0c;ANN&#xff09;&#xff1a; 是一种模仿生物神经网络的计算模型。由多个神经元&#xff08;或称为节点&#xff09;组成&#xff0c;这些节点通过不同的连接来传递信息。 每个神经元可以接…

淘客系统开发之卷轴模式系统源码功能分析

随着互联网技术的快速发展&#xff0c;电商行业不断创新&#xff0c;探索更加高效、有趣的用户参与机制。其中&#xff0c;卷轴模式作为一种新兴的商业模式&#xff0c;以其独特的积分兑换和任务系统&#xff0c;在淘客系统开发中得到了广泛应用。本文将从技术角度&#xff0c;…

Kafka-Manager安装及操作

文章目录 一、kafka-manager介绍二、kafka-manager安装三、Kafka-Manager操作 一、kafka-manager介绍 CMAK (Cluster Manager for Apache Kafka, previously known as Kafka Manager) CMAK (previously known as Kafka Manager) is a tool for managing Apache Kafka cluster…

LeetCode 每周算法 6(图论、回溯)

LeetCode 每周算法 6&#xff08;图论、回溯&#xff09; 图论算法&#xff1a; class Solution: def dfs(self, grid: List[List[str]], r: int, c: int) -> None: """ 深度优先搜索函数&#xff0c;用于遍历并标记与当前位置(r, c)相连的所有陆地&…

切换淘宝最新npm镜像源

&#x1f468;‍⚕️ 主页&#xff1a; gis分享者 &#x1f468;‍⚕️ 感谢各位大佬 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍⚕️ 收录于专栏&#xff1a;前端工程师 文章目录 一、&#x1f30e;前言二、&#x1f30e;切换淘宝最新npm镜像源2.…

[Linux] Linux操作系统 进程的状态

标题&#xff1a;[Linux] Linux操作系统 进程的状态 个人主页&#xff1a;水墨不写bug &#xff08;图片来源于网络&#xff09; 目录 一、前置概念的理解 1.并行和并发 2.时间片 3.进程间具有独立性 4.等待的本质 正文开始&#xff1a; 在校的时候&#xff0c;你一定学过《…

9.25度小满一面

1.map的底层 2.unorder_map哈希表有自己实现过吗&#xff1f;哈希冲突 3.poll和epoll和select的优缺点、 4.线程同步机制是用来做什么的? 5.五子棋项目问题-- 算法题: 6.LeetCode.重排链表 给定一个单链表 L 的头节点 head &#xff0c;单链表 L 表示为&#xff1a; L0…

通信工程学习:什么是VPN虚拟专用网络

VPN:虚拟专用网络 VPN(Virtual Private Network),即虚拟专用网络,是一种通过公共网络(如互联网)建立私有网络连接的技术。以下是关于VPN的详细解释: 一、VPN虚拟专用网络的定义与原理 VPN通过公共网络(通常是互联网)建立一个临时的、安全的连接,形…

JavaEE: 深入探索TCP网络编程的奇妙世界(四)

文章目录 TCP核心机制TCP核心机制四: 滑动窗口为啥要使用滑动窗口?滑动窗口介绍滑动窗口出现丢包咋办? TCP核心机制五: 流量控制 TCP核心机制 上一篇文章 JavaEE: 深入探索TCP网络编程的奇妙世界(三) 书接上文~ TCP核心机制四: 滑动窗口 为啥要使用滑动窗口? 之前我们讨…

stm32单片机个人学习笔记6(EXTI外部中断)

前言 本篇文章属于stm32单片机&#xff08;以下简称单片机&#xff09;的学习笔记&#xff0c;来源于B站教学视频。下面是这位up主的视频链接。本文为个人学习笔记&#xff0c;只能做参考&#xff0c;细节方面建议观看视频&#xff0c;肯定受益匪浅。 STM32入门教程-2023版 细…

thinkphp8 从入门到放弃(后面会完善用到哪里写到哪)

thinkphp8 从入门到放弃 引言 thinkphp* 大道至简一、 thinkphp8 安装安装Composerthinkphp 安装命令(tp-项目名称)多应用安装&#xff08;一个项目不会只有一个应用&#xff09;安装完文件目录如下本地部署配置伪静态好了项目可以run 二、架构服务&#xff08;Service&#xf…

【数学分析笔记】第3章第4节闭区间上的连续函数(1)

3. 函数极限与连续函数 3.4 闭区间上的连续函数 3.4.1 有界性定理 【定理3.4.1】 f ( x ) f(x) f(x)在闭区间 [ a , b ] [a,b] [a,b]上连续&#xff0c;则 f ( x ) f(x) f(x)在闭区间 [ a , b ] [a,b] [a,b]上有界。 【证】用反证法&#xff0c;假设 f ( x ) f(x) f(x)在 [ …

Java项目: 基于SpringBoot+mybatis+maven中小型医院网站管理系统(含源码+数据库+开题报告+毕业论文)

一、项目简介 本项目是一套基于SpringBootmybatismaven中小型医院网站管理系统 包含&#xff1a;项目源码、数据库脚本等&#xff0c;该项目附带全部源码可作为毕设使用。 项目都经过严格调试&#xff0c;eclipse或者idea 确保可以运行&#xff01; 该系统功能完善、界面美观、…

MySql数据库---子查询,多表连接查询,自连接查询,串联查询,数学函数,字符串函数,时间日期函数,判断分支语句

思维导图 子查询[分步走] 1:一个sql的查询结果当做另一个sql的查询条件. 2:内层的那个sql语句要先执行 -- todo --------------子查询---(嵌套查询)--------------- -- 例如&#xff0c;使用命令完成&#xff1a; -- &#xff08;1&#xff09;使用数据库班级db_product3下的商…

html TAB、table生成

1. 代码 <!DOCTYPE html> <head> <meta charset"UTF-8"> <title>Dynamic Tabs with Table Data</title> <style> /* 简单的样式 */ .tab-content { display: none; border: 10px solid #ccc; padding: 30px; mar…

音视频生态下Unity3D和虚幻引擎(Unreal Engine)的区别

技术背景 好多开发者跟我们做技术交流的时候&#xff0c;会问我们&#xff0c;为什么有Unity3D的RTMP|RTSP播放模块&#xff0c;还有RTMP推送和轻量级RTSP服务模块&#xff0c;为什么不去支持虚幻引擎&#xff1f;二者区别在哪里&#xff1f;本文就Unity3D和虚幻引擎之间的差异…

Mac使用gradle编译springboot-2.7.x源码

1 开发环境&#xff1a; JDK8 ideaIU-2024.2.2 gradle-7.6.3 代理网络 2 下载springboot源码 代码仓库网址 git clone -b 2.7.x https://github.com/spring-projects/spring-boot.git3 安装gradle gradle下载网址 https://services.gradle.org/distributions/ 安装此文件指…

统信服务器操作系统【刻录镜像制作U盘启动盘的工具】

统信服务器操作系统各版本上刻录镜像制作U盘启动盘的工具方案 文章目录 应用场景一、问题现象二、问题分析解决方案应用场景 硬件/整机信息:全平台 CPU架构:全架构 OS版本信息:服务器a版,e版,d版(其中d版遇到的刻录类问题较少) 软件信息:dd工具、Fedora Media Writer工…

react hooks--useCallback

概述 useCallback缓存的是一个函数&#xff0c;主要用于性能优化!!! 基本用法 如何进行性能的优化呢&#xff1f; useCallback会返回一个函数的 memoized&#xff08;记忆的&#xff09; 值&#xff1b;在依赖不变的情况下&#xff0c;多次定义的时候&#xff0c;返回的值是…

不在同一局域网怎么远程桌面?非局域网环境下,实现远程桌面访问的5个方法分享!

非局域网环境下&#xff0c;怎么远程桌面&#xff1f;还能做到吗&#xff1f; 在企业管理中&#xff0c;远程桌面访问已成为提高工作效率、实现跨地域协同工作的关键工具。 然而&#xff0c;当被控端与控制端不在同一局域网时&#xff0c;如何实现远程桌面连接成为了许多企业…