Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

Code how to?

Ads

 
 

Share on Google+Share on Google+

rasheed
Code how to?
2 Answer(s)      8 years ago
Posted in : Java Beginners
Ads
View Answers

April 14, 2009 at 1:27 PM


Hi Friend,

We are providing you a code that will display a form on clicking the 'Form' button. After filling the fields, when you click the button 'Submit', the entered values get inserted into excel file.
Now to open the excel file, click the button 'Open Excel file'.

step1) Create CreateForm.java

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
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 CreateForm extends JFrame{
CreateForm()
{
JPanel panel;
JLabel label1,label2,label3,label4,label5;
final JTextField text1,text2,text3,text4,text5;
JButton button=new JButton("Submit");
label1 = new JLabel();
label1.setText("UserID:");
text1 = new JTextField(20);

label2 = new JLabel();
label2.setText("First Name:");
text2 = new JTextField(20);

label3 = new JLabel();
label3.setText("Last Name:");
text3 = new JTextField(20);

label4 = new JLabel();
label4.setText("Address:");
text4 = new JTextField(20);

label5 = new JLabel();
label5.setText("Email:");
text5 = new JTextField(20);

panel=new JPanel(new GridLayout(6,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(label4);
panel.add(text4);
panel.add(label5);
panel.add(text5);
panel.add(button);
add(panel,BorderLayout.CENTER);

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
String value3=text3.getText();
String value4=text4.getText();
String value5=text5.getText();
try{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(" sheet");
HSSFRow row = sheet.createRow((short)0);
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1);
row.createCell((short)0).setCellValue(value1);
row.createCell((short)1).setCellValue(value2);
row.createCell((short)2).setCellValue(value3);
row.createCell((short)3).setCellValue(value4);
row.createCell((short)4).setCellValue(value5);
FileOutputStream fileOut = new FileOutputStream("c:\\File.xls");
wb.write(fileOut);
fileOut.close();
}catch ( Exception ex ){
}
}
});
setTitle("FORM");
setVisible(true);
setSize(300,300);
}
}


April 14, 2009 at 1:29 PM


cont........

step 2) Create AccessFormDemo.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class Form extends JFrame
{
JButton ADD,SAVE;
JPanel panel;

Form(){
ADD=new JButton("Form");
SAVE=new JButton("Open Excel file");

panel=new JPanel(new GridLayout(2,2));
panel.add(ADD);
panel.add(SAVE);
add(panel,BorderLayout.CENTER);
setTitle("FORM");

SAVE.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
try {
Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /C start C:\\File.xls");
}
catch(Exception ex){
ex.printStackTrace();
}
}
});
ADD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
CreateForm tab=new CreateForm();
tab.setVisible(true);
}
});
}
}
class AccessFormDemo
{
public static void main(String arg[])
{
try
{
Form frame=new Form();
frame.setSize(300,100);
frame.setVisible(true);
}
catch(Exception e)
{}
}
}

Thanks

Ads









Related Tutorials/Questions & Answers:
Advertisements