Exporting Java DefaultTableModel To Excel Example

Description

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. So this example will not showing JTable presentation. FYI, building JTable is pretty easy.
Also DefaultTableModel can be used for storing any kind of tabular data for application purposes without need to presentate its data to JTable.
This post intended to show example for exporting data to excel from DefaultTableModel object.

The library that can build an excel file is ‘Apache POI’. You need to download it through this link http://poi.apache.org/download.html.
The title of this component is ‘the Java API for Microsoft Documents’. I think this title is quite self explanatory. In short, you can write & read MS Documents, including Excel using this component.

The Example

import java.io.FileNotFoundException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class ExportToExcel {

   
public static void main(String[] args)  {
try {

//Populate DefaultTableModel data
DefaultTableModel dtm = new DefaultTableModel();
	Vector<String> cols = new Vector<String>();
	dtm.addColumn("Col 1");
	dtm.addColumn("Col 2");
	dtm.addColumn("Col 3");
	
	Vector<String> dtmrow = null;
	for (int i=1;i<=10;i++) {
		dtmrow = new Vector<String>();
		for (int j=1;j<=3;j++) {
			dtmrow.add("Cell " + j + "." + i );
		}
		dtm.addRow(dtmrow);
	}
	
//Exporting to Excel		   
	
	Workbook wb = new HSSFWorkbook();
	CreationHelper createhelper = wb.getCreationHelper();
	Sheet sheet = wb.createSheet("new sheet");
	Row row = null;
	Cell cell = null;
	for (int i=0;i<dtm.getRowCount();i++) {
		row = sheet.createRow(i);
		for (int j=0;j<dtm.getColumnCount();j++) {
			
			cell = row.createCell(j);
			cell.setCellValue((String) dtm.getValueAt(i, j));
		}
	}
	
   
	FileOutputStream out = new FileOutputStream("D:\\workbook.xls");
	wb.write(out);
	out.close();
} catch (FileNotFoundException ex) {
	Logger.getLogger(ExportToExcel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
	Logger.getLogger(ExportToExcel.class.getName()).log(Level.SEVERE, null, ex);
}
	
   }
    
}	

DefaultTableModel column headers can be set using addColumn and its rows using addRow procedure. The row itself is a simple Vector type.
This usually populated by columns and rows from database records.

Exporting to Excel using HSSFWorkbook class is pretty easy as you can see in example above. You need to create workbook, sheet, row & column objects.
After that use FileOutputStream to create a physical file.

The created Excel file will look like below pic:

There is a quick guide for working with Excel. Please follow this link http://poi.apache.org/spreadsheet/quick-guide.html

Regards,
Agung Gugiaji

One response to “Exporting Java DefaultTableModel To Excel Example

  1. sir,showing error in this line showing incompatible types: org.apache.poi.ss.usermodel.Row cannot be converted to com.lowagie.text.Row
    row = sheet.createRow(i);
    for (int j=0;j<dtm.getColumnCount();j++) {

    cell = row.createCell(j);
    cell.setCellValue((String) dtm.getValueAt(i, j));

Leave a reply to raushan Cancel reply