Exporting Jtable into the Excelsheet using HSSFWorkbook

Kishan Bheemajiyani picture Kishan Bheemajiyani · Feb 21, 2014 · Viewed 10.6k times · Source

Hello I am trying to Export Jtable Data into the Excel Sheet using HSSFWorkbook. and i am getting all the content what Table have but i am not getting Table Headers please can anyone help for the same.

Here the Command used for Taking content of the Jtable.

        try {
                    HSSFWorkbook fWorkbook = new HSSFWorkbook();
                    HSSFSheet fSheet = fWorkbook.createSheet("new Sheet");
                    HSSFFont sheetTitleFont = fWorkbook.createFont();
                    File file = new File("/home/kishan/NetBeansProjects/JavaChecking/src/com/verve/SwingChecking/book.xls");
                    HSSFCellStyle cellStyle = fWorkbook.createCellStyle();

                    sheetTitleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                    //sheetTitleFont.setColor();
                    TableModel model = jTable1.getModel();


                    for (int i = 0; i < model.getRowCount(); i++) {
                        HSSFRow fRow = fSheet.createRow((short) i);
                        for (int j = 0; j < model.getColumnCount(); j++) {
                            HSSFCell cell = fRow.createCell((short) j);
                            cell.setCellValue(model.getValueAt(i, j).toString());
                            cell.setCellStyle(cellStyle);

                        }

                    }
    FileOutputStream fileOutputStream;
                fileOutputStream = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
                fWorkbook.write(bos);
                bos.close();
                fileOutputStream.close();
    }catch(Exception e){

        }



for (int i = 0; i < model.getColumnCount(); i++) {
                HSSFRow fRow = fSheet.createRow((short) i);
                for(int j = 0; j < model.getColumnCount(); j++) {
                    HSSFCell cell = fRow.createCell((short) j);
                    cell.setCellValue(model.getValueAt(i, j).toString());

                    System.out.println(model.getColumnName(j));
                }
            }

last for loop is not addind data of table header.enter image description here

and i am getting this excel file

enter image description here how to get Table header along with that??

Answer

aby4reality picture aby4reality · Nov 10, 2016

Here's my implementation of the HSSF Workbook from the answers in this thread.

I created a class ExcelWriter then a Method Writer which takes two parameters; the JTable and the FileLocation to be used.

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author oluwajayi
 */
public class ExcelWriter {

    public static void Writer (JTable jTable1, String Location) throws FileNotFoundException, IOException {

                HSSFWorkbook fWorkbook = new HSSFWorkbook();
                HSSFSheet fSheet = fWorkbook.createSheet("new Sheet");
                HSSFFont sheetTitleFont = fWorkbook.createFont();
                HSSFCellStyle cellStyle = fWorkbook.createCellStyle();
                sheetTitleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                //sheetTitleFont.setColor();
                TableModel model = jTable1.getModel();

                //Get Header
                TableColumnModel tcm = jTable1.getColumnModel();
                HSSFRow hRow = fSheet.createRow((short) 0);
                for(int j = 0; j < tcm.getColumnCount(); j++) {                       
                   HSSFCell cell = hRow.createCell((short) j);
            cell.setCellValue(tcm.getColumn(j).getHeaderValue().toString());
                   cell.setCellStyle(cellStyle);
                }

                //Get Other details
                for (int i = 0; i < model.getRowCount(); i++) {
                    HSSFRow fRow = fSheet.createRow((short) i+1);
                    for (int j = 0; j < model.getColumnCount(); j++) {
                        HSSFCell cell = fRow.createCell((short) j);
                        cell.setCellValue(model.getValueAt(i, j).toString());
                        cell.setCellStyle(cellStyle);
                    }
                }
            FileOutputStream fileOutputStream;
            fileOutputStream = new FileOutputStream(Location);
    try (BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream)) {
        fWorkbook.write(bos);
    }
            fileOutputStream.close();
}
}