Explaination - NgZhengYi/245278-STIW3054-A181-A2 GitHub Wiki
Maven: Apache POI
By using Iterator to traverse through all row in Excel File
Iterator<Row> rowIterator = xssfSheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
}
Since the Excel File contains Paragraph Text and Table
This project will be using static method by identify specific row index to read
if(row.getRowNum() < 4 ) {
// Do something
} else if (row.getRowNum() > 154) {
// Do something
} else {
// Do something
}
When there are numeric on the column, it will be define as double
DataFormatter will helps it to format based on your variable type
DataFormatter dataFormatter = new DataFormatter();
number = dataFormatter.formatCellValue(row.getCell(0));
Maven: iText
Define PDF document with A4 size page and document title
Default path will be saved in project file
Document documentPDF = new Document(PageSize.A4);
PdfWriter.getInstance(documentPDF, new FileOutputStream("Chess.pdf"));
Setting Font for styling document
There are several ways to style your document
Font font = FontFactory.getFont(FontFactory.TIMES, 12, Font.BOLD, BaseColor.BLUE);
There are few methods to set Table Width and Column size
There are absolute method and dynamic method
In this project, we define 6 column with different percent of size and setting table width to be 100%
PdfPTable pdfPTable = new PdfPTable(new float[] {10, 45, 10, 10, 5, 20});
pdfPTable.setWidthPercentage(100); // Default use 80%
pdfPTable.getDefaultCell().setUseAscender(true);
Printing paragraph of Text
// Print row by row
Paragraph paragraph;
for (String text : textArrayList) {
paragraph = new Paragraph(new Phrase(text, font));
paragraph.add(text + "\n");
paragraph.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph);
}
// Store in Paragraph and print at the end
Paragraph paragraph = new Paragraph();
for (String text : textArrayList) {
paragraph.add(text + "\n");
}
document.add(paragraph);
Printing Table using Iterator to traverse through arrayList
Note: The table.addCell amount must equals with the table you defined in creating table column
Iterator<Chess> chessIterator = chessArrayList.iterator();
while (chessIterator.hasNext()) {
Chess chess = chessIterator.next();
pdfPTable.addCell(chess.getNo());
pdfPTable.addCell(chess.getName());
pdfPTable.addCell(chess.getFidelID());
pdfPTable.addCell(chess.getFED());
pdfPTable.addCell(chess.getRtg());
pdfPTable.addCell(chess.getClub());
}