V
V
vitya_brodov2021-10-01 13:30:14
Java
vitya_brodov, 2021-10-01 13:30:14

How to parse a record from Excel?

I have an xlsx file:
6156e1a651538437466152.png
I need to parse and write to a new xlsx file + (parse "Order payment #632-966-417" and write to a new line)
How to implement it?

What I've done:

public static String parseFile(String name) throws IOException {

        String result = "";
        InputStream in = null;
        XSSFWorkbook wb = null;

        try {
            in = new FileInputStream(name);
            wb = new XSSFWorkbook(in);
        }catch (IOException ex){
            ex.printStackTrace();
        }

        Sheet sheet = wb.getSheetAt(0);
        Iterator<Row> it = sheet.iterator();

        while (it.hasNext()){
            Row row = it.next();
            Iterator<Cell> cells = row.iterator();

            while (cells.hasNext()){
                Cell cell = cells.next();
                CellType cellType = cell.getCellType();

                switch (cellType){
                    case STRING:
                        result += cell.getStringCellValue();
                        break;
                    case NUMERIC:
                    case FORMULA:
                        result += " " + cell.getNumericCellValue() + " ";
                        break;
                    default:
                        result += " ";
                        break;
                }
            }
            result += "\n";
        }

        return result;
    }

result:

6156e32f9aca1053734041.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question