这是一个 Emp.xls 表:-
+-------+-------------+---------------+--------+
| EmpId | Name | Designation | Salary|
+-------+-------------+---------------+--------+
| 1.0 | Akon Roy | project led | 12000.0|
| 2.0 | Brey Deo | manager | 13000.0|
| 3.0 | Dean | delivery head | 14000.0|
| 4.0 | Clark | team led |155555.0|
+-------+-------------+---------------+--------+
我的问题是我想创建一个PDF 文件,其中的输出如下:-
Hello <name>,
这里的名称可以是用户给出的 Emp.xls 记录中的任何名称。如何使用iText和Apache poi解决这个问题。
我的代码是:
public static void main(String[] args) {
try {
FileInputStream inputFile = new FileInputStream("E:\\Emp.xls");
HSSFWorkbook workbook = new HSSFWorkbook(inputFile);
HSSFSheet sheet = workbook.getSheetAt(0);
//adding row iterator
Iterator<Row> rowitr = sheet.iterator();
OutputStream file = new FileOutputStream(new File("E:\\Test.pdf"));
Document document = new Document();
PdfWriter.getInstance(document, file);
document.open();
document.add(new Paragraph("Hello World, iText"));
//document.add(new Paragraph("Dear "));
while(rowitr.hasNext()){
Row row = rowitr.next();
//adding cell iterator
Iterator<Cell> cellitr = row.iterator();
while(cellitr.hasNext()){
Cell cell = cellitr.next();
switch(cell.getCellType()){
case Cell.CELL_TYPE_STRING:
if(cell.getStringCellValue == "Dean")
document.add(new Chunk("Dear " cell.getStringCellValue()+ ","));
}
}
}
document.close();
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
如果 iText 无法使用,请向我推荐任何其他开源 API 任何帮助,我们将不胜感激。