2012-03-31 13 views
6

Tôi đang làm việc trên một số tệp excel khá phức tạp và gặp sự cố với việc sao chép trang tính. Bất cứ khi nào tôi cố gắng sao chép một trang tính không hoàn toàn trống, tôi nhận được thông báo sau:Sự cố khi sao chép các trang tính excel bằng 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) 

Tôi tự hỏi vấn đề ở đây là gì. Tại sao thậm chí sẽ có một hàm ".copySheet (" nếu nó không thể được sử dụng cho các trang tính với thông tin trong chúng. Trong một nỗ lực để tạo lại vấn đề trên một quy mô đơn giản hơn, tôi đã tạo mã mà bạn thấy bên dưới. xem là 2 tờ giống hệt nhau với ô (0,0) có nhãn "thử nghiệm" .Trang tính có tên là "Dòng", còn lại là "sao chép" .Tất cả ý tưởng về lý do tại sao điều này cho con trỏ rỗng này?

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: mã này cũng đưa ra cùng một ngoại lệ:

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

một trong những ý tưởng của tôi về những gì có thể sai là vì một tờ được mở và đã được chỉnh sửa nó không thể được sao chép. Tôi thực sự không biết làm thế nào để có được xung quanh điều này mặc dù.

Trả lời

9

Đó là một lỗi trong jxl-2.6.12.jar, sử dụng jxl-2.6.10.jar để thay thế.

chi tiết:

typo '& &' thành '&'

dòng 493 - dòng 504 trong 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); 
      } 
      } 

dòng 540 - dòng 551 trong 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); 
      } 
      } 

dòng 990 - dòng 1001 trong 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 cảm ơn bạn! Tôi đã tìm thấy điều gì đó sai với API nhưng tôi không biết liệu các phiên bản trước có hữu ích hay không. –

+0

@Yourchanges, Vui lòng kiểm tra cho tôi (http://stackoverflow.com/questions/17078543/error-occured-in-copying-excel-sheet-with-jexel-api) – Cataclysm

0

tờ bản là trống rỗng, thêm một số tế bào để sao chép tờ trước khi sao chép

+0

Tôi có thể sai nhưng tôi nghĩ là vậy. Tôi thêm một ô vào rSheet có chỉ số 0. Sau đó, khi tôi gọi copySheet nó được sao chép từ trang tính tại chỉ mục 0 và đặt newSheet tại chỉ mục 0 phải không? Ive đã thử outputBook.copySheet (0, "copy", 1); để đặt bản sao tại chỉ mục 1 nhưng điều đó cho cùng một lỗi. –