How to write the JTables Content into the CSV File.
Hi Friends
I managed to write the Database records into the CSV Files. Now i would like to add the JTables contend into the CSV Files.
I just add the Code which Used to write the Database records into the CSV Files.
void exportApi()throws Exception
{
try
{
PrintWriter writing=new PrintWriter(new FileWriter("Report.csv"));
System.out.println("Connected");
stexport=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
rsexport=stexport.executeQuery("Select * from IssuedBook ");
ResultSetMetaData md = rsexport.getMetaData();
int columns = md.getColumnCount();
String fieldNames[]={"No","Name","Author","Date","Id","Issued","Return"};
//write fields names
String rec ="";
for (int i=0; i < fieldNames.length; i++)
{
rec +='\"'+fieldNames[i]+'\"';
rec+=",";
}
if (rec.endsWith(",")) rec=rec.substring(0, (rec.length()-1));
writing.println(rec);
//write values from result set to file
rsexport.beforeFirst();
while(rsexport.next())
{
rec ="";
for (int i=1; i < (columns+1); i++)
{
try
{
rec +="\""+rsexport.getString(i)+"\",";
rec +="\""+rsexport.getInt(i)+"\",";
}
catch(SQLException sqle)
{
// I would add this System.out.println("Exception in retrieval in for loop:\n"+sqle);
}
}
if (rec.endsWith(",")) rec=rec.substring(0,(rec.length()-1));
writing.println(rec);
}
writing.close();
}
With this Same code how to Write the JTable content into the CSV Files.
Please tell me how to implement this.
Thank you for your Service
Jofin
[3301 byte] By [
jofin123a] at [2007-11-27 3:28:03]

# 1
for (int row = 0; row < yourJtable.getRowCount(); ++row){
for (int col = 0; col < yourJtable.getColumnCount(); ++col){
Object ob = yourJtable.getValueAt(row, col);
addCSV(ob);
}
}
hiwaa at 2007-7-12 8:30:51 >

# 2
Hi Friends
I just modified my code and tried according to your suggestion. But here it does not print the records inside CSV File. But when i use ResultSet it prints the Records inside the CSV. Now i want to Display only the JTable content.
I am posting my code here. Please run this code and find the Report.csv file in your current Directory. and please help me to come out of this Problem.
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
public class Exporting extends JDialog implements ActionListener
{
private JRadioButton rby,rbn,rbr,rbnore,rbnorest;
private ButtonGroup bg;
private JPanel exportpanel;
private JButton btnExpots;
FileReader reading=null;
FileWriter writing=null;
JTable table;
JScrollPane scroll;
public Exporting()throws Exception
{
setSize(550,450);
setTitle("Export Results");
this.setLocation(100,100);
String Heading[]={"BOOK ID","NAME","AUTHOR","PRICE"};
String records[][]={{"B0201","JAVA PROGRAMING","JAMES","1234.00"},
{"B0202","SERVLET PROGRAMING","GOSLIN","1425.00"},
{"B0203","PHP DEVELOPMENT","SUNITHA","123"},
{"B0204","PRIAM","SELVI","1354"},
{"B0205","JAVA PROGRAMING","JAMES","1234.00"},
{"B0206","SERVLET PROGRAMING","GOSLIN","1425.00"},
{"B0207","PHP DEVELOPMENT","SUNITHA","123"},
{"B0208","PRIAM","SELVI","1354"}};
btnExpots= new JButton("Export");
btnExpots.addActionListener(this);
btnExpots.setBounds(140,200,60,25);
table = new JTable();
scroll=new JScrollPane(table);
((DefaultTableModel)table.getModel()).setDataVector(records,Heading);
System.out.println(table.getModel());
exportpanel= new JPanel();
exportpanel.add(btnExpots,BorderLayout.SOUTH);
exportpanel.add(scroll);
getContentPane().add(exportpanel);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
Object obj=ae.getSource();
try {
PrintWriter writing= new PrintWriter(new FileWriter("Report.csv"));
if(obj==btnExpots)
{
for(int row=0;row<table.getRowCount();++row)
{
for(int col=0;col<table.getColumnCount();++col)
{
Object ob=table.getValueAt(row,col);
//exportApi(ob);
System.out.println(ob);
System.out.println("Connected");
String fieldNames[]={"BOOK ID","NAME","AUTHOR","PRICE"};
String rec = "";
for (int i=0; i ><fieldNames.length; i++)
{
rec +='\"'+fieldNames[i]+'\"';
rec+=",";
}
if (rec.endsWith(",")) rec=rec.substring(0, (rec.length()-1));
writing.println(rec);
//write values from result set to file
rec +="\""+ob+"\",";
if (rec.endsWith(",")) rec=rec.substring(0,(rec.length()-1));
writing.println(rec);
writing.close();
}
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String arg[]) throws Exception
{
Exporting ex= new Exporting();
}
}
Could anyone Please modify my code and help me out.
Thank you for your service
Cheers
Jofin>
# 3
Hi friendsCould anyone please help me on this issue. Please run my code and give me a better SolutionThank you for your serviceCheersJofin
# 4
> Object ob=table.getValueAt(row,col);This is all about gettint values from JTable.(You may want some formatting on some of them, though.)Rest is up tp you.What is your problem?
hiwaa at 2007-7-12 8:30:51 >

# 5
Hi friend
When i use this same Program to wirte the Database records it wirtes exactly into the CSV File.
In My program i used this code to get the JTable's content
for (int row = 0; row < yourJtable.getRowCount(); ++row){
for (int col = 0; col < yourJtable.getColumnCount(); ++col){
Object ob = yourJtable.getValueAt(row, col);
}
}
After that i just passed the object to the Following code to write them into the CSV File
String rec = "";
for (int i=0; i ><fieldNames.length; i++)
{
rec +='\"'+fieldNames[i]+'\"';
rec+=",";
}
if (rec.endsWith(",")) rec=rec.substring(0, (rec.length()-1));
writing.println(rec);
//write values from result set to file
rec +="\""+ob+"\",";
if (rec.endsWith(",")) rec=rec.substring(0,(rec.length()-1));
writing.println(rec);
writing.close();
in the above code where ever you find ob, there i Placed the Resultset.getString().
It wrote the records which brought by the SQL.
now i am not able to write the JTable's Contend. the Previouse post of mine is a Working code. Could you please run that and give me good solution
Thank you for your Help
Cheers
Jofin>
# 6
I don't see any reason why your code is not working.Assiming that you are calling you write function inside of the loop, similar to System.out.print() as shown below
for(int i = 0; i < table.getRowCount(); i++)
{
for(int j = 0; j < table.getColumnCount(); j++)
System.out.print(table.getValueAt(i, j)+",");
System.out.println("\n");
}
# 7
Hi Friend
see here on this particular Block i have called the PrintWriters instance inside the for Loop.
for(int row=0;row<table.getRowCount();++row)
{
for(int col=0;col<table.getColumnCount();++col)
{
Object ob=table.getValueAt(row,col);
//exportApi(ob);
System.out.println(ob);
System.out.println("Connected");
String fieldNames[]={"BOOK ID","NAME","AUTHOR","PRICE"};
String rec = "";
for (int i=0; i ><fieldNames.length; i++)
{
rec +='\"'+fieldNames[i]+'\"';
rec+=",";
}
if (rec.endsWith(",")) rec=rec.substring(0, (rec.length()-1));
writing.println(rec);
//write values from result set to file
rec +="\""+ob+"\",";
if (rec.endsWith(",")) rec=rec.substring(0,(rec.length()-1));
writing.println(rec);
writing.close();
}
}
You can run My code and see. But i am not able to find out why it is not printing the Content inside the CSV File.
Thank you for your help
Cheers
Jofin>
# 8
the problem is with this line, you are closing the stream after the first loop.writing.close();
# 9
Hi friends
I just managed to print the JTable's Content inside the CSV File.
But now the Problem is it is now problem is It does not Print the JTable's Content in row and Column wise. All the Records are printed in one Column itself.
This is My code Please run this code and give me a good solution to print exactly as it is in the JTable
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
public class Exporting extends JDialog implements ActionListener
{
private JRadioButton rby,rbn,rbr,rbnore,rbnorest;
private ButtonGroup bg;
private JPanel exportpanel;
private JButton btnExpots;
FileReader reading=null;
FileWriter writing=null;
JTable table;
JScrollPane scroll;
public Exporting()throws Exception
{
setSize(550,450);
setTitle("Export Results");
this.setLocation(100,100);
String Heading[]={"BOOK ID","NAME","AUTHOR","PRICE"};
String records[][]={{"B0201","JAVA PROGRAMING","JAMES","1234.00"},
{"B0202","SERVLET PROGRAMING","GOSLIN","1425.00"},
{"B0203","PHP DEVELOPMENT","SUNITHA","123"},
{"B0204","PRIAM","SELVI","1354"},
{"B0205","JAVA PROGRAMING","JAMES","1234.00"},
{"B0206","SERVLET PROGRAMING","GOSLIN","1425.00"},
{"B0207","PHP DEVELOPMENT","SUNITHA","123"},
{"B0208","PRIAM","SELVI","1354"}};
btnExpots= new JButton("Export");
btnExpots.addActionListener(this);
btnExpots.setBounds(140,200,60,25);
table = new JTable();
scroll=new JScrollPane(table);
((DefaultTableModel)table.getModel()).setDataVector(records,Heading);
System.out.println(table.getModel());
exportpanel= new JPanel();
exportpanel.add(btnExpots,BorderLayout.SOUTH);
exportpanel.add(scroll);
getContentPane().add(exportpanel);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
Object obj=ae.getSource();
Object ob=null;
try {
PrintWriter writing= new PrintWriter(new FileWriter("Report.csv"));
if(obj==btnExpots)
{
for(int row=0;row<table.getRowCount();row++)
{
for(int col=0;col<table.getColumnCount();col++)
{
ob=table.getValueAt(row,col);
String rec = "";
//write values from result set to file
rec +="\""+ob+"\",";
System.out.println(rec);
if (rec.endsWith(","))
rec=rec.substring(0,(rec.length()-1));
System.out.println(rec);
writing.format(rec);
}
}
writing.close();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String arg[]) throws Exception
{
Exporting ex= new Exporting();
}
}
Please help me on this Issue. I have been Lots of time to do this But i am not getting the Answer.
Thank you for your great help
Cheers
Jofin>
# 10
Hi FriendsCould anyone please run my code and see what would be the problem and give me a better solution?Please help me on this IssueThank you for your HelpCheersJofin
# 11
Your PrintWriter 'writing' doesn't do any output action in your current code.Besides, doing I/O in the GUI event handler is a very bad idea. Do it in a separate thread.
hiwaa at 2007-7-12 8:30:51 >

# 12
Hi Friends
how you are telling, are you telling me to write this I/O as separate Method and call it is side the Event Handeler. Ok i will implement your idea.
I just run My program it creates a file Called Report.csv inside the Current directory where you are working on.
It prints the JTable content also. But not exactly as the Content is in the JTable.
Please help me to come out of this Problem
Thank you very much
Cheers
Jofin
# 13
> separate MethodSeparate thread. See http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html> not exactly as the Content is in the JTableIt is just your string composition issue. Check that in every detail.
hiwaa at 2007-7-12 8:30:51 >
