How to export data from database to excel sheet by using java swing in standalone project

How to export data from database to excel sheet by using java swing in standalone project

Sir i have a problem in developing.

Sir i need help from u to solve my problem.
my problem is i don know how to export data from database to excel sheet by using java swing in standalone project.I get solution from u about this topic but u are sending answer related to apache server.But i need solution by using java swing programming.
please send me reply to me
Thank you sir



View Answers

March 19, 2010 at 11:31 AM

Hi Friend,

Actually that code is not related to apache server. The classes we have used belongs to Jakarta api. You need this api in order to create an excel sheet.
We are providing you another code, that will take the database vales from JTable to Excel sheet.

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.table.*;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;

class Form extends JFrame{
ResultSet rs;
Form(){
String data[][] = {{"",""}};
String col[] = {"Name","Address"};
final DefaultTableModel model = new DefaultTableModel(data,col);
final JTable table = new JTable(model);
JScrollPane pane=new JScrollPane(table);
JButton button=new JButton("Export");
JPanel panel=new JPanel();
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
final HSSFWorkbook wb = new HSSFWorkbook();
final HSSFSheet sheet = wb.createSheet("Excel Sheet");
try{
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test";, "root", "root");
Statement st = con.createStatement();
rs= st.executeQuery("Select * from data");
while(rs.next()){
model.insertRow(table.getRowCount(),new Object[]{rs.getString("name"),rs.getString("address")});
}
model.removeRow(0);
panel1.add(pane);
panel2.add(button);
panel.add(panel1);
panel.add(panel2);
add(panel);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev){
try{
HSSFRow rowhead = sheet.createRow((short)0);
rowhead.createCell((short) 0).setCellValue("Name");
rowhead.createCell((short) 1).setCellValue("Address");
int index=1;
int count=table.getRowCount();
for(int i=0;i<count;i++){
Object obj1 = GetData(table, i, 0);
Object obj2 = GetData(table, i, 1);

HSSFRow row = sheet.createRow((short)index);
row.createCell((short) 0).setCellValue(obj1.toString());
row.createCell((short) 1).setCellValue(obj2.toString());
index++;
}
FileOutputStream fileOut = new FileOutputStream("c:\\Hello.xls");
wb.write(fileOut);
fileOut.close();
Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /C start C:\\Hello.xls");
}
catch(Exception ex){}
}
});
}
catch(Exception e){}
}
public Object GetData(JTable table, int row_index, int col_index){
return table.getModel().getValueAt(row_index, col_index);
}
}
class JTableToExcel{
public static void main(String arg[]) {
try
{
Form frame=new Form();
frame.setSize(450,200);
frame.setVisible(true);
}
catch(Exception e)
{}
}
}

Thanks

June 15, 2013 at 3:34 PM

Dear Friend, From your code, I am able to complete my project of Exporting the JTable to Excel and also Importing the same Excel to JTable that is two way movement of data which was not happening initially.


September 1, 2012 at 7:03 AM

Dear Sir, Your Code is Excellent,But in case of exporting data from Oracle DataBase to Excel Seet Some Packages are import like import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFCell;

How to import these Packages in Simple java Program and JDevloper IDE.

Thanks For your Help and Support. Amit Kumar









