Carol.Kar
Carol.Kar

Reputation: 5345

Read an excel sheet in, process it and output it

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!!!

Upvotes: 0

Views: 398

Answers (1)

Birkan Cilingir
Birkan Cilingir

Reputation: 488

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...

Upvotes: 1

Related Questions