Java将Excel显示到jframe_java – 在excel文件中导出Jtable

这是一个关于如何将Java Swing中的JTable数据导出到Excel文件的问题。作者通过创建一个名为`Export`的JFrame,展示了一个包含计算结果的JTable,并提供了一个按钮用于导出数据到Excel。当点击按钮时,数据被写入到一个XML格式的Excel文件中。然而,打开文件后发现内容为空。解决方案推荐使用Apache POI库来更可靠地生成Excel文件。
摘要由CSDN通过智能技术生成

嗨,我是java编程的新手,但这是我的问题

我正在设计GUI进行一些计算,需要在J表中显示输入和输出,然后将它们导出到excel文件中

代码工作就像魅力,但当我打开excel文件时,我发现它是空的..

j表

import java.awt.Desktop;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import javax.swing.JFileChooser;

public class Export extends javax.swing.JFrame {

public Export() {

initComponents();

}

@SuppressWarnings("unchecked")

//

private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();

jTable1 = new javax.swing.JTable();

jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTable1.setModel(new javax.swing.table.DefaultTableModel(

new Object [][] {

{"Channel Model", global.channel_model, global.channel_model},

{"System Bandwidth (MHz)", global.band_width_index, global.band_width_index},

{"Cell Edge Rate (Kbps)", global.Rreq_UL, global.Rreq_DL},

{"Cell edge MCS", global.mcs, global.mcs},

{"Antenna Configuration", global.DL_antenna_config, global.DL_antenna_config},

{"Total RB Number", global.number_of_RB, global.number_of_RB},

{"Tx", "", null},

{"Max Power (dBm)", global.UE_Tx_power, global.UE_Tx_power},

{"Cable Loss (dB)", global.cable_loss, global.cable_loss},

{"Body Loss (dB)", global.body_loss, global.body_loss},

{"Antenna Gain (dB)", global.UE_antennaGain, global.UE_antennaGain},

{"EIRB (dB)", "", null},

{"Rx", null, null},

{"Antenna Gain (dB)", global.UE_antennaGain, global.UE_antennaGain},

{"Cable Loss (dB)", global.cable_loss, global.cable_loss},

{"Body Loss (dB)", global.body_loss, global.body_loss},

{"Noise Figure (dB)", global.UE_noiseFigure, global.UE_noiseFigure},

{"Thermal Noise (dB)", null, null},

{"Interference Margin (dB)", global.Biul, global.Bidl},

{"SINR (dB)", global.SINR, global.DL_SINR},

{"Reciver Sensitivty (dB)", "", null},

{"MAPL", null, null},

{"Diversity (dB)", global.Tx_diversity_gain, global.Tx_diversity_gain},

{"Shadow Fading Margin (dB)", global.shadow_margin, global.shadow_margin},

{"MAPL (dB)", null, null},

{"Cell Radius (Km)", global.R, global.R},

{"Area of Dimensioning (Km squar", global.site_area, global.site_area},

{"Site Type", global.type_of_site, global.type_of_site},

{"Number of Sites Due to Coverage", "", null}

},

new String [] {

"Summary", "Uplink", "Downlink"

}

));

jTable1.setName("Export");

jScrollPane1.setViewportView(jTable1);

jTable1.getAccessibleContext().setAccessibleName("\"Export\"");

jTable1.getAccessibleContext().setAccessibleDescription("");

jButton1.setText("Export");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

}

});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap(562, Short.MAX_VALUE)

.addComponent(jButton1)

.addContainerGap())

.addComponent(jScrollPane1)

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 455, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jButton1)

.addGap(0, 0, Short.MAX_VALUE))

);

pack();

}//

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

try{

JFileChooser fileChooser = new JFileChooser();

int retval = fileChooser.showSaveDialog(jButton1);

if (retval == JFileChooser.APPROVE_OPTION) {

File file = fileChooser.getSelectedFile();

if (file != null) {

if (!file.getName().toLowerCase().endsWith(".xls")) {

file = new File(file.getParentFile(), file.getName() + ".xls");

}

try {

ExcelExporter exp=new ExcelExporter();

exp.exportTable(jTable1, file);

Desktop.getDesktop().open(file);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

System.out.println("not found");

} catch (IOException e) {

e.printStackTrace();

}

}

}

}catch(Exception e){

System.out.println("shit");

}

}

public static void main(String args[]) {

try {

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

}

}

} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(Export.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(Export.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(Export.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(Export.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

}

//

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new Export().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JTable jTable1;

// End of variables declaration

}

excel生成器类

import java.io.*;

import javax.swing.table.TableModel;

import javax.swing.*;

public class ExcelExporter {

ExcelExporter(){}

public void exportTable(JTable jTable1,File file) throws IOException{

TableModel model=jTable1.getModel();

FileWriter out=new FileWriter(file);

BufferedWriter bw=new BufferedWriter(out);

for (int i=0;i

bw.write(model.getColumnName(i)+"\t");

}

bw.write("\n");

for (int i=0;i

for (int j=0;j

bw.write(model.getValueAt(i,j).toString()+"\t");

}

bw.write("\n");

}

bw.close();

System.out.print("Write out to"+file);

}

}

解决方法:

对于编写和操作Excel文件,请使用Apache POI http://poi.apache.org/

如果您想在不使用外部库的情况下自己创建Excel文件,请记住excel文件数据以XML电子表格的形式存储,因此您必须创建类似的文件

xmlns:o="urn:schemas-microsoft-com:office:office"

xmlns:x="urn:schemas-microsoft-com:office:excel"

xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"

xmlns:html="http://www.w3.org/TR/REC-html40">

Name

Example

Value

123

标签:java,excel,button,swing

来源: https://codeday.me/bug/20190517/1120114.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值