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

}

}

相关推荐
有梦想的骇客5 小时前
书籍“之“字形打印矩阵(8)0609
java·算法·矩阵
yours_Gabriel5 小时前
【java面试】微服务篇
java·微服务·中间件·面试·kafka·rabbitmq
hashiqimiya7 小时前
android studio中修改java逻辑对应配置的xml文件
xml·java·android studio
liuzhenghua667 小时前
Python任务调度模型
java·运维·python
結城7 小时前
mybatisX的使用,简化springboot的开发,不用再写entity、mapper以及service了!
java·spring boot·后端
小前端大牛马7 小时前
java教程笔记(十一)-泛型
java·笔记·python
东阳马生架构8 小时前
商品中心—2.商品生命周期和状态的技术文档
java
星辰离彬8 小时前
Java 与 MySQL 性能优化:MySQL 慢 SQL 诊断与分析方法详解
java·spring boot·后端·sql·mysql·性能优化
q_19132846958 小时前
基于Springboot+Vue的办公管理系统
java·vue.js·spring boot·后端·intellij idea