当前位置: 首页 > news >正文

Excel转pdf

1、excel-内存值--Workbook 转pdf

/**
* excel To pdf
*
* @param outPath 输出路径
* @param workbook excel-内存值
* @throws IOException
*/
public static void excelToPdf(String outPath,Workbook workbook) throws IOException, DocumentException {
Document document=null;
try{
// excel
Sheet sheet = workbook.getSheetAt(0);
//设置中文
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
//普通字体
Font fontChinese = new Font(bfChinese, 12,Font.NORMAL);
// 创建pdf
document=new Document(PageSize.A4);
// 写输出路径
PdfWriter.getInstance(document,new FileOutputStream(outPath));
// 打开文档
document.open();
// 转换
convertSheetToPdf(sheet,document,fontChinese);
}catch (Exception e){
e.printStackTrace();
}finally {
document.close();
}
}

/**
* 循环值
*
* @param sheet excel-sheet
* @param document pdf对象
* @param fontChinese 字体
*/
private static void convertSheetToPdf(Sheet sheet, Document document,Font fontChinese) {
try {
// 获取excel总列数
int totalCol = sheet.getRow(0).getPhysicalNumberOfCells();
// 准备遍历excel
Iterator<Row> rowIterator = sheet.iterator();
// 在pdf中创建一个表格
PdfPTable myTable = new PdfPTable(totalCol);
// 遍历excel, 将数据输出到pdf的表格中
PdfPCell tableCell = null;
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
tableCell=new PdfPCell(new Phrase(getCellValue(cell),fontChinese));
myTable.addCell(tableCell);
}
}
document.add(myTable);
} catch (DocumentException e) {
e.printStackTrace();
}
}

/**
* cell-值类型转换
*
* @param cell 单元格-列
* @return
*/
private static String getCellValue(Cell cell) {
if(cell.getCellType() == CellType.STRING){
return cell.getStringCellValue();
}else if(cell.getCellType() == CellType.NUMERIC){
return String.valueOf(cell.getNumericCellValue());
}else{
return "";
}
}

2、获取Excel文件路径转pdf

/**
* 将指定路径excel转换为pdf
*
* @param pdfPath pdf输出路径
* @param excelPath excel路径
* @throws IOException
* @throws DocumentException
*/
public static void excelToPdf(String pdfPath,String excelPath) throws IOException {
Document document=null;
Workbook workbook=null;
try{
// 读取excel
InputStream inputStream = new FileInputStream(excelPath);
workbook = new XSSFWorkbook(inputStream);
//设置中文
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
//普通字体
Font fontChinese = new Font(bfChinese, 12,Font.NORMAL);
// 创建pdf
document=new Document(PageSize.A4);
// 写输出路径
PdfWriter.getInstance(document,new FileOutputStream(pdfPath));
// 打开文档
document.open();
// 遍历工作表中的所有行和单元格,并将其添加到PDF文档中
convertWorkBookToPdf(workbook,document,fontChinese);
}catch (Exception e){
e.printStackTrace();
}finally {
document.close();
workbook.close();
}
}

/**
* 遍历工作表中的所有行和单元格,并将其添加到PDF文档中
*
* @param workbook excel
* @param document pdf
* @param fontChinese 字体
*/
private static void convertWorkBookToPdf(Workbook workbook, Document document,Font fontChinese){
try{
Sheet sheet = workbook.getSheetAt(0);
// 获取excel总列数
int totalCol = sheet.getRow(0).getPhysicalNumberOfCells();
// 在pdf中创建一个表格
PdfPTable myTable = new PdfPTable(totalCol);
// 遍历excel, 将数据输出到pdf的表格中
PdfPCell tableCell = null;
for (int j = 0; j <= sheet.getLastRowNum(); j++) {
// 行
Row row = sheet.getRow(j);
if (row != null) {
for (int k = 0; k < row.getLastCellNum(); k++) {
// 列
Cell cell = row.getCell(k);
if (cell != null) {
// 值
tableCell=new PdfPCell(new Phrase(getCellValue(cell),fontChinese));
myTable.addCell(tableCell);
}
}
}
}
document.add(myTable);
}catch (Exception e){
e.printStackTrace();
}
}

/**
* cell-值类型转换
*
* @param cell 单元格-列
* @return
*/
private static String getCellValue(Cell cell) {
if(cell.getCellType() == CellType.STRING){
return cell.getStringCellValue();
}else if(cell.getCellType() == CellType.NUMERIC){
return String.valueOf(cell.getNumericCellValue());
}else{
return "";
}
}

