2015-09-13 3 views
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; 
} 
} 

Ceci est une classe que j'ai essayé de créer pour lire un fichier Excel et éditer. Le problème est que je continue à obtenir l'erreur de whis:Jexcel éditant le fichier existant

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

Et le fichier est là, je l'ai vérifié deux fois, le programme écrase tout le fichier précédent à chaque fois que

Répondre

1

Votre problème est que vous utilisez à la fois la même et des chemins:

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

Essayez de changer à: File out = new File("Trina_NEW.xls") et après l'exécution, actualiser votre dossier de travail à remarquer le nouveau créé Excel (souris sur votre dossier de projet Eclipse (vue package explorer), puis " F5 ".