在 Java 中,将 Excel 文件转换为 PDF 可以使用多种方式。以下是几种常见的实现方式,每种方式都包含详细的步骤流程和示例代码。
注意:以下示例代码中的依赖版本可能已过时,建议根据实际情况选择更新的版本。
Apache POI 用于读取 Excel 文件,iText 用于生成 PDF 文件。
步骤流程:
Maven 依赖:
<!-- Apache POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
<!-- iText -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.16</version>
</dependency>
示例代码:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelToPdfConverter {
public static void main(String[] args) throws IOException {
String excelFilePath = "input.xlsx";
String pdfFilePath = "output.pdf";
// Read Excel
FileInputStream excelFile = new FileInputStream(excelFilePath);
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet sheet = workbook.getSheetAt(0);
// Create PDF
PdfWriter writer = new PdfWriter(new FileOutputStream(pdfFilePath));
PdfDocument pdfDocument = new PdfDocument(writer);
Document document = new Document(pdfDocument);
for (Row row : sheet) {
for (Cell cell : row) {
String cellValue = cell.toString();
document.add(new Paragraph(cellValue));
}
}
document.close();
pdfDocument.close();
workbook.close();
System.out.println("Excel converted to PDF successfully!");
}
}
JExcelApi 用于读取 Excel 文件,iText 用于生成 PDF 文件。
步骤流程:
Maven 依赖:
<!-- JExcelApi -->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
<!-- iText (同上) -->
示例代码:
import jxl.*;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelToPdfConverter {
public static void main(String[] args) throws IOException, RowsExceededException, WriteException {
String excelFilePath = "input.xls";
String pdfFilePath = "output.pdf";
// Read Excel
WorkbookSettings settings = new WorkbookSettings();
settings.setSuppressWarnings(true);
Workbook workbook = Workbook.getWorkbook(new FileInputStream(excelFilePath), settings);
Sheet sheet = workbook.getSheet(0);
// Create PDF
PdfWriter writer = new PdfWriter(new FileOutputStream(pdfFilePath));
PdfDocument pdfDocument = new PdfDocument(writer);
Document document = new Document(pdfDocument);
for (int row = 0; row < sheet.getRows(); row++) {
for (int col = 0; col < sheet.getColumns(); col++) {
String cellValue = sheet.getCell(col, row).getContents();
document.add(new Paragraph(cellValue));
}
}
document.close();
pdfDocument.close();
workbook.close();
System.out.println("Excel converted to PDF successfully!");
}
}
这里提供了两种不同的实现方式,你可以根据项目需求和偏好选择适合的方式。