Related Tutorials/Questions & Answers:
How to export data from database to excel sheet by using java swing in standalone project - Java Beginners
How to export data from database to excel sheet by using java swing... from u to solve my problem. my problem is i don know how to export data from database to excel sheet by using java swing in standalone project.I get solution from
How to export data from database to excel sheet by using java in standalone project
How to export data from database to excel sheet by using java in standalone project  How to export data from database to excel sheet by using java in standalone project
Advertisements
How to export data from jsp to excel sheet by using java
How to export data from jsp to excel sheet by using java   How to export data from jsp to excel sheet by using java
How to export data from html to excel sheet by using java
How to export data from html to excel sheet by using java   How to export data from html to excel sheet by using java
How to export data from html file to excel sheet by using java
How to export data from html file to excel sheet by using java    How to export data from html file to excel sheet by using java
How to export data from html file to excel sheet by using java
How to export data from html file to excel sheet by using java   reading the data from Html file
export data from database to excel sheet - JDBC
export data from database to excel sheet  I am facing a problem about exporting required data from database table to ms-excel sheet.i mean whenever I execute a query then result will be display in MS-EXCEL sheet. please give me
Java swing: Export excel sheet data to JTable
Java swing: Export excel sheet data to JTable In this tutorial, you will learn how to read data from excel file and export it to JTable. In swing... and POI-HSSF api. Here we are using JExcel api to fetch the data from excel sheet
Export data in excel sheet via Browse and upload button into mysql database
Export data in excel sheet via Browse and upload button into mysql database  how to create a Browse & Upload Buttons and then save the information from it in mysql database's Table Am using struts2,hibernate for making
how to read values from excel sheet and compare with database using jsp
how to read values from excel sheet and compare with database using jsp  hi sir i am arun how to read values from excel sheet and compare with database using jsp coding i.e, if i have 6(assetid,assetname,serialno,cubical
Export data in excel sheet in java in struts - Struts
Export data in excel sheet in java in struts  Hello, All how can i export data in excel file i java  Hi friend, For solving the problem visit to : http://www.roseindia.net/jsp/excelfile.shtml Thanks
How to Retrieve Data from the database and write into excel file using Java
How to Retrieve Data from the database and write into excel file using Java  Hi, I am trying to develop an small application where i trying to retrieve Data from the database and store the details in excel file. Please can
How to get data from Excel sheet - Struts
How to get data from Excel sheet  Hi, I have an excel sheet with some data(including characters and numbers). Now i want read the data from excel sheet and display in console first then later insert this data into database
Read code from excel sheet and upload into database using JSP
Read code from excel sheet and upload into database using JSP  I want to upload data to database from an excel worksheet using jsp ...Please help
How to export the table content from an webpage to excel using java?
How to export the table content from an webpage to excel using java?  How to export the table content from an webpage to excel using java? The table contents are generated dynamically in that java page
How to export the table content from an webpage to excel using java?
How to export the table content from an webpage to excel using java?  How to export the table content from an webpage to excel using java? The table contents are generated dynamically in that java page
Hi how to transfer table data from html page to excel sheet by using javascript .
Hi how to transfer table data from html page to excel sheet by using javascript .  html page to excel sheet by using javascript and i dont want to transfer all rows in table, i want to hide some rows in excel sheet. Please send
read data from Excel sheet
read data from Excel sheet  Hi, How to read data from an excel sheet using JSP. Thank you
export data to excel sheet - JSP-Servlet
export data to excel sheet  Hi.. how to export data to excel sheet from jsp? and how to update the excel sheet from jsp? and how to get the data from excel sheet? and how to make calculations in excel sheet like total avg
jsp with excel sheet data uploading into database
jsp with excel sheet data uploading into database  how to upload data in excel sheet with jsp into oracle 10g database
Export data to Excel Sheet in STRUTS 1.3
Export data to Excel Sheet in STRUTS 1.3  hi. how can i export content present on the jsp to excel sheet on button click..... and challenging part is i have to merge few rows and columns for a particular field(Value
How to save excel sheet into mysql database using blob or clob
How to save excel sheet into mysql database using blob or clob  Hi All, I am new to java and i need to upload excel sheet to mysql, please suggest me the steps to do this, i am able to connect to the database, from there i don't
How to Get The Data from Excel sheet into out jsp page???
How to Get The Data from Excel sheet into out jsp page???  How to Get The Data from excel sheet to out jsp page in webApp
how to display data in excel sheet?
how to display data in excel sheet?  According to the user Id,some links will be generated from the database,and if we click a link,data would be retrieved from the database and data will be displayed in a new page.in this page
extract data from excel sheet to mysql
extract data from excel sheet to mysql  sir, i want to extract data from excel sheet and save the data in mysql5.0 database in the form of table
Read data from excel file and update database using jsp
Read data from excel file and update database using jsp  read data from excel file and update database using jsp Hi, I am using a MySQL database... upload excel file and update database using JSP ? Thanks in Advance
how to read data from excel file through browse and insert into oracle database using jsp or oracle???
how to read data from excel file through browse and insert into oracle database using jsp or oracle???  sir.. i have number of excel sheets which... a browse button which can upload a excelfile and after uploading the data should
How to export web page to excel using java or jsp or servlets
How to export web page to excel using java or jsp or servlets  Hi I am trying to export web page(jsp page ) to excel using jsp or servlets. I am... errors. Please can anyone tell me how to do this using java or jsp or servlets
How to read every cell of an excel sheet using Apache POI and insert those data into a DB?
How to read every cell of an excel sheet using Apache POI and insert those data into a DB?   i have an excel sheet, whose data in each cell has... the rows of an excel sheet. Need the source code
How to set background of an excel sheet using POI - Java Magazine
How to set background of an excel sheet using POI  Hi, i am trying to format the excel using java. How to set the background color of an excel sheet( complete sheet which includes all cells). i am using poi2.5.1. Thanks
excel sheet reading and using that data - JSP-Servlet
excel sheet reading and using that data  i have to do a read a excel sheet file of a employee record and then i have to use a employee details to send mail to those employees how to do in jsp sir please help me sir.. Thanks
Read data from Excel and insert in to DB and export data from DB to Excel
Read data from Excel and insert in to DB and export data from DB to Excel  Read data from Excel and insert in to DB and export data from DB to Excel Hi, I need to read the data from excel and I need to insert the same in to DB
insert rows from Excel sheet into a database by browsing the file
insert rows from Excel sheet into a database by browsing the file  hello, I am trying to insert rows from Excel sheet into SQL database by browsing Excel file in java(JSP). I can insert rows using ODBC connetion. But using odbc
how to import excel sheet into mysql database and save it as a table in database
how to import excel sheet into mysql database and save it as a table in database  sir, i want to import an excel sheet into a MySQL database and after importing it i want to store the excel sheet as a table in database
Store data in HTML forms from Excel sheet
Store data in HTML forms from Excel sheet  I have a excel file having multiple coloums, and having one Html page having forms corrospoding to excel coloums. I want to auto fill up the excel data into html forms, Any one please
connection of java file to excel file and put data from excel file into oracle db
connection of java file to excel file and put data from excel file into oracle db  how to create button on excel sheet using java swing and how we give action to that button excel sheet? how we connect excel file to java file
retrieve data from database in java swing form using prev n next buttons
retrieve data from database in java swing form using prev n next buttons  i have a database having columns id(int),path(text),width(int),height(int... of various rows from database in labels by clicking previous and next
retrieve data from database in java swing form using prev n next buttons
retrieve data from database in java swing form using prev n next buttons  i have a database having columns id(int),path(text),width(int),height(int... of various rows from database in labels by clicking previous and next
how read excel data into database using struts2 with hibernate
how read excel data into database using struts2 with hibernate   hi friends, i am venkat i am started learning struts 2, please help me how to read excel data into database using struts2 with hibernate, please show me
Printing data into Excel sheet - Java Beginners
a Java Bean that will retrieve the data from the database and insert it into excel... database then showing it(data) to in "Excel Sheet" is it possible to do ? if yes... where there is need to bring the data into "Excel Sheet" with proper format.My
Export html data to excel
Export html data to excel  I am trying to export data from an html page to an excel sheet.Any advise
Retrieve Data into JTable and export it to Excel File
Java Retrieve Data into JTable and export it to Excel File In this section, you will learn how to retrieve the data from the database... it into Vector which is then added to table. To export the table data to excel file, we
How to insert rows from Excel spreadsheet into database by browsing the excel file?
How to insert rows from Excel spreadsheet into database by browsing the excel file?  I want to insert rows from excel sheet to database.for this i... the excel file using file browsing dialogue through form in JSP. How can i select
update excel sheet using jsp::
update excel sheet using jsp::   Hi Sir,... I have a excel sheet for employee details "i retrieve particular datas from given excel sheet and display it into another excel sheet using jsp
How to Create New Excel Sheet Using JSP
How to create new excel sheet using jsp  ... a new  excel sheet using java .You can create any number of  new excel sheets in a excel file.  To create a excel sheet we can use third party APIs
Retrieve Data from the database and write into excel file
Retrieve Data from the database and write into excel file. In this section, we are going to retrieve data from the database and write into the excel file..., we have inserted the database table values in the excel sheet. Here
Insert excel file data into database Using Java Programming
excel file data into Database Using java programming: import java.io....Insert excel file data into database in Java Program In this PHP tutorial section, you will learn how to insert excel file data into the database. We have
Problem in executing macro enabled excel sheet using java
Problem in executing macro enabled excel sheet using java  Hi, I have written a java code which will read a excel sheet with macro enabled. then it will write data into sheet1 and sheet2 and it will execute a VB script
exporting data to excel sheet - JDBC
query in java swing program,i want to print the result about the query in excel...exporting data to excel sheet  Sir i have already send request about... = wb.createSheet("Excel Sheet"); try{ Connection con = null
Insert data in Excel File from Database using JSP
Insert data in Excel File from Database  using JSP ... will retrieve the data from database, create an excel file and data insert... developed a application to insert data  in excel file from database in JSP. We

Ads