Let me start by saying I’m very new to Java and to this site. I’ve read a book or two now and since then have been looking for small projects to keep myself entertained. I’ve tried to research this but I haven’t been able to find the information I need. With that said, this is my first question here so if this is very early beginner stuff and I’m missing something obvious I apologize.
Regarding the project, my brother-in-law has an issue at work where he has 90 or so Excel workbooks in a folder and he needs to merge the first worksheet of each report into one master workbook. He can do it manually but I thought it would be interesting to try and figure out a way to do it with Java.
I did some research and downloaded the JExcelAPI and added the .jar to my classpath. I’ve created two directories on my computer.
C:\Excel\
C:\Excel\Finished\
Inside C:\Excel\ I’ve created two dummy excel sheets. I’ve renamed the first sheet on each for testing purposes. In the finished folder I’ve created my master document that I intend to merge these sheets into. When the sheets are empty and I run this the sheets appear to get copied over. There are two sheets in the master file and their names correspond to the names I gave them in their respective workbooks so I assume this is working. However, when I add information to one of these sheets and try to run this I get a null pointer exception. I’ve been working on this for hours now so maybe I just need a break but I can’t figure out what’s wrong. I went to the website for JExcelAPI and tried what looks like an outdated method for doing this (before importSheet() existed). That didn’t work either and also returned a null pointer exception.
If someone has time and is familiar with JExcelAPI, could you let me know what’s wrong? I would really appreciate it. I’ve posted the error and my code below.
–Error–
spreadsheet1.xls
Exception in thread "main" java.lang.NullPointerException
at jxl.write.biff.SheetCopier.deepCopyCells(SheetCopier.java:996)
at jxl.write.biff.SheetCopier.importSheet(SheetCopier.java:542)
at jxl.write.biff.WritableSheetImpl.importSheet(WritableSheetImpl.java:2699)
at jxl.write.biff.WritableWorkbookImpl.importSheet(WritableWorkbookImpl.java:1897)
at sheetcopier.SheetCopier.main(SheetCopier.java:32)
–Code–
package sheetcopier;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.NotDirectoryException;
import java.nio.file.Path;
import java.nio.file.Paths;
import jxl.*;
import jxl.read.biff.BiffException;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class SheetCopier {
public static void main(String[] args) throws WriteException, BiffException {
Path inputpath = Paths.get("C:/Excel"); //Directory with excel documents to be copied
File outputfile = new File("C:/ExcelFinished/finishedbook.xls"); //Master/End file
//Read all files from directory
try (DirectoryStream<Path> inputfiles = Files.newDirectoryStream(inputpath)){
//Get a writable workbook
WritableWorkbook writableworkbook = Workbook.createWorkbook(outputfile);
for(Path path: inputfiles)
{
Workbook sourceworkbook = Workbook.getWorkbook(path.toFile()); //Get the source workbook
System.out.println(path.getFileName()); //Print workbook being processed
Sheet readablesheet = sourceworkbook.getSheet(0); //Get the first sheet
writableworkbook.importSheet(readablesheet.getName(), 0, readablesheet); //Import the sheet into the new workbook
//Sheet names are imported if sheets are empty. If sheets are populated I get a null pointer error.
}
writableworkbook.write();
writableworkbook.close();
}
catch(NotDirectoryException e) {
System.err.println(inputpath + " is not a directory." + e);
}
catch(IOException e) {
System.err.println("I/O error." + e);
}
}
}
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
line 540 – line 551 in WritableSheetCopier.java
line 990 – line 1001 in SheetCopier.java