使用Apache POI从excel文件中读取值

时间:2022-04-24 16:00:25

使用Apache POI从excel文件中读取值

Hello everyone,

I want to read the values of documents i shown in an image.
currently, i am using switch-case statement to save the data, but it is very hard to differentiate from 1. Contact to 2. Contact.

我想阅读我在图像中显示的文档的值。目前,我使用switch-case语句来保存数据,但很难区分1.联系人2.联系人。

case "Address Line"
     contact1.setAddressLine(cellIterator.next().getStringCellValue());
case "City"
     contact1.setCity(cellIterator.next().getStringCellValue());
case "County"
     contact1.setCounty(cellIterator.next().getStringCellValue());
...and so on for Address
case "Number of Students"
     contact1.setAddressLine(cellIterator.next().getStringCellValue());
case "Number Of Professors"
     contact1.setAddressLine(cellIterator.next().getStringCellValue());

This works for University and Professors but not for Contact.

这适用于大学和教授,但不适用于联系。

Can anyone guide me how can i do this?

任何人都可以指导我如何做到这一点?

(Sub question: Is it possible to read all values and store it which is not fixed to particular structure ?)

(子问题:是否可以读取所有值并将其存储在未固定到特定结构的位置?)

1 个解决方案

#1


1  

If the structure of the excel file is static you can refer to the values using their coordinates instead of using a cellIterator.

如果excel文件的结构是静态的,您可以使用它们的坐标而不是使用cellIterator来引用这些值。

A cell A1 can be referenced as

单元格A1可以被引用为

Sheet sheet = ...
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);

// Do what you need

So if for example address of contact one is on B5 and address of contact two is in E5

因此,例如,如果联系人的地址是在B5上,而联系人2的地址在E5中

Sheet sheet = ...
Row row = sheet.getRow(4);           // Reference to row 5
Cell cellContact1 = row.getCell(1);  // Reference to cell B5

Cell cellContact2 = row.getCell(4);  // Reference to cell E5

Note:

Use getRow and getCell in an existing excel to read values

在现有excel中使用getRow和getCell来读取值

Use createRow and createCell in a new excel to create values

在新的Excel中使用createRow和createCell来创建值

#1


1  

If the structure of the excel file is static you can refer to the values using their coordinates instead of using a cellIterator.

如果excel文件的结构是静态的,您可以使用它们的坐标而不是使用cellIterator来引用这些值。

A cell A1 can be referenced as

单元格A1可以被引用为

Sheet sheet = ...
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);

// Do what you need

So if for example address of contact one is on B5 and address of contact two is in E5

因此,例如,如果联系人的地址是在B5上,而联系人2的地址在E5中

Sheet sheet = ...
Row row = sheet.getRow(4);           // Reference to row 5
Cell cellContact1 = row.getCell(1);  // Reference to cell B5

Cell cellContact2 = row.getCell(4);  // Reference to cell E5

Note:

Use getRow and getCell in an existing excel to read values

在现有excel中使用getRow和getCell来读取值

Use createRow and createCell in a new excel to create values

在新的Excel中使用createRow和createCell来创建值