3、获取Excel文件路径转pdf-复杂

/**
* 将指定路径excel转换为pdf
*
* @param pdfPath pdf输出路径
* @param excelPath excel路径
* @param startTime 开始时间
* @param endTime 结束时间
* @param title 标题
* @param bill 总金额
* @param duration 总充电时长
* @param degrees 总用电度数
* @throws IOException
* @throws DocumentException
*/
public static void excelToPdfUseRecord(String pdfPath,String excelPath,String startTime, String endTime,String title,
String bill,String duration,String degrees) throws IOException {
Document document=null;
Workbook workbook=null;
InputStream inputStream=null;
PdfWriter pw=null;
try{
// 读取excel
inputStream = new FileInputStream(excelPath);
workbook = new XSSFWorkbook(inputStream);
// 设置中文
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
// 普通字体
Font fontChinese = new Font(bfChinese, 8,Font.NORMAL);
// 创建pdf
document=new Document(PageSize.A4);
// 写输出路径
pw=PdfWriter.getInstance(document,new FileOutputStream(pdfPath));
// 打开文档
document.open();
// 添加标题
Paragraph paragraphTitle = new Paragraph();
paragraphTitle.setAlignment(Element.ALIGN_CENTER);
paragraphTitle.add(new Phrase(title,new Font(bfChinese, 20,Font.NORMAL)));
document.add(paragraphTitle);

// 换行
document.add(new Paragraph("\n"));

// 次标题
// 第一个单元格居左显示
PdfPTable subTitle = new PdfPTable(1);
subTitle.setWidthPercentage(85);
// 第一个单元格内容居右显示
PdfPCell cellRight = new PdfPCell(new Phrase(startTime+ MessageUtils.message("record.export.pdf.subTitle")+endTime,fontChinese));
cellRight.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); // 设置内容居右显示
cellRight.setBorder(PdfPCell.NO_BORDER); // 设置无边框
subTitle.addCell(cellRight);

// 将表格添加到文档中
document.add(subTitle);
// 遍历工作表中的所有行和单元格,并将其添加到PDF文档中
convertWorkBookToPdf(workbook,document,fontChinese);


// 合计
// 居左显示
PdfPTable hjTitle = new PdfPTable(4);
subTitle.setWidthPercentage(85);
PdfPCell hjCellLeft = new PdfPCell(new Phrase(MessageUtils.message("record.export.pdf.hj"),fontChinese));
// 设置无边框
hjCellLeft.setBorder(PdfPCell.NO_BORDER);
hjTitle.addCell(hjCellLeft);
// 总时长
PdfPCell durationCell = new PdfPCell(new Phrase(MessageUtils.message("record.export.pdf.sumDuration")+duration,fontChinese));
// 设置内容居中显示
durationCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
// 设置无边框
durationCell.setBorder(PdfPCell.NO_BORDER);
hjTitle.addCell(durationCell);
// 总费用
PdfPCell billCell = new PdfPCell(new Phrase(MessageUtils.message("record.export.pdf.sumBill")+bill,fontChinese));
// 设置内容居中显示
billCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
// 设置无边框
billCell.setBorder(PdfPCell.NO_BORDER);
hjTitle.addCell(billCell);
// 总度数
PdfPCell degreesCell = new PdfPCell(new Phrase(MessageUtils.message("record.export.pdf.sumDegrees")+degrees,fontChinese));
// 设置内容居中显示
degreesCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
// 设置无边框
degreesCell.setBorder(PdfPCell.NO_BORDER);
hjTitle.addCell(degreesCell);

// 将表格添加到文档中
document.add(hjTitle);

}catch (Exception e){
e.printStackTrace();
}finally {
inputStream.close();
workbook.close();
document.close();
pw.close();

}
}

