<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.0</version>
</dependency>
import com.itextpdf.text.Font;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.poi.ss.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
/**
* @date 2023/11/2
* @description excel转pdf
*/
public class ExcelPdfUtil {
public static void main(String[] args) {
// 示例
method1("D:\\公司文件\\Excel转PDF测试3.xlsx", "D:\\公司文件\\Excel转PDF测试3.pdf", 0, PageSize.A4, true);
}
/**
* Excel转pdf
*
* @param originalFilePath 原始路径
* @param newFilePath 新路径
* @param sheetAtNum 是那个工作簿(0开始)
* @param pageSize 纸张大小,参数如:PageSize.A4
* @param flag 是否横向输出pdf
*/
public static void method1(String originalFilePath, String newFilePath, Integer sheetAtNum, Rectangle pageSize, Boolean flag) {
try (Workbook workbook = WorkbookFactory.create(new File(originalFilePath));
FileOutputStream fos = new FileOutputStream(newFilePath)) {
// 获取第一个工作表
Sheet sheet = workbook.getSheetAt(sheetAtNum);
// 创建PDF文档对象
Document document = new Document(pageSize, 20, 20, 50, 50);
// 横向
if (flag) {
setPageSizeHen(document, pageSize);
} else {
setPageSizeShu(document, pageSize);
}
// 创建PDF输出流
PdfWriter writer = PdfWriter.getInstance(document, fos);
// 打开PDF文档
document.open();
// 创建PDF表格对象
PdfPTable table = new PdfPTable(sheet.getRow(0).getLastCellNum());
table.setHeaderRows(1);
// 方法设置表格列宽
// table.setWidths(new float[] {1, 2, 2, 2});
// 方法设置表格与标题之间的间距
table.setWidthPercentage(100);
// 设置表格标题
Paragraph title = new Paragraph(sheet.getSheetName(), new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 16, Font.BOLD));
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
// 添加表格内容
for (Row row : sheet) {
for (Cell cell : row) {
PdfPCell pdfCell = new PdfPCell(new Paragraph(cell.getStringCellValue(), new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 12)));
pdfCell.setBorderWidth(1f);
pdfCell.setBorderColor(BaseColor.BLACK);
pdfCell.setPadding(5f);
if (cell.getRowIndex() == 0) {
pdfCell.setBackgroundColor(BaseColor.LIGHT_GRAY);
}
table.addCell(pdfCell);
}
}
// 添加表格到PDF文档
table.setSpacingBefore(20f);
table.setSpacingAfter(20f);
table.setKeepTogether(true);
document.add(table);
// 关闭PDF文档
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Excel转pdf
*
* @param workbook workbook工作簿
* @param sheetAtNum 是那个工作簿(0开始)
* @param pageSize 纸张大小,参数如:PageSize.A4
* @param flag 是否横向输出pdf
*/
public static void method2(Workbook workbook, Integer sheetAtNum, Rectangle pageSize, Boolean flag, HttpServletResponse response) {
try {
// 获取第一个工作表
Sheet sheet = workbook.getSheetAt(sheetAtNum);
String sheetName = sheet.getSheetName();
// 创建PDF文档对象
Document document = new Document(pageSize, 20, 20, 50, 50);
// 横向
if (flag) {
setPageSizeHen(document, pageSize);
} else {
setPageSizeShu(document, pageSize);
}
// 创建PDF输出流
String fileName = sheetName + ".pdf";
response.setContentType("application/octet-stream;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
OutputStream os = response.getOutputStream();
// 把pdf写入os
PdfWriter writer = PdfWriter.getInstance(document, os);
// 打开PDF文档
document.open();
// 创建PDF表格对象
PdfPTable table = new PdfPTable(sheet.getRow(0).getLastCellNum());
table.setHeaderRows(1);
// 方法设置表格列宽
// table.setWidths(new float[] {1, 2, 2, 2});
// 方法设置表格与标题之间的间距
table.setWidthPercentage(100);
// 设置表格标题
Paragraph title = new Paragraph(sheetName, new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 16, Font.BOLD));
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
// 添加表格内容
for (Row row : sheet) {
for (Cell cell : row) {
PdfPCell pdfCell = new PdfPCell(new Paragraph(cell.getStringCellValue(), new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 12)));
pdfCell.setBorderWidth(1f);
pdfCell.setBorderColor(BaseColor.BLACK);
pdfCell.setPadding(5f);
if (cell.getRowIndex() == 0) {
pdfCell.setBackgroundColor(BaseColor.LIGHT_GRAY);
}
table.addCell(pdfCell);
}
}
// 添加表格到PDF文档
table.setSpacingBefore(20f);
table.setSpacingAfter(20f);
table.setKeepTogether(true);
document.add(table);
// 关闭PDF文档
document.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置页面横向
*
* @param document document
*/
public static void setPageSizeHen(Document document, Rectangle pageSize) {
//横向
Rectangle rectangle = new Rectangle(pageSize.getHeight(), pageSize.getWidth());
rectangle.rotate();
document.setPageSize(rectangle);
}
/**
* 设置页面竖向
*
* @param document document
*/
public static void setPageSizeShu(Document document, Rectangle pageSize) {
//竖向
Rectangle rectangle = new Rectangle(pageSize.getWidth(), pageSize.getHeight());
rectangle.rotate();
document.setPageSize(rectangle);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/192708.html