Exporting JTable records to Ms-Excel

HiCould anybody help me on this issue?I Would like to export the record which are displayed on the JTable to Excel program.Please help me by giving some ideas or Proper Materials LinkThank yourowinraj
[242 byte] By [rowinraja] at [2007-11-26 19:01:35]
# 1
Try out Jakarta POIThere is a super tutorial here that does exactly what you are after (exporting a JTable to excel and vice versa): http://www.ociweb.com/jnb/jnbMar2004.htmland of course the FAQ and get started stuff at Jakarta POI.It should
fireman.sparkeya at 2007-7-9 20:46:23 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi

Could you please tell how to set CLASSPATH for poi-2.0-final-20040126.jar.

Becuse when i compile the program which uses poi-2.0-final-20040126.jar. It gives Error that System can't find symble.

Please tell me how to set class path for and then Under which drictory we have to keep poi-2.0-final-20040126.jar.

file.

Please help me out on this Issue

Thank you very Much

rowinraja at 2007-7-9 20:46:23 > top of Java-index,Desktop,Core GUI APIs...
# 3

Hey there

that all depends on which OS you are using.

If you type javac -help you will notice a list of options there regarding classpath.

I'm guessing you are using windows?

try this:

javac -classpath poi-2.0-final-20040126.jar mySourceFile.java

note for this to work the jar must be in the same directory as your source code.

when running your code you can do similar:

java -classpath poi-2.0-final-20040126.jar mySourceFile

Enjoy and well done getting this far.

Jason.

fireman.sparkeya at 2007-7-9 20:46:23 > top of Java-index,Desktop,Core GUI APIs...
# 4

Hi

Tryied to work with Exporting JTable details to Excel. I have downloaded the poi-2.5.1-final-20040804.jar file. and stored it in the Same directory where i have my source code And compiled.

Now i am getting some Errors Like

Can't find symbol

formateCell(cell,rendererComponent);

Here I am posting my code

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.table.*;

import java.sql.*;

import org.apache.poi.hssf.usermodel.*;

import java.io.*;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.hssf.usermodel.HSSFCell.*;

import org.apache.poi.hssf.usermodel.HSSFSheet.*;

public class Visitors extends JFrame implements ActionListener

{

JTable table;

JPanel buttonPanel;

JButton button,button1;

private Connection con;

private Statement st;

//Addvisitor av=null;

JDesktopPane desktop;

public Visitors()

{

Vector columnNames = new Vector();

Vector data = new Vector();

// Create table

// Add table and a Button panel to the frame

buttonPanel = new JPanel();

button = new JButton( "Add Visitor" );

button1 = new JButton("Export");

button.addActionListener(this);

button1.addActionListener(this);

JScrollPane scrollPane = new JScrollPane( table );

getContentPane().add( scrollPane );

table = new JTable();

JPanel pnl2 = new JPanel();

pnl2.add(scrollPane);

buttonPanel.add(table);

buttonPanel.add( button );

buttonPanel.add( button1 );

getContentPane().add(pnl2, BorderLayout.NORTH);

getContentPane().add( buttonPanel, BorderLayout.SOUTH );

desktop=new JDesktopPane();

getContentPane().add(desktop);

try

{

Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");

//Class.forName("net.sourceforge.jtds.jdbc.Driver");

String loc = "jdbc:odbc:Library";

//String loc="jdbc:jtds:sqlserver://192.168.1.20:1433/testDatabase";

con = DriverManager.getConnection (loc,"sa","madmax");

st=con.createStatement();

ResultSet rs=st.executeQuery("Select * from Books");

ResultSetMetaData md= rs.getMetaData();

int columns =md.getColumnCount();

String booktblheading[]={"VistorName","IN TIME","OUT TIME","REASON"};

for(int i=1; i<= booktblheading.length;i++)

{

columnNames.addElement(booktblheading[i-1]);

}

while(rs.next())

{

Vector row = new Vector(columns);

for(int i=1;i<=columns;i++)

{

row.addElement(rs.getObject(i));

}

data.addElement(row);

//System.out.println("data is:"+data);

}

((DefaultTableModel)table.getModel()).setDataVector(data,columnNames);

rs.close();

st.close();

}

catch (ClassNotFoundException cnf) {

JOptionPane.showMessageDialog (null, "Driver not Loaded...");

System.exit (0);

}

catch (SQLException sqlex) {

JOptionPane.showMessageDialog (null, "Unable to Connect to Database...");

System.exit (0);

}

}

public void actionPerformed (ActionEvent ae)

{

Object obj = ae.getSource();

if (obj == button)

{

//av = new Addvisitor(table.getModel());

//desktop.add(av);

}

else if(obj==button1)

{

}

}

public void exportTableToSheet(JTable table, HSSFSheet sheet) {

int rowCount = table.getRowCount();

int colCount = table.getColumnCount();

int currentSheetRow = 0;

for (int tableRowIndex = 0; tableRowIndex < rowCount; tableRowIndex++) {

for (int tableColIndex = 0; tableColIndex < colCount; tableColIndex++) {

// create and format the cell in the spreadsheet

createAndFormatCell(table, tableRowIndex, tableColIndex, sheet, currentSheetRow);

}

currentSheetRow++;

}

}

private void createAndFormatCell(JTable table, int tableRowIndex, int tableColIndex,

HSSFSheet sheet, int currentSheetRow) {

// get the cell value from the table

Object cellValue = table.getValueAt(tableRowIndex, tableColIndex);

// create the cell

HSSFCell cell = createHSSFCell(sheet, cellValue, currentSheetRow, tableColIndex);

// get the renderer component that renders the cell

TableCellRenderer renderer = table.getCellRenderer(tableRowIndex, tableColIndex);

Component rendererComponent = renderer.getTableCellRendererComponent(table,

cellValue,

false,

false,

tableRowIndex,

tableColIndex);

if (rendererComponent instanceof JLabel) {

// if it is a JLabel, get the label text which is the actual formatted displayed text

//and not the raw cell value

JLabel label = (JLabel) rendererComponent;

cellValue = label.getText();

}

formatCell(cell, rendererComponent);

}

public static void main(String[] args)

{

Visitors frame = new Visitors();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setSize(700,500);

frame.setVisible(true);

}

}

Please check my code and give me proper solution

Otherwise give me some source code , So that i can Understand

Thanking you

rowinraj

rowinraja at 2007-7-9 20:46:23 > top of Java-index,Desktop,Core GUI APIs...
# 5
Hi camikrPlease help me on this issue, I have posted my sample code on the Perviouse thread.Please provide me some sample codeThank you very MuchRowinraj
rowinraja at 2007-7-9 20:46:23 > top of Java-index,Desktop,Core GUI APIs...