excel and Jtable problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vijaykumarsharma
    New Member
    • Nov 2007
    • 12

    excel and Jtable problem

    hi to all.
    i written a action performed method to export table data but it is not working fine.It is creating a excel file but there is no data in it. I am sending the code here. please do favour for me.bye.Have a nice day.

    //////////////////////////////////////////////////////////////////////////////////////////////////////
    export.addActio nListener(new ActionListener( )
    {
    public void actionPerformed (ActionEvent e)
    {
    HSSFWorkbook workbook = new HSSFWorkbook();
    FileOutputStrea m fout=null;
    try{
    fout = new FileOutputStrea m("MyWorkbook.x ls");
    workbook.write( fout);}
    catch (Exception ee)
    {
    }
    HSSFRow[] row=new HSSFRow[table.getRowCou nt()]; HSSFSheet sheet1 = workbook.create Sheet("Sheet One");
    for(int i=0;i<table.get RowCount();i++)
    {
    row[i] = sheet1.createRo w((short) i);
    }
    HSSFCell[][] cell=new HSSFCell[table.getRowCou nt()] [table.getColumn Count()];
    short col=0;
    for(int i=0;i<row.lengt h;i++)
    {
    for(int j=0;j<table.get ColumnCount();j ++){
    cell[i][j]=row[j].createCell(col ++); }
    String str;
    for(int i=0;i<table.get RowCount();i++)
    {
    int k=1;
    for(int j=0;j<table.get ColumnCount()-1;j++)
    {

    str=(String)tab le.getValueAt(i ,k);
    System.out.prin tln("c["+i+"]["+j+"]="+str);
    cell[i][j].setCellValue(s tr);
    k++;
    }
    }
    try
    {
    fout.close();
    }
    catch(Exception ee)
    {
    }


    }
    });
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by vijaykumarsharm a
    hi to all.
    i written a action performed method to export table data but it is not working fine.It is creating a excel file but there is no data in it. I am sending the code here. please do favour for me.bye.Have a nice day.

    //////////////////////////////////////////////////////////////////////////////////////////////////////
    export.addActio nListener(new ActionListener( )
    {
    public void actionPerformed (ActionEvent e)
    {
    HSSFWorkbook workbook = new HSSFWorkbook();
    FileOutputStrea m fout=null;
    try{
    fout = new FileOutputStrea m("MyWorkbook.x ls");
    workbook.write( fout);}
    catch (Exception ee)
    {
    }
    HSSFRow[] row=new HSSFRow[table.getRowCou nt()]; HSSFSheet sheet1 = workbook.create Sheet("Sheet One");
    for(int i=0;i<table.get RowCount();i++)
    {
    row[i] = sheet1.createRo w((short) i);
    }
    HSSFCell[][] cell=new HSSFCell[table.getRowCou nt()] [table.getColumn Count()];
    short col=0;
    for(int i=0;i<row.lengt h;i++)
    {
    for(int j=0;j<table.get ColumnCount();j ++){
    cell[i][j]=row[j].createCell(col ++); }
    String str;
    for(int i=0;i<table.get RowCount();i++)
    {
    int k=1;
    for(int j=0;j<table.get ColumnCount()-1;j++)
    {

    str=(String)tab le.getValueAt(i ,k);
    System.out.prin tln("c["+i+"]["+j+"]="+str);
    cell[i][j].setCellValue(s tr);
    k++;
    }
    }
    try
    {
    fout.close();
    }
    catch(Exception ee)
    {
    }


    }
    });
    1.) Use code tags when posting code
    2.) Don't gobble up exceptions with
    [CODE=java]catch(Exception ee)
    {
    }[/CODE]
    Use [CODE=java]catch(Exception ee)
    {
    ee.printStackTr ace();
    }[/CODE]

    instead so you know if any exceptions were thrown.
    3.) You do have the poi tutorial open on the page where it explains how to create .xls file, right?

    Comment

    Working...