目錄
??前言:
??Excel轉(zhuǎn)pdf
??進(jìn)一步優(yōu)化代碼:
??進(jìn)一步優(yōu)化代碼:?
??依賴:
??前言:
? ? 加油? 少年
??Excel轉(zhuǎn)pdf
-
使用try-with-resources語(yǔ)句自動(dòng)關(guān)閉資源,避免手動(dòng)關(guān)閉資源時(shí)出現(xiàn)的錯(cuò)誤。
-
使用POI庫(kù)的Sheet.getRow()方法獲取行對(duì)象,避免強(qiáng)制類型轉(zhuǎn)換。
-
使用POI庫(kù)的Cell.getStringCellValue()方法獲取單元格的字符串值,避免使用toString()方法時(shí)出現(xiàn)的錯(cuò)誤。
-
使用iText庫(kù)的PdfPCell對(duì)象設(shè)置單元格的字體和邊框等屬性,使PDF表格更美觀。
-
使用iText庫(kù)的Paragraph對(duì)象設(shè)置表格標(biāo)題的字體和對(duì)齊方式,使PDF文檔更美觀。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.ss.usermodel.Header;
import org.apache.poi.ss.usermodel.PageOrientation;
import org.apache.poi.ss.usermodel.PaperSize;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.PrintArea;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.BaseFont;
public class ExcelToPdfDemo {
public static void main(String[] args) {
try (Workbook workbook = WorkbookFactory.create(new File("example.xlsx"));
FileOutputStream fos = new FileOutputStream("example.pdf")) {
// 獲取第一個(gè)工作表
Sheet sheet = workbook.getSheetAt(0);
// 創(chuàng)建PDF文檔對(duì)象
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
// 創(chuàng)建PDF輸出流
PdfWriter writer = PdfWriter.getInstance(document, fos);
// 打開PDF文檔
document.open();
// 創(chuàng)建PDF表格對(duì)象
PdfPTable table = new PdfPTable(sheet.getRow(0).getLastCellNum());
// 設(shè)置表格寬度
table.setWidthPercentage(100);
// 設(shè)置表格標(biāo)題
Paragraph title = new Paragraph("Example Table", new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 16, Font.BOLD));
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
// 添加表格內(nèi)容
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.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(pdfCell);
}
}
// 添加表格到PDF文檔
document.add(table);
// 關(guān)閉PDF文檔
document.close();
} catch (IOException | DocumentException e) {
e.printStackTrace();
}
}
}
- 上面的示例代碼中,我們使用try-with-resources語(yǔ)句自動(dòng)關(guān)閉資源,使用Sheet.getRow()方法獲取行對(duì)象,使用Cell.getStringCellValue()方法獲取單元格的字符串值,使用PdfPCell對(duì)象設(shè)置單元格的字體和邊框等屬性,使用Paragraph對(duì)象設(shè)置表格標(biāo)題的字體和對(duì)齊方式,使PDF文檔更美觀。
??進(jìn)一步優(yōu)化代碼:
-
使用POI庫(kù)的Workbook.getNumberOfSheets()方法獲取工作簿中的工作表數(shù)量,避免手動(dòng)指定工作表編號(hào)。
-
使用iText庫(kù)的PdfPTable.setHeaderRows()方法設(shè)置表格標(biāo)題行,使表格更易讀。
-
使用iText庫(kù)的PdfPTable.setWidths()方法設(shè)置表格列寬,使表格更美觀。
-
使用iText庫(kù)的PdfPTable.setSpacingBefore()方法設(shè)置表格與標(biāo)題之間的間距,使PDF文檔更美觀。
-
使用iText庫(kù)的PdfPTable.setSpacingAfter()方法設(shè)置表格與下一個(gè)元素之間的間距,使PDF文檔更美觀。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.ss.usermodel.Header;
import org.apache.poi.ss.usermodel.PageOrientation;
import org.apache.poi.ss.usermodel.PaperSize;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.PrintArea;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.BaseFont;
public class ExcelToPdfDemo {
public static void main(String[] args) {
try (Workbook workbook = WorkbookFactory.create(new File("example.xlsx"));
FileOutputStream fos = new FileOutputStream("example.pdf")) {
// 獲取第一個(gè)工作表
Sheet sheet = workbook.getSheetAt(0);
// 創(chuàng)建PDF文檔對(duì)象
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
// 創(chuàng)建PDF輸出流
PdfWriter writer = PdfWriter.getInstance(document, fos);
// 打開PDF文檔
document.open();
// 創(chuàng)建PDF表格對(duì)象
PdfPTable table = new PdfPTable(sheet.getRow(0).getLastCellNum());
table.setHeaderRows(1);
table.setWidths(new float[] {1, 2, 2, 2});
// 設(shè)置表格寬度
table.setWidthPercentage(100);
// 設(shè)置表格標(biāo)題
Paragraph title = new Paragraph("Example Table", new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 16, Font.BOLD));
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
// 添加表格內(nèi)容
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.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(pdfCell);
}
}
// 添加表格到PDF文檔
table.setSpacingBefore(20f);
table.setSpacingAfter(20f);
document.add(table);
// 關(guān)閉PDF文檔
document.close();
} catch (IOException | DocumentException e) {
e.printStackTrace();
}
}
}
- 在上面的示例代碼中,我們使用Workbook.getNumberOfSheets()方法獲取工作簿中的工作表數(shù)量,使用PdfPTable.setHeaderRows()方法設(shè)置表格標(biāo)題行,使用PdfPTable.setWidths()方法設(shè)置表格列寬,使用PdfPTable.setSpacingBefore()方法設(shè)置表格與標(biāo)題之間的間距,使用PdfPTable.setSpacingAfter()方法設(shè)置表格與下一個(gè)元素之間的間距, 。
??進(jìn)一步優(yōu)化代碼:?
-
使用POI庫(kù)的Workbook.getSheetName()方法獲取工作表名稱,避免手動(dòng)指定工作表編號(hào)。
-
使用iText庫(kù)的PdfPTable.setKeepTogether()方法設(shè)置表格是否在同一頁(yè)顯示,避免表格被分頁(yè)顯示。
-
使用iText庫(kù)的PdfPCell.setPadding()方法設(shè)置單元格的內(nèi)邊距,使表格更美觀。
-
使用iText庫(kù)的PdfPCell.setBackgroundColor()方法設(shè)置單元格的背景顏色,使表格更美觀。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-700702.html
-
使用iText庫(kù)的PdfPCell.setBorderColor()方法設(shè)置單元格的邊框顏色,使表格更美觀。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-700702.html
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.ss.usermodel.Header;
import org.apache.poi.ss.usermodel.PageOrientation;
import org.apache.poi.ss.usermodel.PaperSize;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.PrintArea;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.BaseColor;
public class ExcelToPdfDemo {
public static void main(String[] args) {
try (Workbook workbook = WorkbookFactory.create(new File("example.xlsx"));
FileOutputStream fos = new FileOutputStream("example.pdf")) {
// 獲取第一個(gè)工作表
Sheet sheet = workbook.getSheetAt(0);
// 創(chuàng)建PDF文檔對(duì)象
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
// 創(chuàng)建PDF輸出流
PdfWriter writer = PdfWriter.getInstance(document, fos);
// 打開PDF文檔
document.open();
// 創(chuàng)建PDF表格對(duì)象
PdfPTable table = new PdfPTable(sheet.getRow(0).getLastCellNum());
table.setHeaderRows(1);
table.setWidths(new float[] {1, 2, 2, 2});
// 設(shè)置表格寬度
table.setWidthPercentage(100);
// 設(shè)置表格標(biāo)題
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);
// 添加表格內(nèi)容
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);
// 關(guān)閉PDF文檔
document.close();
} catch (IOException | DocumentException e) {
e.printStackTrace();
}
}
}
- 上面的示例代碼中,我們使用Workbook.getSheetName()方法獲取工作表名稱,使用PdfPTable.setKeepTogether()方法設(shè)置表格是否在同一頁(yè)顯示,使用PdfPCell.setPadding()方法設(shè)置單元格的內(nèi)邊距,使用PdfPCell.setBackgroundColor()方法設(shè)置單元格的背景顏色,使用PdfPCell.setBorderColor()方法設(shè)置單元格的邊框顏色, 。
??依賴:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
iText庫(kù):用于創(chuàng)建和操作PDF文件。
Apache POI庫(kù):用于讀取和寫入Excel文件。
到了這里,關(guān)于[Java 實(shí)現(xiàn)Excel轉(zhuǎn)pdf ] 篇1的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!