1
package Mundo;

import java.io.*;
import jxl.*;
import jxl.read.biff.BiffException;
import jxl.write.*; 

public class ExcelIO
{
  public static void main(String[] args) throws BiffException
  {
    toExcel("testName", "65", "180", "80", "120", "36");

    leerExcel();

}

public static void toExcel(String name, String weight, String height, String systolic, String diastolic, String temp) throws BiffException
{
    try
    {
        File inp = new File("Trina.xls");
        File out = new File("Trina.xls");

        // Abre una copia del archivo de solo lectura
        Workbook existingWorkbook = Workbook.getWorkbook(inp);
        // Abre un archivo que se puede editar
        WritableWorkbook workbook = Workbook.createWorkbook(out,existingWorkbook);


        //Revisar celda disponible
        int i = leerExcel();

        //Se obtiene la primera hoja
        WritableSheet sheet = workbook.getSheet(0); 
        WritableCell cell = sheet.getWritableCell(0, i);  



        //Creamos celdas de varios tipos
        sheet.addCell(new jxl.write.Label(0, i, name));
        sheet.addCell(new jxl.write.Number(1, i, Double.parseDouble(weight)));
        sheet.addCell(new jxl.write.Number(2, i, Double.parseDouble(height)));
        sheet.addCell(new jxl.write.Number(3, i, Double.parseDouble(systolic)));
        sheet.addCell(new jxl.write.Number(4, i, Double.parseDouble(diastolic)));
        sheet.addCell(new jxl.write.Number(5, i, Double.parseDouble(temp)));



        //Escribimos los resultados al fichero Excel
        // Important: Close it before writing the copy with copy.write();
        existingWorkbook.close();    
        //inp.close();
        workbook.write();
        workbook.close();

    }
    catch (IOException ex)
    {
        System.out.println("Error al crear el fichero.");
    }
    catch (WriteException ex)
    {
        System.out.println("Error al escribir el fichero.");
    }
}


public static int leerExcel()
{

    int i = 0;

    try
    {
        //Se abre el fichero Excel
        Workbook workbook = Workbook.getWorkbook(new File("Trina.xls")); 

        //Se obtiene la primera hoja
        Sheet sheet = workbook.getSheet(0); 

        //Revisa que la primera celda es "Nombre"
        Cell cell = sheet.getCell(0,0);

            while(!cell.getContents().isEmpty())
            {
                //Se obtiene la celda i-esima
                cell = sheet.getCell(i,0);
                i ++;
            }
        }

    catch (IOException | BiffException | IndexOutOfBoundsException ex)
    {
        System.out.println("Error!" + ex);
    }

    return i;
}
}

This is a class i´ve been trying to create to read an excel file and editing. The problem is that I keep getting whis error:

 Error!jxl.read.biff.BiffException: The input file was not found
 Error!java.lang.ArrayIndexOutOfBoundsException: 6

And the file is there, i've double checked, the program just overwrites the previous file every time

Gabriel Luque
  • 117
  • 11

1 Answers1

1

Your problem is that you use both the same in and out paths:

File inp = new File("Trina.xls");
File out = new File("Trina.xls");

Try to change to: File out = new File("Trina_NEW.xls") and after running, actualize your working folder to notice the new created excel (mouse over your project folder in eclipse (package explorer view), and then "F5".

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Paul Efford
  • 261
  • 4
  • 12