Этот пост показывает, как вы можете использовать jXLS для анализа файла Excel в списке JavaBeans.
Вот общий метод утилиты, который я написал для этого:
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
|
/*** Parses an excel file into a list of beans.** @param <T> the type of the bean* @param xlsFile the excel data file to parse* @param jxlsConfigFile the jxls config file describing how to map rows to beans* @return the list of beans or an empty list there are none* @throws Exception if there is a problem parsing the file*/public static <T> List<T> parseExcelFileToBeans(final File xlsFile, final File jxlsConfigFile) throws Exception { final XLSReader xlsReader = ReaderBuilder.buildFromXML(jxlsConfigFile); final List<T> result = new ArrayList<>(); final Map<String, Object> beans = new HashMap<>(); beans.put("result", result); try (InputStream inputStream = new BufferedInputStream(new FileInputStream(xlsFile))) { xlsReader.read(inputStream, beans); } return result;} |
Пример:
Рассмотрим следующий файл Excel, содержащий информацию о человеке:
| Имя | Фамилия | Возраст |
| Джо | Bloggs | 25 |
| Джон | лань | 30 |
Создайте следующий компонент Person чтобы привязать каждую строку Excel к:
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package model;public class Person { private String firstName; private String lastName; private int age; public Person() { } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }} |
Создайте файл конфигурации jXLS, который сообщает jXLS, как обрабатывать файл Excel и сопоставлять строки с объектами Person :
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
<workbook> <worksheet name="Sheet1"> <section startRow="0" endRow="0" /> <loop startRow="1" endRow="1" items="result" var="person" varType="model.Person"> <section startRow="1" endRow="1"> <mapping row="1" col="0">person.firstName</mapping> <mapping row="1" col="1">person.lastName</mapping> <mapping row="1" col="2">person.age</mapping> </section> <loopbreakcondition> <rowcheck offset="0"> <cellcheck offset="0" /> </rowcheck> </loopbreakcondition> </loop> </worksheet></workbook> |
Теперь вы можете разобрать файл Excel в список объектов Person с помощью одной строки:
|
1
2
|
List<Person> persons = Utils.parseExcelFileToBeans(new File("/path/to/personData.xls"), new File("/path/to/personConfig.xml")); |
Похожие сообщения:
Разбор файла CSV в JavaBeans с использованием OpenCSV
| Ссылка: | Разбор файла Excel в JavaBeans с использованием jXLS от нашего партнера по JCG Фахда Шарифа в блоге fahd.blog . |