Load java file

Asked

Viewed 88 times

-1

I need to upload a file . xlsx that is in a directory to my tool.

What API or implementation can I use to upload?

1 answer

1

You can use the public Apache POI for that reason

Below is an example of how you can upload a file. xlsx with Apache POI library, later you can save it in a directory you find more convenient.

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

public class ExcelReader {
    public static final String XLSX_FILE_PATH = "./arquivo-excel.xlsx";

    public static void main(String[] args) throws IOException, InvalidFormatException {

        //Criando um Workbook de Uma Arquivo Excel (.xlsx ou .xls)
        Workbook workbook = WorkbookFactory.create(new File(XLSX_FILE_PATH));

        //Visualizando Todas as Sheets presentes na planilha
        for(Sheet sheet: workbook) {
            System.out.println("=> " + sheet.getSheetName());
        }


        // Visualizando os valores das células da primeira aba da planilha
        Sheet sheet = workbook.getSheetAt(0);

        for (Row row: sheet) {
            for(Cell cell: row) {
                String cellValue = dataFormatter.formatCellValue(cell);
                System.out.print(cellValue + "\t");
            }
            System.out.println();
        }

        workbook.close();
    }
}

Apache POI terminology

  • Workbook: A workbook is the high-level representation of a worksheet.

  • Sheet: Represents a worksheet tab. A workbook can contain many tabs.

  • Row: As the name suggests, represents a line in the spreadsheet.

  • Cell: a cell represents an element in the worksheet column.

  • Thanks for the alternative, I used and it worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.