【问题标题】:EDITED Write JTable to Excel已编辑将 JTable 写入 Excel
【发布时间】:2017-09-19 21:02:25
【问题描述】:

我正在尝试将我的 JTable 导出到 Excel 文件。列名和行名很好,但我添加到 JTable 中的所有信息都没有被写入。我尝试了 System.out.println() 并在除列名和行名之外的所有地方打印 Null 值。我试图从谷歌妈妈那里得到答案,但经过 2 小时的阅读和尝试,仍然没有进展。 留在我脑海中的是,在写入 Excel 部分的代码中可能存在一些错误,或者添加到 JTable 的所有内容都只是我显示器上的图片,而不是其中的实际数据。? 如果我错了,请纠正我,非常感谢任何帮助。

这是写入 Excel 部分。 在第一个 For 循环中,我得到了标题,在第二个 For 循环中,我应该得到 JTable 中的所有内容,但我没有。

TableColumnModel tcm = nrp.rotaTable.getColumnModel();

    String nameOfFile = JOptionPane.showInputDialog("Name of the file");

    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 < nrp.tableModel.getRowCount(); i++) {
        row = sheet.createRow(i);
        for (int j = 0; j < tcm.getColumnCount(); j++) {

            cell = row.createCell(j);
            cell.setCellValue(tcm.getColumn(j).getHeaderValue().toString());

        }
    }
    for (int i = 1; i < nrp.tableModel.getRowCount(); i++) {
        row = sheet.createRow(i);
        System.out.println("");
        for (int j = 0; j < nrp.tableModel.getColumnCount(); j++) {

            cell = row.createCell(j);
            cell.setCellValue((String) nrp.tableModel.getValueAt(i, j)+" ");
            System.out.print((String) nrp.tableModel.getValueAt(i, j)+" ");
        }
    }


    File file = new File("Some name.xls");
    FileOutputStream out = new FileOutputStream(file);
    wb.write(out);
    out.close();
    wb.close();
  }
}

这里是 FocusListener 代码。

rotaTable.addFocusListener(new FocusListener() {
            public void focusGained(FocusEvent e) {
            }
            public void focusLost(FocusEvent e) {
                CellEditor cellEditor = rotaTable.getCellEditor();
                if (cellEditor != null)
                    if (cellEditor.getCellEditorValue() != null)
                        cellEditor.stopCellEditing();
                    else
                        cellEditor.cancelCellEditing();
            }
        });

我正在使用“DefaultTableModel”

 DefaultTableModel tableModel = new DefaultTableModel(12,8); 
JTable rotaTable = new JTable(tableModel); 

这是我第一次使用 POI 库。

我的 JTable 的图片http://imgur.com/a/jnB8j

控制台中打印结果的图片http://imgur.com/a/jnB8j

创建的 Excel 文件的图片。 http://imgur.com/a/jnB8j

【问题讨论】:

  • 第二个for,你为什么不从0开始?
  • 也许你必须把cell.setCellValue((String) nrp.tableModel.getValueAt(i, j)+" "); .. 改成这个cell.setCellValue((String) tcm.getValueAt(i, j)+" ");
  • 没有更多信息很难判断,您可能引用了错误的模型
  • 另外,你的第二个循环,你没有补偿行索引从1开始的事实,所以你冒着IndexOutOfBoundsException的风险
  • @Kh.Taheri 他们从1 开始,因为标题从0 开始,所以他们试图让excel 电子表格中的行被偏移...一些多么危险……

标签: java excel eclipse swing jtable


【解决方案1】:
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;

@SuppressWarnings("serial")
public class EmployeePanel extends JPanel {
    public ArrayList columnNames = new ArrayList();
    ArrayList data = new ArrayList();
    Connection conn;
    String query;
    String sql = "SELECT * FROM employee";
    String url = "jdbc:mysql://localhost:3306/nandos_db";
    String userid = "User1";
    String password = "Password1";
    Statement stmt;
    ResultSet rs;
    int rows = 0;
    int columns = 0;
    JTable employeeTable;

