forked from eugenejade/JavaTPCProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject04_F.java
More file actions
101 lines (89 loc) · 3.21 KB
/
Project04_F.java
File metadata and controls
101 lines (89 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
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 kr.inflearn.ExcelVO;
public class Project04_F {
public static void main(String[] args) {
String fileName="isbn.xls";
List<ExcelVO> data=new ArrayList<ExcelVO>();
try(FileInputStream fis=new FileInputStream(fileName)) {
HSSFWorkbook workbook=new HSSFWorkbook(fis);
HSSFSheet sheet=workbook.getSheetAt(0);
Iterator<Row> rows=sheet.rowIterator();
rows.next();
String[] imsi=new String[5];
while(rows.hasNext()) {
HSSFRow row=(HSSFRow) rows.next();
Iterator<Cell> cells=row.cellIterator();
int i=0;
while(cells.hasNext()) {
HSSFCell cell=(HSSFCell) cells.next();
imsi[i]=cell.toString();
i++;
if(i==5) break;
}
// 묶고(VO)->담고(List)
ExcelVO vo=new ExcelVO(imsi[0],imsi[1],imsi[2],imsi[3],imsi[4]);
data.add(vo);
}
pdf_maker(data);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void pdf_maker(List<ExcelVO> data) {
String[] headers=new String[] {"제목","저자","출판사","이미지"};
Document doc=new Document(PageSize.A4);
try {
PdfWriter.getInstance(doc, new FileOutputStream(new File("bookList.pdf")));
doc.open();
BaseFont bFont=BaseFont.createFont("NANUMMYEONGJO.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font fontHeader=new Font(bFont, 12);
Font fontRow=new Font(bFont, 10);
PdfPTable table=new PdfPTable(headers.length);
for(String header : headers) {
PdfPCell cell=new PdfPCell();
cell.setGrayFill(0.9f);
cell.setPhrase(new Phrase(header.toUpperCase(), fontHeader));
table.addCell(cell);
}
table.completeRow();
for(ExcelVO vo : data) {
Phrase phrase=new Phrase(vo.getTitle(), fontRow);
table.addCell(new PdfPCell(phrase));
phrase=new Phrase(vo.getAuthor(), fontRow);
table.addCell(new PdfPCell(phrase));
phrase=new Phrase(vo.getCompany(), fontRow);
table.addCell(new PdfPCell(phrase));
Image image=Image.getInstance(vo.getImgurl());
table.addCell(image);
table.completeRow();
}
doc.addTitle("PDF Table Demo");
doc.add(table);
System.out.println("bookList 생성완료");
} catch (Exception e) {
e.printStackTrace();
} finally {
doc.close();
}
}
}