您的位置:首页 > 其它

如何将jTABLE的数据写入EXCEL中

2014-09-23 21:39 260 查看
2008-07-27 19:31:59| 分类: J***A|举报|字号 订阅

package com.jiandian.zhuangwaiyun.db.dao;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import jxl.*;

import jxl.write.Label;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import jxl.write.WriteException;

import jxl.write.biff.RowsExceededException;

import javax.swing.JOptionPane;

import javax.swing.JTable;

//EXCL读取类

public class ReaderXls {

public void reader(String filename ,JTable table,String []mane){//传参,分别为,文件名,表名和表名上的各个属性值

File fileWrite = new File(filename);//声明一个文件类

try {

fileWrite.createNewFile();

OutputStream os = new FileOutputStream(fileWrite);

WritableWorkbook wwb = Workbook.createWorkbook(os);

//创建子表并写入数据

WritableSheet ws = wwb.createSheet("Test Sheet 1", 0);

//取得TABLE的行数

int a = table.getRowCount();

//取得TABLE的列数

int b =table.getColumnCount();

for(int k =0;k < b;k++){

jxl.write.Label labelN = new jxl.write.Label(k, 0, mane[k]);

try {

ws.addCell(labelN);

} catch (RowsExceededException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (WriteException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

for(int i =0;i<b ;i++){

for(int j=1;j<=a;j++){

String str = null;

str = (String) table.getValueAt(j-1, i);

jxl.write.Label labelN = new jxl.write.Label(i, j, str);

try {

ws.addCell(labelN);

} catch (RowsExceededException e) {

e.printStackTrace();

} catch (WriteException e) {

e.printStackTrace();

}

}

}

//写入工作表

wwb.write();



try {

wwb.close();

JOptionPane.showMessageDialog(null, "在"+filename+"成功导入数据");

} catch (WriteException e) {

e.printStackTrace();

}

} catch (FileNotFoundException e) {

JOptionPane.showMessageDialog(null, "导入数据前请关闭工作表");

} catch (Exception e) {

JOptionPane.showMessageDialog(null, "没有进行筛选");



}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: