Java JTable Model toCsv(TableModel model)

Here you can find the source of toCsv(TableModel model)

Description

Convert the given table model to comma separated string

License

Open Source License

Parameter

Parameter Description
model The table model to write

Return

CSV representation of the given table model

Declaration

public static String toCsv(TableModel model) 

Method Source Code

//package com.java2s;
/*//  ww w .  jav  a 2s .co  m
 * Copyright 1997-2016 Unidata Program Center/University Corporation for
 * Atmospheric Research, P.O. Box 3000, Boulder, CO 80307,
 * support@unidata.ucar.edu.
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import javax.swing.table.TableModel;

public class Main {
    /**
     * Convert the given table model to comma separated string
     *
     * @param model The table model to write
     *
     * @return CSV representation of the given table model
     */
    public static String toCsv(TableModel model) {
        return toCsv(model, false);
    }

    /**
     * Convert the given table model to comma separated string
     *
     * @param model The table model to write
     * @param includeColumnNames  true to include the column names
     *
     * @return CSV representation of the given table model
     */
    public static String toCsv(TableModel model, boolean includeColumnNames) {
        StringBuffer sb = new StringBuffer();
        int rows = model.getRowCount();
        int cols = model.getColumnCount();
        if (includeColumnNames) {
            for (int col = 0; col < cols; col++) {
                if (col > 0) {
                    sb.append(",");
                }
                sb.append(model.getColumnName(col));
            }
            sb.append("\n");
        }
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                if (col > 0) {
                    sb.append(",");
                }
                sb.append(model.getValueAt(row, col));
            }
            sb.append("\n");
        }
        return sb.toString();
    }
}

Related

  1. modelIndexToDisplayIndex(JTable table, int modelIndex)
  2. saveTModelToCSV(String fileName, JTable table)
  3. setDataVector(DefaultTableModel model, Vector dataVector)
  4. tableModelToArray(JTable table)
  5. TableModelToList(JTable mode)
  6. toTableModel(HashMap data)