Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Create a New Excel File Using JXL?

Tags:

java

jxl

I'm trying to create a new Excel file using jxl, but am having a hard time finding examples in their API documentation and online.

like image 711
Aaron Avatar asked Sep 29 '08 21:09

Aaron


3 Answers

I know that it's a very old question. However, I think I can contribute with an example that also adds the cell values:

/**
 *
 * @author Almir Campos
 */
public class Write01
{
    public void test01() throws IOException, WriteException
    {
        // Initial settings
        File file = new File( "c:/tmp/genexcel.xls" );
        WorkbookSettings wbs = new WorkbookSettings();
        wbs.setLocale( new Locale( "en", "EN" ) );
        // Creates the workbook
        WritableWorkbook wwb = Workbook.createWorkbook( file, wbs );
        // Creates the sheet inside the workbook
        wwb.createSheet( "Report", 0 );
        // Makes the sheet writable
        WritableSheet ws = wwb.getSheet( 0 );
        // Creates a cell inside the sheet
        //CellView cv = new CellView();
        Number n;
        Label l;
        Formula f;
        for ( int i = 0; i < 10; i++ )
        {
            // A
            n = new Number( 0, i, i );
            ws.addCell( n );
            // B
            l = new Label( 1, i, "by" );
            ws.addCell( l );
            // C
            n = new Number( 2, i, i + 1 );
            ws.addCell( n );
            // D
            l = new Label( 3, i, "is" );
            ws.addCell( l );
            // E
            f = new Formula(4, i, "A" + (i+1) + "*C" + (i+1) );
            ws.addCell( f );
        }
        wwb.write();
        wwb.close();
    }
}
like image 142
Almir Campos Avatar answered Nov 08 '22 07:11

Almir Campos


After messing around awhile longer I finally found something that worked and saw there still wasn't a solution posted here yet, so here's what I found:

try {
    String fileName = "file.xls";
    WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));
    workbook.createSheet("Sheet1", 0);
    workbook.createSheet("Sheet2", 1);
    workbook.createSheet("Sheet3", 2);
    workbook.write();
    workbook.close();
} catch (WriteException e) {

}
like image 37
Aaron Avatar answered Nov 08 '22 07:11

Aaron


First of all you need to put Jxl Api into your java directory , download JXL api from http://www.andykhan.com/ extract it,copy jxl and paste like C:\Program Files\Java\jre7\lib\ext.

try {
    String fileName = "file.xls";
    WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));
    WritableSheet writablesheet1 = workbook.createSheet("Sheet1", 0);
    WritableSheet writablesheet2 = workbook.createSheet("Sheet2", 1);
    WritableSheet writablesheet3 = workbook.createSheet("Sheet3", 2);
    Label label1 = new Label("Emp_Name");
    Label label2 = new Label("Emp_FName");
    Label label3 = new Label("Emp_Salary");
    writablesheet1.addCell(label1);
    writablesheet2.addCell(label2);
    writablesheet3.addCell(label3);
    workbook.write();
    workbook.close();
} catch (WriteException e) {

}
like image 2
Zaheer Astori Avatar answered Nov 08 '22 06:11

Zaheer Astori