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 "";

}

}

相关推荐
我命由我1234510 分钟前
TCP 协议相关的参数(KeepAliveTime、KeepAliveInterval、TcpMaxDataRetransmissions)
java·网络·后端·网络协议·tcp/ip·java-ee·信息与通信
落落落sss15 分钟前
spring-data-mongoDB
java·服务器·数据库·后端·python·mongodb·spring
爱吃烤鸡翅的酸菜鱼18 分钟前
Java【网络原理】(2)初识网络续与网络编程
java·网络·后端·java-ee
晚安72020 分钟前
Maven环境搭建
java·maven
sighting_info22 分钟前
maven编译出错,javac: ��Ч��Ŀ�귢�а�: 17
java·数据库·maven
waicsdn_haha23 分钟前
Eclipse IDE 2025-03 最新版安装教程(官方下载+环境配置详解)
java·linux·开发语言·ide·windows·fpga开发·eclipse
秋野酱24 分钟前
基于javaweb的SSM+Maven疫情物业系统设计和实现(源码+文档+部署讲解)
java·spring boot·maven·课程设计
PXM的算法星球42 分钟前
Maven模块化管理:巧用packaging标签优化多模块项目
java·maven
计算机毕设指导61 小时前
基于Springboot医院预约挂号小程序系统【附源码】
java·spring boot·后端·spring·小程序·apache·intellij-idea
m0_748234711 小时前
Java基础进阶提升
java·开发语言