    @SuppressWarnings({ "unchecked" })
    public EmployeePanel() {
        super();
        this.setBackground(Color.GREEN);
        this.setLayout(null);
        this.setVisible(true);
        try {
            conn = DriverManager.getConnection(url, userid, password);
            stmt = (Statement) conn.createStatement();
            rs = stmt.executeQuery(sql);
            ResultSetMetaData md = (ResultSetMetaData) rs.getMetaData();

            int columns = md.getColumnCount();
            for (int i = 1; i <= 3; i++) {
                columnNames.add(md.getColumnName(i));
            }
            while (rs.next()) {
                ArrayList row = new ArrayList(columns);
                for (int i = 1; i <= columns; i++) {
                    row.add(rs.getObject(i));
                }
                data.add(row);
            }
            // Get row data
        } catch (SQLException e) {
            // System.out.println(e.getMessage());
            e.printStackTrace();
        }
        // Create Vectors and copy over elements from ArrayLists to them
        // a custom defined class which inherits from the AbstractTableModel
        // class
        Vector columnNamesVector = new Vector();
        Vector dataVector = new Vector();
        for (int i = 0; i < data.size(); i++) {
            ArrayList subArray = (ArrayList) data.get(i);
            Vector subVector = new Vector();
            for (int j = 0; j < subArray.size(); j++) {
                subVector.add(subArray.get(j));
            }
            dataVector.add(subVector);
        }
        for (int i = 0; i < columnNames.size(); i++) {
            columnNamesVector.add(columnNames.get(i));
        }
        // Create table with database data
        employeeTable = new JTable(dataVector, columnNamesVector);
        employeeTable.getTableHeader().setOpaque(false);
        employeeTable.getTableHeader().setBackground(Color.pink);
        employeeTable.setBackground(Color.orange);
        employeeTable.setSelectionMode((ListSelectionModel.SINGLE_SELECTION));
        employeeTable.setEnabled(true);
        employeeTable.setDragEnabled(false);
        employeeTable.setCellEditor(null);

        JScrollPane sp = new JScrollPane(employeeTable);
        sp.setSize(1030, 540);
        sp.setLocation(10, 10);
        this.add(sp);
    }

    public static void main(String... args) throws IOException {
        JFrame f = new JFrame();
        EmployeePanel t = new EmployeePanel();
        f.add(t);
        f.setVisible(true);
        f.pack();

        System.out.println(t.employeeTable.getValueAt(0, 0));

        Workbook wb = new HSSFWorkbook();
        CreationHelper createhelper = wb.getCreationHelper();

        Sheet sheet = wb.createSheet("new sheet");
        Row row = null;
        Cell cell = null;
        row = sheet.createRow(0);
        for (int i = 0; i < t.employeeTable.getColumnCount(); i++) {
            cell = row.createCell(i);
            cell.setCellValue(String.valueOf(t.employeeTable.getColumnName(i)));
        }

        for (int j = 1; j <= t.employeeTable.getRowCount(); j++) {
            row = sheet.createRow(j);
            for (int i = 0; i < t.employeeTable.getColumnCount(); i++) {
                cell = row.createCell(i);

                cell.setCellValue(String.valueOf(t.employeeTable.getValueAt(j - 1, i)));

            }
        }

        File file = new File("C:\\Users\\test\\Desktop\\Some name.xls");
        FileOutputStream out = new FileOutputStream(file);
        wb.write(out);
        out.close();
        wb.close();

    }

}

如果我让这段代码连接到我的数据库,我会得到一个与框架中的表格内容完全相同的 Excel 表格。由于您显然还有一些尚未显示的代码,因此您可能需要适当地调整它。这段代码在 1 个类中都可以工作

【讨论】:

    【解决方案2】:

    一旦我写了这段代码,用于将一组 JTables 写入 Excel 文件(一个并排)。 正如我所看到的,它实际上与您编写的代码相同。你可以试试这个而不是你阻止。您必须替换 Entry 对象或将表包装到 Map 中

    for (int i = 1; i < nrp.tableModel.getRowCount(); i++) {
    ...
    }
    

    因为我需要将表格一个接一个地写入,所以我使用变量 fila 和 columna 来获取当前位置以放置下一个表格。 您可以将它们设置为 0 或 1 以进行测试。 也许它可以帮助你产生新的想法。

    我注意到的唯一区别是: if (row == null) {...}

    private void escribirContenido(Sheet hoja, Entry<String, JTable> e) {
        Row row;
        Cell cell;
        for (int i = fila; i < e.getValue().getRowCount() + fila; i++) {
            row = hoja.getRow(i);
            if (row == null) {
                row = hoja.createRow(i);
            }
            for (int j = 0; j < e.getValue().getColumnCount(); j++) {
                cell = row.createCell(j + columna);
                String value = String.valueOf(e.getValue().getValueAt(i - fila, j));
                try {
                    cell.setCellValue(new Double(value));
                } catch (NumberFormatException e1) {
                    cell.setCellValue(value);
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      您必须从 0 开始行索引,因为表格行从 0 开始并从 1 创建 excel 行,因为首先为您编写列名。我修改了您的第二个 for 循环,如下所示:

      for (int i= 0; i < nrp.tableModel.getRowCount(); i++) {
          row = sheet.createRow(i+1);
          System.out.println("");
          for (int j = 0; j < nrp.tableModel.getColumnCount(); j++) {
              cell = row.createCell(j);
              if(nrp.tableModel.getValueAt(i, j)!=null){
              cell.setCellValue((String) nrp.tableModel.getValueAt(i, j));
              }
              System.out.print((String) nrp.tableModel.getValueAt(i, j)+" ");
          }
      }
      

      【讨论】:

      • imgur.com/LxxdD19 没有任何变化,同样的输出,只有空值没有复制到excel文件中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-14
      相关资源
      最近更新 更多