Export Jtable in excel file

how to export jtable data to excel in java netbeans
to develop a program to export jtable data to excel in java
how to import data from excel to jtable in java
apache poi excel
excel jar file for java
java excel library
ole2 office documents
how to save java table in excel

hi i am newbie in java programming but here's my problem i am designing GUI which does some calculation and need to show the inputs and outputs in J table then export them into excel file

the code working like charm but when i open the excel file i find it empty ..

j Table

import java.awt.Desktop;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.swing.JFileChooser;

public class Export extends javax.swing.JFrame {

public Export() {

// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    jTable1 = new javax.swing.JTable();
    jButton1 = new javax.swing.JButton();


    jTable1.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {"Channel Model", global.channel_model, global.channel_model},                    
    {"System Bandwidth (MHz)", global.band_width_index,      global.band_width_index},
            {"Cell Edge Rate (Kbps)", global.Rreq_UL, global.Rreq_DL},
            {"Cell edge MCS", global.mcs, global.mcs},
    {"Antenna Configuration", global.DL_antenna_config, global.DL_antenna_config},
            {"Total RB Number", global.number_of_RB, global.number_of_RB},

            {"Tx", "", null},
            {"Max Power (dBm)", global.UE_Tx_power, global.UE_Tx_power},
            {"Cable Loss (dB)", global.cable_loss, global.cable_loss},
            {"Body Loss (dB)", global.body_loss, global.body_loss},
            {"Antenna Gain (dB)", global.UE_antennaGain, global.UE_antennaGain},
            {"EIRB (dB)", "", null},
            {"Rx", null, null},
            {"Antenna Gain (dB)", global.UE_antennaGain, global.UE_antennaGain},
            {"Cable Loss (dB)", global.cable_loss, global.cable_loss},
            {"Body Loss (dB)", global.body_loss, global.body_loss},
            {"Noise Figure (dB)", global.UE_noiseFigure, global.UE_noiseFigure},
            {"Thermal Noise (dB)", null, null},
            {"Interference Margin (dB)", global.Biul, global.Bidl},
            {"SINR (dB)", global.SINR, global.DL_SINR},
            {"Reciver Sensitivty (dB)", "", null},
            {"MAPL", null, null},
            {"Diversity (dB)", global.Tx_diversity_gain, global.Tx_diversity_gain},

            {"Shadow Fading Margin (dB)", global.shadow_margin, global.shadow_margin},
            {"MAPL (dB)", null, null},
            {"Cell Radius (Km)", global.R, global.R},
            {"Area of Dimensioning (Km squar", global.site_area, global.site_area},
            {"Site Type", global.type_of_site, global.type_of_site},
            {"Number of Sites  Due to Coverage", "", null}
        new String [] {
            "Summary", "Uplink", "Downlink"

    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            .addContainerGap(562, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 455, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 0, Short.MAX_VALUE))

    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     JFileChooser fileChooser = new JFileChooser();
    int retval = fileChooser.showSaveDialog(jButton1);

    if (retval == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();
        if (file != null) {
            if (!file.getName().toLowerCase().endsWith(".xls")) {
                file = new File(file.getParentFile(), file.getName() + ".xls");


            try {
                ExcelExporter exp=new ExcelExporter();
                exp.exportTable(jTable1, file);

            } catch (UnsupportedEncodingException e) {

            } catch (FileNotFoundException e) {
                System.out.println("not found");
            } catch (IOException e) {

   }catch(Exception e){


    public static void main(String args[]) {
            try {
        for (javax.swing.UIManager.LookAndFeelInfo info :     javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Export.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Export.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Export.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Export.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Export().setVisible(true);

 // Variables declaration - do not modify                     
 private javax.swing.JButton jButton1;
 private javax.swing.JScrollPane jScrollPane1;
 private javax.swing.JTable jTable1;
 // End of variables declaration                   

The excel generator class

import java.io.*;
import javax.swing.table.TableModel;
import javax.swing.*;

public class ExcelExporter {
    public void exportTable(JTable jTable1,File file) throws IOException{
      TableModel model=jTable1.getModel();
      FileWriter out=new FileWriter(file);
      BufferedWriter bw=new BufferedWriter(out);
      for (int i=0;i<model.getColumnCount();i++){
      for (int i=0;i<model.getRowCount();i++){
        for (int j=0;j<model.getColumnCount();j++){
   System.out.print("Write out to"+file);


For write and manipulating Excel file use Apache POI http://poi.apache.org/

If You want to create Excel file by Yourself without using external library, keep in mind that excel file data is stored in form of XML Spreadsheet, so You will have to create file like that

<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 <Worksheet ss:Name="Sheet1">
  <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1" x:FullRows="1">
    <Cell><Data ss:Type="String">Name</Data></Cell>
    <Cell><Data ss:Type="String">Example</Data></Cell>
    <Cell><Data ss:Type="String">Value</Data></Cell>
    <Cell><Data ss:Type="Number">123</Data></Cell>

source: http://en.wikipedia.org/wiki/Microsoft_Excel

Export Jtable in excel file, To export data from java JTable to excel you need POI library. To download POI(Apache POI Duration: 37:47 Posted: Jul 11, 2019 So now you're saying the export process is different from the loading of the table!? – MadProgrammer Apr 12 '17 at 10:22 @MadProgrammer..thank you for the codeproblem resolved – Vsal Apr 12 '17 at 10:22

As has been recommended Apache POI is a very good api for manipulating and creating Excel documents. It is free and open source so easily usable. If you try it out then here is one way you could create your file:

private static void writeToExcell(JTable table, Path path) throws FileNotFoundException, IOException {
    new WorkbookFactory();
    Workbook wb = new XSSFWorkbook(); //Excell workbook
    Sheet sheet = wb.createSheet(); //WorkSheet
    Row row = sheet.createRow(2); //Row created at line 3
    TableModel model = table.getModel(); //Table model

    Row headerRow = sheet.createRow(0); //Create row at line 0
    for(int headings = 0; headings < model.getColumnCount(); headings++){ //For each column
        headerRow.createCell(headings).setCellValue(model.getColumnName(headings));//Write column name

    for(int rows = 0; rows < model.getRowCount(); rows++){ //For each table row
        for(int cols = 0; cols < table.getColumnCount(); cols++){ //For each table column
            row.createCell(cols).setCellValue(model.getValueAt(rows, cols).toString()); //Write value

        //Set the row to the next one in the sequence 
        row = sheet.createRow((rows + 3)); 
    wb.write(new FileOutputStream(path.toString()));//Save the file     

The alternative would be to learn the Microsoft Excel file specifications and then manually create the Excel file that way. What you are doing at the moment is writing the data in your table to a file, but not in any way that the Excel application can understand it.

How To Export Jtable Data To Excel In Java Netbeans, ProgrammingWizards Java Excel -java excel jtable export, java excel api that can help us Duration: 24:30 Posted: Aug 20, 2015 Export From JTable To MS Excel - Teach Me Mr. Xymon. Open your Registry (Start -> Run -> regedit.exe) Open your Registry (Start -> Run -> regedit.exe) Navigate to HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\OFFICE\12.0\EXCEL\SECURITY. Right click in the right window and choose New -> DWORD. Type

You can solve this without using Apache POI. What worked for me was to remove the String conversion while writing in the excel exporter class.


See if this works. This is because the getValueAt() function returns an object which has a problem in conversion to String. Let me know.

Java Excel Ep.02 - Export JTable to Excel Programmatically, my former students on how to export data from JTable to MS Excel file. Excel uses a complicated formatting for its native .xls files but it also  I am filtering data from excel sheet to jTable n storing the result in a new excel sheet. Ive refered to both of your import from excel and export to excel codes. but i would like the data type to remain same in both jtable n new excel sheet. plss tell the modifications as i need to complete my project. thanx. Reply Delete

Export From JTable To MS Excel - Teach Me Mr. Xymon, xls” extension. The file can then be opened by Excel and converted to a spreadsheet. I liked their approach, but I also wanted to provide the  exportTable() function takes two parameter one is table and another one is the file name e g. exportTable(table, new File("D:\tabledata.xls")); public void exportTable(JTable table, File file) throws IOException {

Exporting JTable data to Excel, Java Retrieve Data into JTable - This java tutorial guide you will learn how to Retrieve Data from the database in the table and then export it to the Excel file. JAVA JTABLE EXPORT TO PDF - Hello friends, following is the function which helps to export tables data to the excel file. exportTable() function takes two parameter one is table. Get PDF Mobi

Retrieve Data into JTable and export it to Excel File, JTable. This post intended to show example for exporting data to excel from DefaultTableModel object. The library that can build an excel file  DefaultTableModel is one of swing JTable component. This DefaultTableModel class populate JTable data. In other words, JTable data (rows, columns, cells) is a DefaultTableModel instance / object. However, this post example will show how to populate DefaultTableModel’s column headers, rows, and cells value only.

  • i used Apache POI as u've suggested ... it worked thanks
  • "..not in any way that the Excel application can understand it." Excel sure does understand TSV (tab separated variables).
  • i used Apache POI as u've suggested ... it worked thanks