Praxton February 2016

Data corrupting on program close in jxl

Im using jxl to store data entered in by the user into an excel spreadsheet. I want to be able to close my program while its running and open up the excel spreadsheet and view the data. Whenever I do this, I get a message that says that it is in a different format than specified in the file extension. Here is my code.

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import jxl.*;                                                                    
import jxl.write.*;                                                              
import jxl.Workbook;                                                             
import jxl.read.biff.BiffException;                                              
import jxl.write.Label;                                                          
import jxl.write.Number;                                                         
import jxl.write.WritableSheet;                                                  
import jxl.write.WritableWorkbook;                                               
import jxl.write.WriteException;                                                 
import jxl.write.biff.RowsExceededException;                                     

public class ReadWriteTest                                                       
{
    public static void main(String[] args) throws BiffException, IOException, RowsExceededException, WriteException
    {                                         
        Scanner scan = new Scanner(System.in);

        int i = 1;

        Integer iAlliance = -1;

        File test = new File("C:\\test.xls");

        WritableWorkbook wb = Workbook.createWorkbook(test);

        while(iAlliance != 0 && i != 5)                           
        {   
            WritableSheet sheet = wb.createSheet("Sheet" + i, i);

            iAlliance = scan.nextInt();

            WriteExcel.addNumber(sheet, 0, 0, iAlliance);

            iAlliance = scan.nextInt();

                   

Answers


Ti Strga February 2016

"close my program while its running"

Your code needs to handle that. Right now, it just gets killed at any point (probably in that loop), and nothing after that point happens. The WriteableWorkbook never gets write() or close() called, and the file is probably corrupted.

So see this, try adding some simple output at various places, like "got to line number XXX". Run it, close it, and see how far it got.

You'll probably need to wrap the bulk of your code in a try-catch-finally. The loop is inside the try, the catch handles the lightning-out-of-the-blue program closing from that pesky human at the keyboard, and the finally closes up the workbook file properly.

Post Status

Asked in February 2016
Viewed 2,993 times
Voted 5
Answered 1 times

Search




Leave an answer