/**
* 遍历工作表中的所有行和单元格,并将其添加到PDF文档中
*
* @param workbook excel
* @param document pdf
* @param fontChinese 字体
*/
private static void convertWorkBookToPdf(Workbook workbook, Document document,Font fontChinese){
try{
Sheet sheet = workbook.getSheetAt(0);
// 获取excel总列数
int totalCol = sheet.getRow(0).getPhysicalNumberOfCells();
// 在pdf中创建一个表格


PdfPTable myTable = new PdfPTable(totalCol);
// 表格总宽度
myTable.setWidthPercentage(85);
// 每个单元格宽度
myTable.setWidths(new float[]{1f, 1.5f, 1.5f,1.5f,1f,1f,1f});

// 遍历excel, 将数据输出到pdf的表格中
PdfPCell tableCell = null;
for (int j = 0; j <= sheet.getLastRowNum(); j++) {
// 行
Row row = sheet.getRow(j);
if (row != null) {
for (int k = 0; k < row.getLastCellNum(); k++) {
// 列
Cell cell = row.getCell(k);
if (cell != null) {
// 值
String cellValue=getCellValue(cell);
if(cellValue.contains("\n")){
cellValue=""+cellValue;
}
tableCell=new PdfPCell(new Phrase(cellValue,fontChinese));
// 设置内容居中显示
tableCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
myTable.addCell(tableCell);
}
}
}
}
document.add(myTable);
}catch (Exception e){
e.printStackTrace();
}
}

相关文章:

Excel转pdf

