如何使用iText获取获取单元格值

时间:2022-10-02 21:08:28

Here is an Emp.xls Table :-

这是一个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|
+-------+-------------+---------------+--------+

My problem is that i want to create a PDF file in which the output will be like:-

我的问题是我想创建一个PDF文件,输出结果如下: -

 Hello <name>,

here name can be any of the name from the Emp.xls record, given by the user. How to do solve this problem using iText and Apache poi.

这里的名称可以是用户给出的Emp.xls记录中的任何名称。如何使用iText和Apache poi解决这个问题。

My code is:

我的代码是:

          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();
        }
    }

If it is not possible with iText then pleas suggest me any other open source API Any help is appreciated.

如果用iText不可能,那么请求建议我任何其他开源API任何帮助表示赞赏。

1 个解决方案

#1


7  

According to one of your comments to the original question, you

根据你对原始问题的一个评论,你

try to get cell value and compare with excel name field like:

尝试获取单元格值并与excel名称字段进行比较,如:

switch(cell.getCellType())
{
case Cell.CELL_TYPE_STRING:
    if (cell.getStringCellValue() == "Dean")
        document.add(new Chunk(cell.getStringCellValue()));

I.e. you use == for String comparison.

即你使用==进行字符串比较。

This is something which hardly ever works because in Java == applied to objects checks whether the identical object instance is referenced on both sides. You, on the other hand, only want to check whether the String instances on both sides represent the same sequence of characters. For this task, you should use the equals method:

这几乎不起作用,因为在Java中= =应用于对象检查是否在两侧引用了相同的对象实例。另一方面,您只想检查两侧的String实例是否表示相同的字符序列。对于此任务,您应该使用equals方法:

    if ("Dean".equals(cell.getStringCellValue()))

(I also switched the operands to prevent NullPointerExceptions if cell.getStringCellValue() is null for some reason.)

(如果由于某种原因,cell.getStringCellValue()为null,我也会切换操作数以防止NullPointerExceptions。)

In general using == only makes sense for primitive data types and for objects which you know to be unique in what they represent, e.g. enum objects.

通常使用==仅对原始数据类型和您知道在它们所代表的内容中唯一的对象有意义,例如:枚举对象。

#1


7  

According to one of your comments to the original question, you

根据你对原始问题的一个评论,你

try to get cell value and compare with excel name field like:

尝试获取单元格值并与excel名称字段进行比较,如:

switch(cell.getCellType())
{
case Cell.CELL_TYPE_STRING:
    if (cell.getStringCellValue() == "Dean")
        document.add(new Chunk(cell.getStringCellValue()));

I.e. you use == for String comparison.

即你使用==进行字符串比较。

This is something which hardly ever works because in Java == applied to objects checks whether the identical object instance is referenced on both sides. You, on the other hand, only want to check whether the String instances on both sides represent the same sequence of characters. For this task, you should use the equals method:

这几乎不起作用,因为在Java中= =应用于对象检查是否在两侧引用了相同的对象实例。另一方面,您只想检查两侧的String实例是否表示相同的字符序列。对于此任务,您应该使用equals方法:

    if ("Dean".equals(cell.getStringCellValue()))

(I also switched the operands to prevent NullPointerExceptions if cell.getStringCellValue() is null for some reason.)

(如果由于某种原因,cell.getStringCellValue()为null,我也会切换操作数以防止NullPointerExceptions。)

In general using == only makes sense for primitive data types and for objects which you know to be unique in what they represent, e.g. enum objects.

通常使用==仅对原始数据类型和您知道在它们所代表的内容中唯一的对象有意义,例如:枚举对象。