本文主要是介绍如何将jTABLE的数据写入EXCEL中,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
2008-07-27 19:31:59| 分类: JAVA|举报|字号 订阅
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, "没有进行筛选");
}
}
}
这篇关于如何将jTABLE的数据写入EXCEL中的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!