1、excel-内存值--Workbook 转pdf /** * excel To pdf * * param outPath 输出路径 * param workbook excel-内存值 * throws IOException */ public static void excelToPdf(String outPath,Workbook workbook) throws IOException, DocumentException { Document documentnul…...

QT:用opencv的KNN识别图片中的LED数字(一)

前言 一款功能测试的软件demo,使用了QT作为界面,主要使用了opencv的KNN识别,使用gstreamer作为管道,用来打开图片。后期会写一篇打开摄像头实时识别的文章。 (正在写,未完成,稍候) 效果一预览: 效果二预览: 效果三预览: 正在写。。。 设计思路 1. 软件UI设计 2. …...

leetcode72. 编辑距离

leetcode72. 编辑距离 题目 思路 dp[i][j] 代表 word1 到 i 位置转换成 word2 到 j 位置需要最少步数&#xff0c;所以&#xff0c; 当 word1[i] word2[j]&#xff0c;dp[i][j] dp[i-1][j-1]&#xff1b; 当 word1[i] ! word2[j]&#xff0c;dp[i][j] 1 min(dp[i-1][j-1]…...

R语言的数据类型与数据结构:向量、列表、矩阵、数据框及操作方法

R语言的数据类型与数据结构&#xff1a;向量、列表、矩阵、数据框及操作方法 介绍向量列表矩阵数据框 介绍 R语言拥有丰富的数据类型和数据结构&#xff0c;以满足各类数据处理和分析的需求。本文将分享R语言中的数据类型&#xff0c;包括向量、列表、矩阵、数据框等&#xff…...

第十六章 构建和配置 Nginx 以与 Web 网关配合使用 (Windows) - 将 NSD 与 Nginx 结合使用

文章目录 第十六章 构建和配置 Nginx 以与 Web 网关配合使用 (Windows) - 将 NSD 与 Nginx 结合使用将 NSD 与 Nginx 结合使用CSPNSD_pass hostname:portNum;CSP on; and CSP off;CSPFileTypes filetype1[ filetype2...];CSPNSD_response_headers_maxsize size;CSPNSD_connect_…...

数据中台:数字中国战略关键技术设施

文章目录 每日一句正能量前言为何要建设数据中台数据中台建设痛点数据中台学习资料聚焦前沿&#xff0c;方法论体系更新与时俱进&#xff0c;紧跟时代热点深入6大行业&#xff0c;提炼实践精华大咖推荐&#xff0c;数字化转型必备案头书购买链接赠书活动 每日一句正能量 人生之…...

mac上更改vscode快捷键

以移动当前行代码为例 mac上的vscode&#xff0c;默认移动当前行代码的快捷键是⌥↑即option↑按键 现在我想改成command↑ 步骤如下 1.打开vscode-code-首选项-键盘快捷键 2.打开快捷键列表 3.输入move line&#xff0c;找到要改动的这个快捷键 当前行-右键-更改键绑定&…...

Day18:信息打点-小程序应用解包反编译动态调试抓包静态分析源码架构

目录 小程序获取-各大平台&关键字搜索 小程序体验-凡科建站&模版测试上线 小程序抓包-Proxifier&BurpSuite联动 小程序逆向-解包反编译&动态调试&架构 思维导图 章节知识点 Web&#xff1a;语言/CMS/中间件/数据库/系统/WAF等 系统&#xff1a;操作系…...

真实案例分享:MOS管电源开关电路,遇到上电冲击电流超标

做硬件&#xff0c;堆经验。 分享一个案例&#xff1a;MOS管电源开关电路&#xff0c;遇到上电冲击电流超标&#xff0c;怎么解决的呢&#xff1f; 下面是正文部分。 —— 正文 —— 最近有一颗用了挺久的MOSFET发了停产通知&#xff0c;供应链部门找到我们研发部门&#xff0c…...

LCR 164. 破解闯关密码

解题思路&#xff1a; 贪心 class Solution {public String crackPassword(int[] password) {String[] strs new String[password.length];for(int i 0; i < password.length; i)strs[i] String.valueOf(password[i]);Arrays.sort(strs, (x, y) -> (x y).compareTo(…...

【鸿蒙 HarmonyOS 4.0】常用组件:List/Grid/Tabs

一、背景 列表页面&#xff1a;List组件和Grid组件&#xff1b; 页签切换&#xff1a;Tabs组件&#xff1b; 二、列表页面 在我们常用的手机应用中&#xff0c;经常会见到一些数据列表&#xff0c;如设置页面、通讯录、商品列表等。下图中两个页面都包含列表&#xff0c;“…...

打造经典游戏:HTML5与CSS3实现俄罗斯方块

&#x1f31f; 前言 欢迎来到我的技术小宇宙&#xff01;&#x1f30c; 这里不仅是我记录技术点滴的后花园&#xff0c;也是我分享学习心得和项目经验的乐园。&#x1f4da; 无论你是技术小白还是资深大牛&#xff0c;这里总有一些内容能触动你的好奇心。&#x1f50d; &#x…...

什么是系统工程(字幕)48

0 00:00:00,760 --> 00:00:03,550 那这里我们要说一下 1 00:00:04,050 --> 00:00:06,163 你看&#xff0c;刚才我们这里 2 00:00:06,163 --> 00:00:06,740 3 00:00:07,440 --> 00:00:13,460 这个我们把它说成&#xff0c;打开这个&#xff0c;关闭这个 4 00:00:…...

Jenkins发送邮件、定时执行、持续部署

集成Allure报告只需要配置构建后操作即可。但如果是web自动化&#xff0c;或是用HTMLTestRunner生成报告&#xff0c;构建后操作要选择Publish HTML reports&#xff0c;而构建中还要添加Execute system Groovy script插件&#xff0c;内容&#xff1a; System.setProperty(&q…...

Mysql的Cardinality值

什么是Cardinality值&#xff1f; Cardinality值是Mysql做索引优化时一个非常关键的值&#xff0c;优化器会根据这个值来判断是否使用这个索引&#xff0c;它表示索引中唯一值的数目估计值&#xff0c;该值应该尽可能接近1&#xff0c;如果非常小&#xff0c;则用户需要考虑是否…...

数据结构 - 栈和队列

本篇博客将介绍栈和队列的定义以及实现。 1.栈的定义 栈是一种特殊的线性表&#xff0c;只允许在固定的一端进行插入和删除数据&#xff0c;插入数据的一端叫做栈顶&#xff0c;另一端叫做栈底。栈中的数据遵守后进先出的原则 LIFO (Last In First Out)。 插入数据的操作称为压…...

C++:模版进阶 | Priority_queue的模拟实现

创作不易&#xff0c;感谢三连支持 一、非类型模版参数 模板参数分类为类型形参与非类型形参。 类型形参即&#xff1a;出现在模板参数列表中&#xff0c;跟在class或者typename之类的参数类型名称。 非类型形参&#xff0c;就是用一个常量作为类(函数)模板的一个参数&…...

【刷题记录】详谈设计循环队列

下题目为个人的刷题记录&#xff0c;在本节博客中我将详细谈论设计循环队列的思路&#xff0c;并给出代码&#xff0c;有需要借鉴即可。 题目&#xff1a;LINK 循环队列是线性表吗&#xff1f;或者说循环队列是线性结构吗&#xff1f; 对于这个问题&#xff0c;我们来看一下线…...

人工智能|机器学习——k-近邻算法(KNN分类算法)

1.简介 k-最近邻算法&#xff0c;也称为 kNN 或 k-NN&#xff0c;是一种非参数、有监督的学习分类器&#xff0c;它使用邻近度对单个数据点的分组进行分类或预测。虽然它可以用于回归问题&#xff0c;但它通常用作分类算法&#xff0c;假设可以在彼此附近找到相似点。 对于分类…...

乐得瑞 1C to 2C快充线:引领充电数据线新潮流,高效快充解决接口难题

随着科技的不断进步&#xff0c;数据线的接口种类也日渐繁多&#xff0c;但在早些时候&#xff0c;三合一和二合一的数据线因其独特的设计而备受欢迎。这类数据线通常采用USB-A口作为输入端&#xff0c;并集成了Micro USB、Lightning以及USB-C三种接口&#xff0c;满足了当时市…...

第19节 Node.js Express 框架

Express 是一个为Node.js设计的web开发框架&#xff0c;它基于nodejs平台。 Express 简介 Express是一个简洁而灵活的node.js Web应用框架, 提供了一系列强大特性帮助你创建各种Web应用&#xff0c;和丰富的HTTP工具。 使用Express可以快速地搭建一个完整功能的网站。 Expre…...

地震勘探——干扰波识别、井中地震时距曲线特点

目录 干扰波识别反射波地震勘探的干扰波 井中地震时距曲线特点 干扰波识别 有效波&#xff1a;可以用来解决所提出的地质任务的波&#xff1b;干扰波&#xff1a;所有妨碍辨认、追踪有效波的其他波。 地震勘探中&#xff0c;有效波和干扰波是相对的。例如&#xff0c;在反射波…...

多模态2025:技术路线“神仙打架”,视频生成冲上云霄

文&#xff5c;魏琳华 编&#xff5c;王一粟 一场大会&#xff0c;聚集了中国多模态大模型的“半壁江山”。 智源大会2025为期两天的论坛中&#xff0c;汇集了学界、创业公司和大厂等三方的热门选手&#xff0c;关于多模态的集中讨论达到了前所未有的热度。其中&#xff0c;…...

uni-app学习笔记二十二---使用vite.config.js全局导入常用依赖

在前面的练习中&#xff0c;每个页面需要使用ref&#xff0c;onShow等生命周期钩子函数时都需要像下面这样导入 import {onMounted, ref} from "vue" 如果不想每个页面都导入&#xff0c;需要使用node.js命令npm安装unplugin-auto-import npm install unplugin-au…...

Opencv中的addweighted函数

一.addweighted函数作用 addweighted&#xff08;&#xff09;是OpenCV库中用于图像处理的函数&#xff0c;主要功能是将两个输入图像&#xff08;尺寸和类型相同&#xff09;按照指定的权重进行加权叠加&#xff08;图像融合&#xff09;&#xff0c;并添加一个标量值&#x…...

STM32F4基本定时器使用和原理详解

STM32F4基本定时器使用和原理详解 前言如何确定定时器挂载在哪条时钟线上配置及使用方法参数配置PrescalerCounter ModeCounter Periodauto-reload preloadTrigger Event Selection 中断配置生成的代码及使用方法初始化代码基本定时器触发DCA或者ADC的代码讲解中断代码定时启动…...

在 Nginx Stream 层“改写”MQTT ngx_stream_mqtt_filter_module

1、为什么要修改 CONNECT 报文&#xff1f; 多租户隔离&#xff1a;自动为接入设备追加租户前缀&#xff0c;后端按 ClientID 拆分队列。零代码鉴权&#xff1a;将入站用户名替换为 OAuth Access-Token&#xff0c;后端 Broker 统一校验。灰度发布&#xff1a;根据 IP/地理位写…...

k8s业务程序联调工具-KtConnect

概述 原理 工具作用是建立了一个从本地到集群的单向VPN&#xff0c;根据VPN原理&#xff0c;打通两个内网必然需要借助一个公共中继节点&#xff0c;ktconnect工具巧妙的利用k8s原生的portforward能力&#xff0c;简化了建立连接的过程&#xff0c;apiserver间接起到了中继节…...

dify打造数据可视化图表

一、概述 在日常工作和学习中&#xff0c;我们经常需要和数据打交道。无论是分析报告、项目展示&#xff0c;还是简单的数据洞察&#xff0c;一个清晰直观的图表&#xff0c;往往能胜过千言万语。 一款能让数据可视化变得超级简单的 MCP Server&#xff0c;由蚂蚁集团 AntV 团队…...

九天毕昇深度学习平台 | 如何安装库?

pip install 库名 -i https://pypi.tuna.tsinghua.edu.cn/simple --user 举个例子&#xff1a; 报错 ModuleNotFoundError: No module named torch 那么我需要安装 torch pip install torch -i https://pypi.tuna.tsinghua.edu.cn/simple --user pip install 库名&#x…...