V
V
vitya_brodov2022-04-22 06:08:59
Java
vitya_brodov, 2022-04-22 06:08:59

How to parse an excel curve in java?

Hello!
I get an excel curve (the structure is not convenient for parsing), I need to write all the fields into an array of
excel objects:
62621b428da31759755735.png
my pojo:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class RegisterModel {
    private String date;
    private String suip;
    private String state;
    private String transactionID;
}


service:
public List<RegisterModel> parse(MultipartFile file) throws IOException {
        Workbook workbook = new HSSFWorkbook(file.getInputStream());
        Sheet worksheet = workbook.getSheetAt(0);
        List<RegisterModel> registerModelList = new ArrayList<>();
        for (int i = 3; i < 10; i++) {

            Row row = worksheet.getRow(i);
            RegisterModel registerModel = new RegisterModel();
            String str = String.valueOf(row.getCell(1));
            if (!str.contains("null")) {
//                если дата
                if (str.contains(":")){
                    registerModel.setDate(str);
                }
//                если число
                if (NumberUtils.isDigits(str)){
                    registerModel.setTransactionID(str);
                }
                registerModelList.add(registerModel);
            }
        }
        System.out.println(registerModelList);
        return registerModelList;
    }


result: the data is not completely written to the object, and is repeated.
[RegisterModel(date=2022-03-01T00:58:03, suip=null, state=null, transactionID=null), RegisterModel(date=null, suip=null, state=null, transactionID=502553852624)]


Ps please help me I'm desperate

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2022-04-22
@vitya_brodov

You need to parse both columns.
We parse the first - we understand what needs to be done with the second.
1. If the first one contains something like "oper_//d*" - create a new RegisterModel object, write the old one (if any) to registerModelList.
2. If something like OPERDAY - parse the second column, try to pull out the date (you can make several options for the date format, if necessary)
3. If SUITE, STATE, NUM_ORDER - similarly.
I would write some separate method that will parse the data from the first column and return commands from the enam.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question