BAEL-7278 Modify the code based on the feedback
This commit is contained in:
parent
c314363bcf
commit
29c0b906b5
|
@ -3,10 +3,13 @@ package com.baeldung.exceltopdf;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
import org.apache.poi.ss.usermodel.CellStyle;
|
import org.apache.poi.ss.usermodel.CellStyle;
|
||||||
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
||||||
import org.apache.poi.ss.usermodel.IndexedColors;
|
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||||
|
import org.apache.poi.ss.usermodel.VerticalAlignment;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFColor;
|
import org.apache.poi.xssf.usermodel.XSSFColor;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFFont;
|
import org.apache.poi.xssf.usermodel.XSSFFont;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||||
|
@ -14,31 +17,40 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
import org.apache.poi.ss.usermodel.CellType;
|
import org.apache.poi.ss.usermodel.CellType;
|
||||||
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
import org.apache.poi.ss.usermodel.Cell;
|
import org.apache.poi.ss.usermodel.Cell;
|
||||||
|
|
||||||
import com.itextpdf.text.BaseColor;
|
import com.itextpdf.text.BaseColor;
|
||||||
import com.itextpdf.text.Document;
|
import com.itextpdf.text.Document;
|
||||||
import com.itextpdf.text.DocumentException;
|
import com.itextpdf.text.DocumentException;
|
||||||
import com.itextpdf.text.Element;
|
import com.itextpdf.text.Element;
|
||||||
import com.itextpdf.text.Font;
|
import com.itextpdf.text.Font;
|
||||||
|
import com.itextpdf.text.FontFactory;
|
||||||
import com.itextpdf.text.Paragraph;
|
import com.itextpdf.text.Paragraph;
|
||||||
import com.itextpdf.text.Phrase;
|
import com.itextpdf.text.Phrase;
|
||||||
import com.itextpdf.text.pdf.PdfPCell;
|
import com.itextpdf.text.pdf.PdfPCell;
|
||||||
import com.itextpdf.text.pdf.PdfPTable;
|
import com.itextpdf.text.pdf.PdfPTable;
|
||||||
import com.itextpdf.text.pdf.PdfWriter;
|
import com.itextpdf.text.pdf.PdfWriter;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
public class ExcelToPDFConverter {
|
public class ExcelToPDFConverter {
|
||||||
|
|
||||||
|
private static final Logger logger = LogManager.getLogger(ExcelToPDFConverter.class);
|
||||||
|
|
||||||
public static XSSFWorkbook readExcelFile(String excelFilePath) throws IOException {
|
public static XSSFWorkbook readExcelFile(String excelFilePath) throws IOException {
|
||||||
FileInputStream inputStream = new FileInputStream(excelFilePath);
|
FileInputStream inputStream = new FileInputStream(excelFilePath);
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
|
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
|
||||||
inputStream.close();
|
inputStream.close();
|
||||||
return workbook;
|
return workbook;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Document createPDFDocument(String pdfFilePath) throws IOException, DocumentException {
|
private static Document createPDFDocument(String pdfFilePath) throws IOException, DocumentException {
|
||||||
Document document = new Document();
|
Document document = new Document();
|
||||||
PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));
|
PdfWriter.getInstance(document, new FileOutputStream(pdfFilePath));
|
||||||
document.open();
|
document.open();
|
||||||
return document;
|
return document;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void convertExcelToPDF(String excelFilePath, String pdfFilePath) throws IOException, DocumentException {
|
public static void convertExcelToPDF(String excelFilePath, String pdfFilePath) throws IOException, DocumentException {
|
||||||
XSSFWorkbook workbook = readExcelFile(excelFilePath);
|
XSSFWorkbook workbook = readExcelFile(excelFilePath);
|
||||||
Document document = createPDFDocument(pdfFilePath);
|
Document document = createPDFDocument(pdfFilePath);
|
||||||
|
@ -48,6 +60,7 @@ public class ExcelToPDFConverter {
|
||||||
|
|
||||||
// Add header with sheet name as title
|
// Add header with sheet name as title
|
||||||
Paragraph title = new Paragraph(worksheet.getSheetName(), new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD));
|
Paragraph title = new Paragraph(worksheet.getSheetName(), new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD));
|
||||||
|
title.setSpacingAfter(20f);
|
||||||
title.setAlignment(Element.ALIGN_CENTER);
|
title.setAlignment(Element.ALIGN_CENTER);
|
||||||
document.add(title);
|
document.add(title);
|
||||||
|
|
||||||
|
@ -63,7 +76,8 @@ public class ExcelToPDFConverter {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void createAndAddTable(XSSFSheet worksheet, Document document) throws DocumentException, IOException {
|
private static void createAndAddTable(XSSFSheet worksheet, Document document) throws DocumentException, IOException {
|
||||||
PdfPTable table = new PdfPTable(worksheet.getRow(0).getPhysicalNumberOfCells());
|
PdfPTable table = new PdfPTable(worksheet.getRow(0)
|
||||||
|
.getPhysicalNumberOfCells());
|
||||||
table.setWidthPercentage(100);
|
table.setWidthPercentage(100);
|
||||||
addTableHeader(worksheet, table);
|
addTableHeader(worksheet, table);
|
||||||
addTableData(worksheet, table);
|
addTableData(worksheet, table);
|
||||||
|
@ -74,9 +88,27 @@ public class ExcelToPDFConverter {
|
||||||
Row headerRow = worksheet.getRow(0);
|
Row headerRow = worksheet.getRow(0);
|
||||||
for (int i = 0; i < headerRow.getPhysicalNumberOfCells(); i++) {
|
for (int i = 0; i < headerRow.getPhysicalNumberOfCells(); i++) {
|
||||||
Cell cell = headerRow.getCell(i);
|
Cell cell = headerRow.getCell(i);
|
||||||
String headerText = cell.getCellType() == CellType.STRING ? cell.getStringCellValue() : String.valueOf(cell.getNumericCellValue());
|
|
||||||
|
String headerText;
|
||||||
|
switch (cell.getCellType()) {
|
||||||
|
case STRING:
|
||||||
|
headerText = cell.getStringCellValue();
|
||||||
|
break;
|
||||||
|
case NUMERIC:
|
||||||
|
headerText = String.valueOf(BigDecimal.valueOf(cell.getNumericCellValue()));
|
||||||
|
break;
|
||||||
|
case BLANK:
|
||||||
|
headerText = ""; // or null
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
logger.warn("Unsupported cell type: {}", cell.getCellType());
|
||||||
|
headerText = ""; // or throw an exception
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
PdfPCell headerCell = new PdfPCell(new Phrase(headerText, getCellStyle(cell)));
|
PdfPCell headerCell = new PdfPCell(new Phrase(headerText, getCellStyle(cell)));
|
||||||
headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
setBackgroundColor(cell, headerCell);
|
||||||
|
setCellAlignment(cell, headerCell);
|
||||||
table.addCell(headerCell);
|
table.addCell(headerCell);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,42 +123,81 @@ public class ExcelToPDFConverter {
|
||||||
for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
|
for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
|
||||||
Cell cell = row.getCell(i);
|
Cell cell = row.getCell(i);
|
||||||
String cellValue;
|
String cellValue;
|
||||||
if (cell != null) {
|
if (cell.getCellType() == CellType.STRING) {
|
||||||
if (cell.getCellType() == CellType.STRING) {
|
cellValue = cell.getStringCellValue();
|
||||||
cellValue = cell.getStringCellValue();
|
} else if (cell.getCellType() == CellType.NUMERIC) {
|
||||||
} else if (cell.getCellType() == CellType.NUMERIC) {
|
cellValue = String.valueOf(cell.getNumericCellValue());
|
||||||
cellValue = String.valueOf(cell.getNumericCellValue());
|
|
||||||
} else {
|
|
||||||
cellValue = "";
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
cellValue = "";
|
cellValue = "";
|
||||||
}
|
}
|
||||||
PdfPCell cellPdf = new PdfPCell(new Phrase(cellValue, getCellStyle(cell)));
|
PdfPCell cellPdf = new PdfPCell(new Phrase(cellValue, getCellStyle(cell)));
|
||||||
// Set background color
|
setBackgroundColor(cell, cellPdf);
|
||||||
short bgColorIndex = cell.getCellStyle()
|
setCellAlignment(cell, cellPdf);
|
||||||
.getFillForegroundColor();
|
|
||||||
if (bgColorIndex != IndexedColors.AUTOMATIC.getIndex()) {
|
|
||||||
XSSFColor bgColor = (XSSFColor) cell.getCellStyle()
|
|
||||||
.getFillForegroundColorColor();
|
|
||||||
if (bgColor != null) {
|
|
||||||
byte[] rgb = bgColor.getRGB();
|
|
||||||
if (rgb != null && rgb.length == 3) {
|
|
||||||
cellPdf.setBackgroundColor(new BaseColor(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cellPdf.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
||||||
table.addCell(cellPdf);
|
table.addCell(cellPdf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Font getCellStyle(Cell cell) throws DocumentException, IOException {
|
private static void setBackgroundColor(Cell cell, PdfPCell cellPdf) {
|
||||||
Font font = new Font(Font.FontFamily.HELVETICA, 12);
|
// Set background color
|
||||||
|
short bgColorIndex = cell.getCellStyle()
|
||||||
|
.getFillForegroundColor();
|
||||||
|
if (bgColorIndex != IndexedColors.AUTOMATIC.getIndex()) {
|
||||||
|
XSSFColor bgColor = (XSSFColor) cell.getCellStyle()
|
||||||
|
.getFillForegroundColorColor();
|
||||||
|
if (bgColor != null) {
|
||||||
|
byte[] rgb = bgColor.getRGB();
|
||||||
|
if (rgb != null && rgb.length == 3) {
|
||||||
|
cellPdf.setBackgroundColor(new BaseColor(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setCellAlignment(Cell cell, PdfPCell cellPdf) {
|
||||||
CellStyle cellStyle = cell.getCellStyle();
|
CellStyle cellStyle = cell.getCellStyle();
|
||||||
org.apache.poi.ss.usermodel.Font cellFont = cell.getSheet().getWorkbook().getFontAt(cellStyle.getFontIndexAsInt());
|
|
||||||
|
HorizontalAlignment horizontalAlignment = cellStyle.getAlignment();
|
||||||
|
VerticalAlignment verticalAlignment = cellStyle.getVerticalAlignment();
|
||||||
|
|
||||||
|
switch (horizontalAlignment) {
|
||||||
|
case LEFT:
|
||||||
|
cellPdf.setHorizontalAlignment(Element.ALIGN_LEFT);
|
||||||
|
break;
|
||||||
|
case CENTER:
|
||||||
|
cellPdf.setHorizontalAlignment(Element.ALIGN_CENTER);
|
||||||
|
break;
|
||||||
|
case JUSTIFY:
|
||||||
|
case FILL:
|
||||||
|
cellPdf.setVerticalAlignment(Element.ALIGN_JUSTIFIED);
|
||||||
|
break;
|
||||||
|
case RIGHT:
|
||||||
|
cellPdf.setHorizontalAlignment(Element.ALIGN_RIGHT);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (verticalAlignment) {
|
||||||
|
case TOP:
|
||||||
|
cellPdf.setVerticalAlignment(Element.ALIGN_TOP);
|
||||||
|
break;
|
||||||
|
case CENTER:
|
||||||
|
cellPdf.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
||||||
|
break;
|
||||||
|
case JUSTIFY:
|
||||||
|
cellPdf.setVerticalAlignment(Element.ALIGN_JUSTIFIED);
|
||||||
|
break;
|
||||||
|
case BOTTOM:
|
||||||
|
cellPdf.setVerticalAlignment(Element.ALIGN_BOTTOM);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Font getCellStyle(Cell cell) throws DocumentException, IOException {
|
||||||
|
Font font = new Font();
|
||||||
|
CellStyle cellStyle = cell.getCellStyle();
|
||||||
|
org.apache.poi.ss.usermodel.Font cellFont = cell.getSheet()
|
||||||
|
.getWorkbook()
|
||||||
|
.getFontAt(cellStyle.getFontIndexAsInt());
|
||||||
|
|
||||||
short fontColorIndex = cellFont.getColor();
|
short fontColorIndex = cellFont.getColor();
|
||||||
if (fontColorIndex != IndexedColors.AUTOMATIC.getIndex() && cellFont instanceof XSSFFont) {
|
if (fontColorIndex != IndexedColors.AUTOMATIC.getIndex() && cellFont instanceof XSSFFont) {
|
||||||
|
@ -138,6 +209,7 @@ public class ExcelToPDFConverter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cellFont.getItalic()) {
|
if (cellFont.getItalic()) {
|
||||||
font.setStyle(Font.ITALIC);
|
font.setStyle(Font.ITALIC);
|
||||||
}
|
}
|
||||||
|
@ -156,6 +228,16 @@ public class ExcelToPDFConverter {
|
||||||
if (cellFont.getBold()) {
|
if (cellFont.getBold()) {
|
||||||
font.setStyle(Font.BOLD);
|
font.setStyle(Font.BOLD);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String fontName = cellFont.getFontName();
|
||||||
|
if (FontFactory.isRegistered(fontName)) {
|
||||||
|
font.setFamily(fontName); // Use extracted font family if supported by iText
|
||||||
|
} else {
|
||||||
|
logger.warn("Unsupported font type: {}", fontName);
|
||||||
|
// - Use a fallback font (e.g., Helvetica)
|
||||||
|
font.setFamily("Helvetica");
|
||||||
|
}
|
||||||
|
|
||||||
return font;
|
return font;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue