2012-03-31 12 views
6

Estaba trabajando en algunos archivos de Excel bastante complejos y encontré un problema con la copia de hojas. Siempre que intente copiar una hoja que no está completamente en blanco que recibo el siguiente mensaje:Problemas para copiar hojas de Excel con JExcel API

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) 

Me pregunto cuál es el problema aquí es. ¿Por qué habría incluso una función ".copySheet (" si no se podía usar para las hojas con información en ellas. En un intento de reproducir el problema en una escala más simple, creé el código que ve a continuación. ver es de 2 hojas idénticas con las células (0,0) que tiene la etiqueta de "prueba". Una hoja denominada "Flujo" el otro "copia". ¿Alguna idea de por qué esto está dando este puntero nulo?

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: Este código también da la misma excepción:

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 de mis ideas de lo que podría estar mal es que, dado que una hoja está abierta y ha sido editado no puede ser copiado Realmente no sé cómo evitar esto.

Respuesta

9

Es un error en jxl-2.6.12.jar, utilice jxl-2.6.10.jar lugar.

Detalles:

typo '& &' en '&'

línea 493 - línea 504 en 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); 
      } 
      } 

línea 540 - línea 551 en 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); 
      } 
      } 

línea 990 - línea 1001 en 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 gracias! Pensé que algo estaba mal con la API, pero no sabía si las versiones anteriores serían útiles. –

+0

@Yourchanges, consúlteme esto (http://stackoverflow.com/questions/17078543/error-occured-in-copying-excel-sheet-with-jexel-api) – Cataclysm

0

la hoja de copia está vacía, añadir algunas células para copiar hoja antes de copiar

+0

Podría estar equivocado, pero creo que sí. Agrego una celda a rSheet que tiene el índice 0. Entonces, cuando llamo a copySheet, está copiando desde la hoja en el índice 0 y colocando la hoja nueva en el índice 0 ¿está bien? He intentado outputBook.copySheet (0, "copy", 1); poner la copia en el índice 1 pero eso da el mismo error. –

Cuestiones relacionadas