0

I have an error "the method Getworkbook(file) in the type workbook is not applicable for the arguments (string)" in the following code. I precise that there is another class before ReadExcel which opens an internet browser and go till a form. My objective is to fill a form with excel. ANy advices would help, I am at the very beginning of my Java learning.

Thanks,

class ReadExcel {

private String inputFile;

public void setInputFile(String inputFile) {
    this.inputFile = inputFile;
}

public void read() throws IOException  {
    File inputWorkbook = new File(inputFile);
    Workbook w;
    try {
        w = Workbook.getWorkbook("C:\\Users\\mahfo\\eclipse-workspace\\Download\\uketsukebango_list.xlsx");
        // Get the first sheet
        Sheet sheet = w.getSheet("Sheet1");
        // Loop over first 10 column and lines

        for (int j = 0; j < sheet.getColumns(); j++) {
            for (int i = 0; i < sheet.getRows(); i++) {
                Cell cell = sheet.getCell(1, 1);
                CellType type = cell.getType();
                if (type == CellType.LABEL) {
                    System.out.println("I got a label "
                            + cell.getContents());
                }

                if (type == CellType.NUMBER) {
                    System.out.println("I got a number "
                            + cell.getContents());
                }

            }
        }
    } catch (BiffException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) throws IOException {
    ReadExcel test = new ReadExcel();
    test.setInputFile("C:\\Users\\mahfo\\eclipse-workspace\\Download\\uketsukebango_list.xlsx");
    test.read();
}

}

delalma
  • 838
  • 3
  • 12
  • 24
  • Error clearly says that getWorkbook(file) is not applicable for string. It accepts File. Try getWorkbook(new File("your file path")); – Santosh Dec 08 '17 at 02:12

1 Answers1

0

Change your code as below

w = Workbook.getWorkbook(inputWorkbook);

Check out this javadoc

Beaware of IOException. Catch & handle it.

abhi3232
  • 371
  • 1
  • 4
  • 15
  • Thank you. I don't have the error anymore but it does not run properly. Could you elaborate "Beaware of IOException. Catch & handle it" please? I have copied/pasted internet, I am not understand all what is written. Also I have the following message when I run the classe: jxl.read.biff.BiffException: Unable to recognize OLE stream at jxl.read.biff.CompoundFile.(CompoundFile.java:116) at jxl.read.biff.File.(File.java:127) at jxl.Workbook.getWorkbook(Workbook.java:221) – delalma Dec 08 '17 at 03:50
  • at jxl.Workbook.getWorkbook(Workbook.java:198) at com.deposits.ReadExcel.read(FillForm.java:70) at com.deposits.ReadExcel.main(FillForm.java:99) – delalma Dec 08 '17 at 03:50
  • Workbook.getWorkbook throws IOException that's why you have to handle it. Anyways as you are doing pactice on excel it is ok. Check this [stackoverflow link](https://stackoverflow.com/a/12274734/2887739) it will solve this new exception. – abhi3232 Dec 09 '17 at 04:06
  • I would suggest you to use [Apache POI](https://poi.apache.org/spreadsheet) to work with spreadsheets. Here is one [example](https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/) of it. – abhi3232 Dec 09 '17 at 04:11