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

相关推荐

  1. ABAP EXCEL PDF

    2024-03-10 00:24:03       27 阅读
  2. word excel pptpdf

    2024-03-10 00:24:03       21 阅读
  3. docx、excel、wordpdf文件

    2024-03-10 00:24:03       25 阅读
  4. word、excel文件PDF(documents4j方式,简单)

    2024-03-10 00:24:03       38 阅读
  5. Python 读取电子发票PDF Excel

    2024-03-10 00:24:03       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-10 00:24:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-10 00:24:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-10 00:24:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-10 00:24:03       20 阅读

热门阅读

  1. 如何快速的搭建一个小程序

    2024-03-10 00:24:03       25 阅读
  2. unity中摄像机跟随

    2024-03-10 00:24:03       21 阅读
  3. Error running ‘Attach debug to process‘

    2024-03-10 00:24:03       25 阅读
  4. BDD测试框架Cucumber学习

    2024-03-10 00:24:03       23 阅读
  5. C# 的一些好用的语法糖介绍

    2024-03-10 00:24:03       21 阅读
  6. linux脚本练习2-文件压缩删除

    2024-03-10 00:24:03       20 阅读
  7. 铭文资产是比特币生态破局者 or 短暂热点?

    2024-03-10 00:24:03       20 阅读
  8. 数据结构-二分查找

    2024-03-10 00:24:03       22 阅读
  9. 如何更好的理解设计模式之桥接模式

    2024-03-10 00:24:03       21 阅读
  10. golang从0到1实战系统四十:处理表单的输入

    2024-03-10 00:24:03       24 阅读
  11. YUNBBE云贝:PG表空间介绍

    2024-03-10 00:24:03       26 阅读