如何将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, "没有进行筛选");
}
}
}
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, "没有进行筛选");
}
}
}
相关文章推荐
- C#中如何将DataTable中的数据写入Excel
- python 在excel文件中写入date日期数据,以及读取excel日期数据,如何在python中正确显示date日期。
- swing中如何将jtable中的数据导入到excel中?
- swing中如何将jtable中的数据导入到excel中?
- C#中创建、打开、读取、写入、保存Exce,C#如何向EXCEL写入数据
- 如何操作Excel模板,往Excel模板中写入数据
- 按键精灵如何调用Excel及按键精灵写入Excel数据的方法教程---入门自动操作表格
- C#如何向EXCEL写入数据
- 如何将Excel和xml文件数据保存到数据库
- [转贴]EXCEL读取与写入数据的最佳方案
- 如何将Excel和xml文件数据保存到数据库
- 如何在写入一个long型的数据到文件?在线等。20分送!
- 如何自动更新JTable中的数据(代码实现)
- 如何用ASP对EXCEL表进行写入数据???[转]
- 如何将数据从DATASET写入XML文档,并转换成其他应用系统所需的格式!(如RSS阅读器XML文档格式)
- 如何将 Excel 数据导入 SQL Server
- 如何成功把EXCEL表的数据导入到SQL数据库,代码如何编写
- EXCEL读取与写入数据的最佳方案
- 以实例说明如何使用C#从数据库中提取数据,按要求自动生成定制的Excel表格?[转]
- 如何将网页数据到入EXCEL里面