简体   繁体   中英

How to read cells from xlsx

I need to read cells from XLSX. I use Apache POI, but I don't know what the mistake is.

This is my code:

import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

public class miniwolfi {
    public static void main(String[] args) throws IOException {
        File excel = new File("/tmp/table.xlsx");
        FileInputStream fis = new FileInputStream(excel);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
        
        double result = wb.getSheetAt(0).getRow(0).getCell(0).getNumericCellValue();
        System.out.println(result);
        fis.close();
    }
}

And the error

java.io.FileNotFoundException: /tmp/таблица.xlsx (No such file or directory)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
    at miniwolfi.main(miniwolfi.java:17)

Maybe mistake is the way to XLSX. How can I fix this?

Check if the file is xlsx or xls files first, and you have permissions,

Your error is the file is not found

I would write it as follow and use try-with-resources

        File excel = new File("/tmp/table.xlsx");
        try(FileInputStream fis = new FileInputStream(excel);){
           XSSFWorkbook wb = new XSSFWorkbook(fis);
              
           double result = wb.getSheetAt(0).getRow(0).getCell(0).getNumericCellValue();
           System.out.println(result);
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM