Working with excel in java

Hello guys i have tried editing and working with excel in java, but kept getting errors after doing everything as below:

package writer;
import java.io.File;
import java.io.IOException;
import jxl.*;
import jxl.write.*;
import jxl.write.Number;


public class Writer {


    public static void main(String[] args) throws IOException{
        try{

   String fileName = "‪‪‪C:\\Users\\Valentine\\Documents\\NetBeansProjects\\Writer\\src\\Workbook.xls"; 
    WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));
    WritableSheet sheet = workbook.createSheet("Sheet1", 0);
    //Adding A Label
    Label label = new Label(0,0,"A label record");
    sheet.addCell(label); 
    Number number = new Number(0,1,3.1459);
   sheet.addCell(number);
   workbook.write();
   workbook.close();

    }catch (WriteException e){

This is the error an getting

run:
Exception in thread "main" java.io.FileNotFoundException: ‪‪‪C:\Users\Valentine\Documents\NetBeansProjects\Writer\src\Workbook.xls (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
    at jxl.Workbook.createWorkbook(Workbook.java:301)
    at jxl.Workbook.createWorkbook(Workbook.java:286)
    at writer.Writer.main(Writer.java:17)
C:\Users\Valentine\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)

I appreciate your help. thanks

Upvotes: 0

Views: 144

Answers (1)

Trash Can
Trash Can

Reputation: 6824

You need to close the excel file before running the program. Even though the exception is FileNotFoundException, if you have the file opened, and you run the program, it throws that exception which is a little misleading. Your program JVM process tries to lock the file which was locked by MS Excel program, so the exception is understandable

Upvotes: 2

Related Questions