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



Share on Google+Share on Google+

rajeyah jordan
How to save data to excel with a 2.1 work sheet format without changing it?
1 Answer(s)      5 years and 4 months ago
Posted in : Java Beginners

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.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();

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);
FileOutputStream fileOut = new FileOutputStream("c:\\Hello.xls");
Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /C start C:\\Hello.xls");
catch(Exception e){}

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


Related Tutorials/Questions & Answers:
Java Spring Hibernate Struts Training ClassNotFoundException HttpRequestInterceptor java.lang.noclassdeffounderror: org/apache/http/httprequest noclassdeffounderror: org/apache/http/client/methods/httpurirequest java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient How do I resolve this Java Class not found exception? httpclient java.lang.NoClassDefFoundError Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java


Advertisement null