Here you can find the source of toCsv(TableModel model)
Parameter | Description |
---|---|
model | The table model to write |
public static String toCsv(TableModel model)
//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(); } }