0

I am playing around with the jexcel libary

I have tried to code a small program which does the following:

  1. Read an xls File
  2. Make some computaitons in the sheet and write it to another place

public class DataProcessor {

    private String inputFile;
    private String outputFile;



private Sheet sheet;
    private Workbook w;

    public void setInputFile(String inputFile) {
        this.inputFile = inputFile;
    }

    public void setOutputFile(String outputFile) {
        this.outputFile = outputFile;
    }

    public void read() throws IOException {
        File inputWorkbook = new File(inputFile);
        Workbook w;
        try {
            w = Workbook.getWorkbook(inputWorkbook);
            sheet = w.getSheet(0);
        } catch (BiffException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("deprecation")
    public void write() throws IOException, WriteException {
        File file = new File(inputFile);
        WorkbookSettings wbSettings = new WorkbookSettings();

        wbSettings.setLocale(new Locale("en", "EN"));

        WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
        workbook.createSheet("Lolonator", 0);
        workbook.createSheet("Lolonator123", 1);
        workbook.copy(w);

        workbook.write();
        workbook.close();
    }

    public static void main(String[] args) throws IOException, WriteException {
        ReadExcel test = new ReadExcel();
        test.setInputFile("C:/Users/Desktop/sheet1.xls");
        test.read();
        System.out.println("####################################################");
        System.out.println("File read!");

//      Write
        System.out.println("####################################################");
        System.out.println("Start to write the file!");
        WriteExcel out = new WriteExcel();
        out.setOutputFile("C:/Users/Desktop/sheet2.xls");
        out.write();
        System.out.println("Please check the result file!");

    }

}

However, this does not work. I do not get any output in my sheet, even though my program runs without exception to the end. I really appreciate your answer!!!

Carol.Kar
  • 4,581
  • 36
  • 131
  • 264

1 Answers1

1

In your write function, you are using "inputFile" as parameter to File constructor but you are not initializing it after you create the out object.

So the following line in the write function

File file = new File(inputFile);

should be

File file = new File(outputFile);

Also are you sure that you do not see any errors after running this code. It should be throwing a null pointer exception.

Hope this helps...

Birkan Cilingir
  • 488
  • 4
  • 15
  • I can't see the body of ReadExcel and WriteExcel classes in your post but shouldn't you be creating DataProcessor objects? Also check if your File is created correctly in your write function. – Birkan Cilingir Oct 13 '13 at 13:17
  • Can you surround the code where you call the write function with a try-catch block and print out the exception. It will give you the reason of the problem (File not found, path not found etc.). – Birkan Cilingir Oct 13 '13 at 13:26