How to save data to excel with a 2.1 work sheet format without changing it?

How to save data to excel with a 2.1 work sheet format without changing it?

hi , i have done a program that allows user to inout data through jtable and save it to excel file then read it again .. the problem is , when data is saved , the extension file is changed to excel file with text ( tab limited ) type , if i read it as it is it throughs an exceeption so i should save that file as ... excel 2.1 work sheet at first then read it back ..

Whats the solution ? i tried to make the default type 2.1 work sheet from excel tools but didnt work

View Answers

January 3, 2012 at 11:31 AM

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
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;

public class JTableToExcel{
JTable table;
JButton button;
public static void main(String[] args) {
new JTableToExcel();
}
public JTableToExcel(){
JFrame frame = new JFrame("Getting Cell Values in JTable");
JPanel panel = new JPanel();
String data[][] = {{"Angelina","Mumbai"},{"Martina","Delhi"}};

String col[] = {"Name","Address"};
DefaultTableModel model = new DefaultTableModel(data, col);
table = new JTable(model);
JScrollPane pane = new JScrollPane(table);
button=new JButton("Submit");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Excel Sheet");
int index=0;
int count=table.getRowCount();

try{
for(int i=0;i<count;i++){
Object obj1 = GetData(table, i, 0);
Object obj2 = GetData(table, i, 1);
String value1=obj1.toString();
String value2=obj2.toString();
HSSFRow row = sheet.createRow((short)index);
row.createCell((short) 0).setCellValue(value1);
row.createCell((short) 1).setCellValue(value2);
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 e){}
}
});
panel.add(pane);
panel.add(button);
frame.add(panel);
frame.setSize(650,350);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public Object GetData(JTable table, int row_index, int col_index){
return table.getModel().getValueAt(row_index, col_index);
}
}









Related Tutorials/Questions & Answers:
How to save data to excel with a 2.1 work sheet format without changing it?
how to reverse a string without changing its place
Advertisements
To save table format data in pdf/excel in jsp
how to display data in excel sheet?
how to import excel sheet into mysql database and save it as a table in database
How to save excel sheet into mysql database using blob or clob
How to get data from Excel sheet - Struts
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 file to excel sheet by using java
How to Get The Data from Excel sheet into out jsp page???
read data from Excel sheet
How to export data from html file to excel sheet by using java
jsp with excel sheet data uploading into database
save data in excel
save data in excel
extract data from excel sheet to mysql
Built in Data Format in Excel Using POI 3.0
Format Cell of Simple Excel Sheet(.xls)
How to read every cell of an excel sheet using Apache POI and insert those data into a DB?
How to export data from database to excel sheet by using java in standalone project
Hi how to transfer table data from html page to excel sheet by using javascript .
exporting data to excel sheet - JDBC
Export data in excel sheet in java in struts - Struts
export data to excel sheet - JSP-Servlet
Export data to Excel Sheet in STRUTS 1.3
Printing data into Excel sheet - Java Beginners
Uploading Excel sheet record in JSP to insert data in MySql
excel sheet reading and using that data - JSP-Servlet
Store data in HTML forms from Excel sheet
How to export data from database to excel sheet by using java swing in standalone project - Java Beginners
export data from database to excel sheet - JDBC
Export data in excel sheet via Browse and upload button into mysql database
Convert the excel sheet data into oracle table through java or jsp
How to download web page table data, export the table records in an excel file and save
How to set background of an excel sheet using POI - Java Magazine
Set Data Format in Excel Using POI 3.0
Java swing: Export excel sheet data to JTable
insert excel sheet into mysql as a table
apa itu data science
itu data science
How to Create New Excel Sheet Using JSP
Insert Image into Excel Sheet
Need to Remove Duplicate Records from Excel Sheet
changing of data...
changing of data...
How to open excel sheet when excel file is placed in some folder in desktop
update excel sheet using jsp::
how to read values from excel sheet and compare with database using jsp
Excel sheet in read only mode - WebSevices

Ads