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 位置需要最少步数,所以, 当 word1[i] word2[j],dp[i][j] dp[i-1][j-1]; 当 word1[i] ! word2[j],dp[i][j] 1 min(dp[i-1][j-1]…...
R语言的数据类型与数据结构:向量、列表、矩阵、数据框及操作方法
R语言的数据类型与数据结构:向量、列表、矩阵、数据框及操作方法 介绍向量列表矩阵数据框 介绍 R语言拥有丰富的数据类型和数据结构,以满足各类数据处理和分析的需求。本文将分享R语言中的数据类型,包括向量、列表、矩阵、数据框等ÿ…...
第十六章 构建和配置 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_…...
数据中台:数字中国战略关键技术设施
文章目录 每日一句正能量前言为何要建设数据中台数据中台建设痛点数据中台学习资料聚焦前沿,方法论体系更新与时俱进,紧跟时代热点深入6大行业,提炼实践精华大咖推荐,数字化转型必备案头书购买链接赠书活动 每日一句正能量 人生之…...
mac上更改vscode快捷键
以移动当前行代码为例 mac上的vscode,默认移动当前行代码的快捷键是⌥↑即option↑按键 现在我想改成command↑ 步骤如下 1.打开vscode-code-首选项-键盘快捷键 2.打开快捷键列表 3.输入move line,找到要改动的这个快捷键 当前行-右键-更改键绑定&…...
Day18:信息打点-小程序应用解包反编译动态调试抓包静态分析源码架构
目录 小程序获取-各大平台&关键字搜索 小程序体验-凡科建站&模版测试上线 小程序抓包-Proxifier&BurpSuite联动 小程序逆向-解包反编译&动态调试&架构 思维导图 章节知识点 Web:语言/CMS/中间件/数据库/系统/WAF等 系统:操作系…...
真实案例分享:MOS管电源开关电路,遇到上电冲击电流超标
做硬件,堆经验。 分享一个案例:MOS管电源开关电路,遇到上电冲击电流超标,怎么解决的呢? 下面是正文部分。 —— 正文 —— 最近有一颗用了挺久的MOSFET发了停产通知,供应链部门找到我们研发部门,…...
LCR 164. 破解闯关密码
解题思路: 贪心 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
一、背景 列表页面:List组件和Grid组件; 页签切换:Tabs组件; 二、列表页面 在我们常用的手机应用中,经常会见到一些数据列表,如设置页面、通讯录、商品列表等。下图中两个页面都包含列表,“…...
打造经典游戏:HTML5与CSS3实现俄罗斯方块
🌟 前言 欢迎来到我的技术小宇宙!🌌 这里不仅是我记录技术点滴的后花园,也是我分享学习心得和项目经验的乐园。📚 无论你是技术小白还是资深大牛,这里总有一些内容能触动你的好奇心。🔍 &#x…...
什么是系统工程(字幕)48
0 00:00:00,760 --> 00:00:03,550 那这里我们要说一下 1 00:00:04,050 --> 00:00:06,163 你看,刚才我们这里 2 00:00:06,163 --> 00:00:06,740 3 00:00:07,440 --> 00:00:13,460 这个我们把它说成,打开这个,关闭这个 4 00:00:…...
Jenkins发送邮件、定时执行、持续部署
集成Allure报告只需要配置构建后操作即可。但如果是web自动化,或是用HTMLTestRunner生成报告,构建后操作要选择Publish HTML reports,而构建中还要添加Execute system Groovy script插件,内容: System.setProperty(&q…...
Mysql的Cardinality值
什么是Cardinality值? Cardinality值是Mysql做索引优化时一个非常关键的值,优化器会根据这个值来判断是否使用这个索引,它表示索引中唯一值的数目估计值,该值应该尽可能接近1,如果非常小,则用户需要考虑是否…...
数据结构 - 栈和队列
本篇博客将介绍栈和队列的定义以及实现。 1.栈的定义 栈是一种特殊的线性表,只允许在固定的一端进行插入和删除数据,插入数据的一端叫做栈顶,另一端叫做栈底。栈中的数据遵守后进先出的原则 LIFO (Last In First Out)。 插入数据的操作称为压…...
C++:模版进阶 | Priority_queue的模拟实现
创作不易,感谢三连支持 一、非类型模版参数 模板参数分类为类型形参与非类型形参。 类型形参即:出现在模板参数列表中,跟在class或者typename之类的参数类型名称。 非类型形参,就是用一个常量作为类(函数)模板的一个参数&…...
【刷题记录】详谈设计循环队列
下题目为个人的刷题记录,在本节博客中我将详细谈论设计循环队列的思路,并给出代码,有需要借鉴即可。 题目:LINK 循环队列是线性表吗?或者说循环队列是线性结构吗? 对于这个问题,我们来看一下线…...
人工智能|机器学习——k-近邻算法(KNN分类算法)
1.简介 k-最近邻算法,也称为 kNN 或 k-NN,是一种非参数、有监督的学习分类器,它使用邻近度对单个数据点的分组进行分类或预测。虽然它可以用于回归问题,但它通常用作分类算法,假设可以在彼此附近找到相似点。 对于分类…...
乐得瑞 1C to 2C快充线:引领充电数据线新潮流,高效快充解决接口难题
随着科技的不断进步,数据线的接口种类也日渐繁多,但在早些时候,三合一和二合一的数据线因其独特的设计而备受欢迎。这类数据线通常采用USB-A口作为输入端,并集成了Micro USB、Lightning以及USB-C三种接口,满足了当时市…...
ESP32物联网开发终极指南:从Arduino核心到智能硬件实战
ESP32物联网开发终极指南:从Arduino核心到智能硬件实战 【免费下载链接】arduino-esp32 Arduino core for the ESP32 项目地址: https://gitcode.com/GitHub_Trending/ar/arduino-esp32 想要快速构建物联网设备却担心开发难度?ESP32 Arduino核心为…...
终极指南:如何用Meshroom开源工具将普通照片变成专业3D模型
终极指南:如何用Meshroom开源工具将普通照片变成专业3D模型 【免费下载链接】Meshroom Node-based Visual Programming Toolbox 项目地址: https://gitcode.com/gh_mirrors/me/Meshroom 想将手机照片一键变成可旋转、可触摸的3D模型吗?Ƕ…...
OpenClaw与系统环境冲突:Windows/Mac系统兼容问题解决指南
OpenClaw 与系统环境冲突:Windows/Mac 系统兼容问题解决指南引言在当今多平台协作的时代,软件能否在不同操作系统上顺畅运行变得至关重要。OpenClaw,作为一款功能强大的专业工具(例如:数据处理、设计、开发环境等&…...
手把手教你为STM32移植AK09918磁力计驱动(附Linux驱动对比与源码)
从零构建STM32磁力计驱动:AK09918移植实战与Linux对比 在无人机飞控和智能穿戴设备开发中,地磁传感器是实现方向感知的核心部件。AKM公司的AK09918作为三轴磁力计中的佼佼者,以其高精度和低功耗特性受到嵌入式开发者的青睐。但将这颗传感器成…...
LinkSwift:8大网盘直链下载的终极解决方案,告别限速烦恼
LinkSwift:8大网盘直链下载的终极解决方案,告别限速烦恼 【免费下载链接】Online-disk-direct-link-download-assistant 一个基于 JavaScript 的网盘文件下载地址获取工具。基于【网盘直链下载助手】修改 ,支持 百度网盘 / 阿里云盘 / 中国移…...
别再手动翻官网了!用Python脚本自动爬取CKEditor历史漏洞与安全更新(附完整代码)
高效获取CKEditor安全情报:Python自动化爬虫实战指南 每次安全审计前,团队总要花几小时手动翻找CKEditor的漏洞公告?作为经历过这种低效工作模式的安全工程师,我开发了一套自动化解决方案。这个工具不仅能抓取所有历史漏洞&#x…...
Arduino多任务进阶:手把手教你用TaskScheduler实现智能小车避障与巡线‘双模切换’
Arduino多任务实战:智能小车双模切换系统设计与实现 当你的Arduino智能小车需要同时处理避障和巡线功能时,单线程的loop()结构很快就会遇到性能瓶颈。超声波传感器的实时测距与红外传感器的线路检测相互竞争处理器时间,导致响应延迟或功能失效…...
从数据碎片到数字记忆:WeChatMsg如何重构你的微信对话价值
从数据碎片到数字记忆:WeChatMsg如何重构你的微信对话价值 【免费下载链接】WeChatMsg 提取微信聊天记录,将其导出成HTML、Word、CSV文档永久保存,对聊天记录进行分析生成年度聊天报告 项目地址: https://gitcode.com/GitHub_Trending/we/W…...
Typegoose 性能优化:10个技巧让你的数据库查询更快
Typegoose 性能优化:10个技巧让你的数据库查询更快 【免费下载链接】typegoose Typegoose - Define Mongoose models using TypeScript classes. 项目地址: https://gitcode.com/gh_mirrors/ty/typegoose Typegoose 是一个让开发者能够使用 TypeScript 类定义…...
S32K144 MCAL 4.2.1 环境搭建避坑全记录:从EB Tresos Studio到GCC 6.3.1的保姆级教程
S32K144 MCAL 4.2.1 环境搭建实战指南:从零开始构建AutoSAR开发环境 第一次接触S32K144的AutoSAR MCAL开发环境搭建时,我花了整整三天时间才让第一个例程成功运行。这期间经历了License激活失败、GCC版本冲突、路径配置错误等一系列问题。本文将把这些踩…...
