Trouble copying excel sheets with JExcel API
Asked Answered
F

2

8

I was working on some fairly complex excel files and ran into a problem with copying sheets. Whenever I attempt to copy a sheet that is not completely blank i recieve the following message:

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)

I am wondering what the problem here is. Why would there even be a ".copySheet(" function if it couldnt be used for sheets with information in them. In an attempt to reproduce the problem on a more simple scale I created the code you see below. The output I would expect to see is 2 identical sheets with cell(0,0) having the label "test". One sheet named "Flows" the other, "copy". Any ideas as to why this is giving this null pointer?

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: This code also gives the same exception:

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();
    }
}

One of my ideas of what could be wrong is that since a sheet is open and has been edited it cannot be copied. I really don't know how to get around this though.

Fichtean answered 31/3, 2012 at 20:36 Comment(0)
T
10

It's a bug in jxl-2.6.12.jar, use jxl-2.6.10.jar instead.

Details:

typo '&&' into '&'

line 493 - line 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);
            }
          }

line 540 - line 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);
            } 
          }

line 990 - line 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);
            }
          }
Tatouay answered 11/4, 2012 at 5:51 Comment(2)
wow thank you! I figured something was wrong with the API but I didn't know if previous versions would help.Fichtean
@Yourchanges, Please check me for this (#17079043)Batrachian
W
0

the copy sheet is empty, add some cells to copy sheet before copying

Wassyngton answered 31/3, 2012 at 21:45 Comment(1)
I could be wrong but I think I am. I add a cell to rSheet which has index 0. Then when I call copySheet it is copying from the sheet at index 0 and putting the newSheet at index 0 right? Ive tried outputBook.copySheet(0, "copy", 1); to put the copy at index 1 but that gives the same error.Fichtean

© 2022 - 2024 — McMap. All rights reserved.