2012-03-31 10 views
6

Stavo lavorando su alcuni file di Excel abbastanza complessi e ho riscontrato un problema con i fogli di copia. Ogni volta che tento di copiare un foglio che non è completamente vuoto ricevo il seguente messaggio:Problemi di copia dei fogli excel con l'API JExcel

Exception in thread "main" java.lang.NullPointerException 
    at jxl.write.biff.WritableSheetCopier.shallowCopyCells(WritableSheetCopier.java:499) 
    at jxl.write.biff.WritableSheetCopier.copySheet(WritableSheetCopier.java:239) 
    at jxl.write.biff.WritableSheetImpl.copy(WritableSheetImpl.java:1622) 
    at jxl.write.biff.WritableWorkbookImpl.copySheet(WritableWorkbookImpl.java:987) 
    at excelCalc.main(excelCalc.java:18) 

Mi chiedo quale sia il problema qui è. Perché dovrebbe esserci anche una funzione ".copySheet (" se non può essere usata per i fogli con le informazioni in essi. "Nel tentativo di riprodurre il problema su una scala più semplice ho creato il codice che vedi qui sotto. vedi è 2 fogli identici con cellule (0,0) avere l'etichetta "test". Un foglio denominato "flussi" l'altro, "copia". Tutte le idee perché questo sta dando questo puntatore nullo?

import java.io.File; 

import jxl.*; 
import jxl.write.*; 

public class excelCalc 
{ 
    public static void main(String[] args) throws Exception 
    { 
     WritableWorkbook outputBook = Workbook.createWorkbook(new File("C:/Users/Kevin Brey/CS243/ExcelTest/files/output", "output.xls")); 

     WritableSheet rSheet = outputBook.createSheet("Flows", 0); 

     rSheet.addCell(new Label(0, 0, "test")); 
     outputBook.copySheet(0, "copy", 0); 
     outputBook.write(); 
     outputBook.close(); 
    } 
} 

EDIT: Questo codice dà anche la stessa eccezione:

import java.io.File; 

import jxl.*; 
import jxl.write.*; 

public class excelCalc 
{ 
    public static void main(String[] args) throws Exception 
    { 
     WritableWorkbook outputBook = Workbook.createWorkbook(new File("C:/Users/Kevin Brey/CS243/ExcelTest/files/output", "output.xls")); 

     WritableSheet sheet1 = outputBook.createSheet("Sheet1", 0); 
     WritableSheet sheet2 = outputBook.createSheet("Sheet2", 1); 

     sheet1.addCell(new Label(0, 0, "Label1")); 
     sheet2.addCell(new Label(0, 0, "Label2")); 

     outputBook.copySheet(0, "Copy", 1); 

     outputBook.write(); 
     outputBook.close(); 
    } 
} 

una delle mie idee di quello che potrebbe essere sbagliato è che, poiché un foglio è aperto ed è stato modificato non può essere copiati Io davvero non so come ovviare a questo però.

risposta

9

Si tratta di un bug in jxl-2.6.12.jar, utilizzare al posto JXL-2.6.10.jar.

Dettagli:

errore di battitura '& &' in '&'

linea 493 - linea 504 in WritableSheetCopier.java

if (c != null) 
      { 
      toSheet.addCell(c); 

      // Cell.setCellFeatures short circuits when the cell is copied, 
      // so make sure the copy logic handles the validated cells   
      if (c.getCellFeatures() != null & 
       c.getCellFeatures().hasDataValidation()) 
      { 
       validatedCells.add(c); 
      } 
      } 

linea 540 - linea 551 in WritableSheetCopier.java

if (c != null) 
      { 
      toSheet.addCell(c); 

      // Cell.setCellFeatures short circuits when the cell is copied, 
      // so make sure the copy logic handles the validated cells   
      if (c.getCellFeatures() != null & 
       c.getCellFeatures().hasDataValidation()) 
      { 
       validatedCells.add(c); 
      } 
      } 

Linea 990 - Linea 1001 in SheetCopier.java

if (c != null) 
      { 
      toSheet.addCell(c); 

      // Cell.setCellFeatures short circuits when the cell is copied, 
      // so make sure the copy logic handles the validated cells 
      if (c.getCellFeatures() != null & 
       c.getCellFeatures().hasDataValidation()) 
      { 
       validatedCells.add(c); 
      } 
      } 
+1

wow grazie! Ho capito che c'era qualcosa di sbagliato nell'API, ma non sapevo se le versioni precedenti sarebbero state d'aiuto. –

+0

@Yourchanges, per favore mi controlli per questo (http://stackoverflow.com/questions/17078543/error-occured-in-copying-excel-sheet-with-jexel-api) – Cataclysm

0

il foglio di copia è vuoto, aggiungere alcune cellule di copia foglio prima di copiare

+0

potrei sbagliarmi, ma credo che io sono. Aggiungo una cella a rSheet che ha indice 0. Quindi quando chiamo copySheet sta copiando dal foglio all'indice 0 e mettendo il newSheet all'indice 0 giusto? Ive ha provato outputBook.copySheet (0, "copy", 1); mettere la copia all'indice 1 ma che dà lo stesso errore. –