如何把JTABLE中的数据导出到EXCEL表中 要代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 借用package com.tabel4;import java.io.*; import javax.swing.*; import javax.swing.table.DefaultTableModel;import jxl.*; import jxl.read.*; import jxl.write.*; import jxl.Workbook; import jxl.write.Number; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; public class creatExcel { public void creatExcel(File file,DefaultTableModel tableModel,JTable jTable) { Label labelS = null; Number labelN = null; try{ FileOutputStream os = new FileOutputStream(file); WritableWorkbook wwb = Workbook.createWorkbook(os); WritableSheet ws = wwb.createSheet("选择题试题",0); for(int i = 0;i<tableModel.getColumnCount();i++){ Label label = new Label(i,0,tableModel.getColumnName(i)); ws.addCell(label); } for(int j =0 ; j<jTable.getRowCount();j++){ for(int i = 0;i<jTable.getColumnCount();i++){ if(jTable.getValueAt(j, i).getClass().equals(Integer.class)){ labelN = new Number(i,j+1,(Integer)jTable.getValueAt(j, i)); ws.addCell(labelN); }else if(jTable.getValueAt(j, i).getClass().equals(String.class)){ labelS = new Label(i,j+1,(String)jTable.getValueAt(j, i)); ws.addCell(labelS); } } } // 将内容写到文件中 wwb.write(); // 将wwb关闭 wwb.close(); JOptionPane.showMessageDialog(null,"导出到"+file.getName()+"成功","成功",JOptionPane.INFORMATION_MESSAGE); }catch(FileNotFoundException fnfe){ System.out.println("文件"+file.getName()+"没有找到"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (RowsExceededException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 大家帮我看看这个java链表问题出在哪? 如何取出ArrayList中元素为HashMap的HashMap对象指定值 过滤器实现权限验证解释 标中输出的问题 类的含义问题 求另一种的方法调用的解释 问一个Class method and Instance class的问题 如何生成一个jar,怎么运行一个jar??? 哪位大哥用过JET1.0,请告诉小弟那里有下载!!! 郁闷,继续散分 一个死锁的问题 关于线程的小问题,求解释,谢谢!!
package com.tabel4;import java.io.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;import jxl.*;
import jxl.read.*;
import jxl.write.*;
import jxl.Workbook;
import jxl.write.Number;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class creatExcel {
public void creatExcel(File file,DefaultTableModel tableModel,JTable jTable)
{
Label labelS = null;
Number labelN = null;
try{
FileOutputStream os = new FileOutputStream(file);
WritableWorkbook wwb = Workbook.createWorkbook(os);
WritableSheet ws = wwb.createSheet("选择题试题",0);
for(int i = 0;i<tableModel.getColumnCount();i++){
Label label = new Label(i,0,tableModel.getColumnName(i));
ws.addCell(label);
}
for(int j =0 ; j<jTable.getRowCount();j++){
for(int i = 0;i<jTable.getColumnCount();i++){
if(jTable.getValueAt(j, i).getClass().equals(Integer.class)){
labelN = new Number(i,j+1,(Integer)jTable.getValueAt(j, i));
ws.addCell(labelN);
}else if(jTable.getValueAt(j, i).getClass().equals(String.class)){
labelS = new Label(i,j+1,(String)jTable.getValueAt(j, i));
ws.addCell(labelS);
}
}
}
// 将内容写到文件中
wwb.write();
// 将wwb关闭
wwb.close();
JOptionPane.showMessageDialog(null,"导出到"+file.getName()+"成功","成功",JOptionPane.INFORMATION_MESSAGE);
}catch(FileNotFoundException fnfe){
System.out.println("文件"+file.getName()+"没有找到");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}