繁体   English   中英

将 JTable 数据(包括表头)导出到 Excel 文件

[英]Exporting JTable Data including Table Headers to Excel File

我需要这方面的帮助,我做不到。 我想将 JTable 数据导出到 excel 文件。 没有错误,但我只得到它显示的数据,不包括表头。

我在 Netbeans 8.2 IDE 中运行它,并且还导入了基本的 jar 文件。

我的 jtable 上的数据由我的 mysql 数据库提供,我需要将其从 jtable 导出到 ZBF57C906FA7D415BB66D96Z 文件。 无论如何 tblData 是我的 JTable 变量名。

private void btnExportActionPerformed(java.awt.event.ActionEvent evt) {                                          

        FileOutputStream excelFOS = null;
        BufferedOutputStream excelBOS = null;
        XSSFWorkbook wb = null;


        JFileChooser excelFileChooser = new JFileChooser();
        excelFileChooser.setDialogTitle("Save As");
        FileNameExtensionFilter fnef = new FileNameExtensionFilter("Excel Files","xls","xlsx","ods");
        excelFileChooser.setFileFilter(fnef);
        int excelChooser = excelFileChooser.showSaveDialog(null);

        if(excelChooser == JFileChooser.APPROVE_OPTION){

            try {
                wb = new XSSFWorkbook();
                XSSFSheet sheet = wb.createSheet("Data Sheet");

                for(int i = 0; i < tblData.getRowCount(); i++){
                    XSSFRow excelRow = sheet.createRow(i);
                    for(int j = 0; j < tblData.getColumnCount(); j++){

                        XSSFCell excelCell = excelRow.createCell(j);
                        excelCell.setCellValue(tblData.getValueAt(i, j).toString());

                    }
                }   

                excelFOS = new FileOutputStream(excelFileChooser.getSelectedFile() + ".xlsx");
                excelBOS = new BufferedOutputStream(excelFOS);
                wb.write(excelBOS);
                JOptionPane.showMessageDialog(null, "Successfully saved.");

            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if(excelBOS != null){
                         excelBOS.close();
                    }
                    if(excelFOS != null){
                         excelFOS.close();
                    }
                    if(wb != null){
                         wb.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            } //---- end finally
        } //---- end if condition
    }

我期待 header 被导出。 谁能帮我这个。

不太清楚您提供的代码中的tblData是什么。 But If I would have the requirement to export a JTable to Excel , then I would go by TableModel of the JTable . 首先使用TableModel.getColumnName将列名写入Excel表的第一行。 然后使用TableModel.getValueAt将表数据写入Excel表的下一行。

完整示例:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;

import javax.swing.JTable;
import javax.swing.table.TableModel;

class WriteJTableToExcel {

 static void exportToExcel(JTable table, String filePath) throws Exception {
  TableModel model = table.getModel();
  Workbook workbook = new XSSFWorkbook();
  Sheet sheet = workbook.createSheet();
  Row row;
  Cell cell;

  // write the column headers
  row = sheet.createRow(0);
  for (int c = 0; c < model.getColumnCount(); c++) {
   cell = row.createCell(c);
   cell.setCellValue(model.getColumnName(c));
  }

  // write the data rows
  for (int r = 0; r < model.getRowCount(); r++) {
   row = sheet.createRow(r+1);
   for (int c = 0; c < model.getColumnCount(); c++) {
    cell = row.createCell(c);
    Object value = model.getValueAt(r, c);
    if (value instanceof String) {
     cell.setCellValue((String)value);
    } else if (value instanceof Double) {
     cell.setCellValue((Double)value);
    }
   }
  }

  FileOutputStream out = new FileOutputStream(filePath);
  workbook.write(out);
  out.close();
  workbook.close();

 }

 public static void main(String[] args) throws Exception {

  Object columnNames[] = {"Name", "Amount", "Factor"};
  Object rowData[][] = {
   {"Bob", 12.0, 3.0},
   {"Alice", 34.0, 2.5},
   {"Jack", 56.0, 2.0},
   {"John", 78.0, 1.5}
  };
  JTable table = new JTable(rowData, columnNames);

  exportToExcel(table, "./Excel.xlsx");

 }
}

在导出数据之前,您需要分别从 jTable 导出 getTableHeader()。 它不与数据一起存储,因为它在 Excel 工作簿中。
希望有帮助:-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM