
本站地址:http://www.bajiao123.com

用JXL写EXCEL方法的封装[原]

前些日子写了一些关于EXCEL解决的方法,今天用JXL写文件的方法,来处理。把封装好的东西发上来,大家一起看看。
import java.util.*;
import java.io.*;
import java.sql.*;
import jxl.*;
import jxl.write.*;
/**
* 提供了常用的Excel读取和写入的方法
* <p> * Title: * </p>
* <p> * Description: * </p>
* <p> * Copyright: Copyright (c) 2006 * </p>
* <p> * Company: * </p>
* * @author wujiaqian * @version 1.0
*/
public class Excel {
int sheetCount = 1; // excel工作簿,默认为1
WritableWorkbook wwb = null; // 构建Workbook对象,只读Workbook对象
Vector vSheet = null;
/**
* 无参构造函数,生成默认名字的excel文件
*/
public Excel() {
this("noName.excel");
}
/**
* 带有一个String类型参数的构造函数
*
* @param fileName
* String 将要生成的excel文件名
*/
public Excel(String fileName) {
try {
wwb = Workbook.createWorkbook(new File(fileName));
vSheet = new Vector(5);
} catch (Exception e) {
}
}
/**
* 带有一个File类型参数的构造函数
*
* @param fileName
* String 将要生成的excel文件名
*/
public Excel(File file) {
try {
wwb = Workbook.createWorkbook(file);
vSheet = new Vector(5);
} catch (Exception e) {
}
}
/**
* 读取一个EXCEL文件的所有行和列,在同一行上的数据以一个String的形式保存在Vector中,各列数据以","号分割
*
* @param fileName
* String 文件名
* @throws Exception
* @return Vector
*/
public static Vector readFromExcel(String fileName) throws Exception {
Vector v = new Vector();
File file = new File(fileName);
if (!file.isFile()) {
return null;
}
v = readExl(file, -1, -1);
return v;
}
public static Vector readFromExcel(File file) throws Exception {
Vector v = new Vector();
if (!file.isFile()) {
return null;
}
v = readExl(file, -1, -1);
return v;
}
/**
* 读取一行多列或者一列多行
*
* @param fileName
* String 文件名
* @param rowORcol
* int 第几行或者第几列
* @param flag
* String ROW表示前面一个参数的值指的是行数,行COL表示前面一个参数的值指的是列数
* @throws Exception
* @return Vector
*/
public static Vector readFromExcel(String fileName, int rowORcol,
String flag) throws Exception {
Vector v = new Vector();
File file = new File(fileName);
if (!file.isFile()) {
return null;
}
if (flag != null && flag.equals("ROW")) {
v = readExl(file, rowORcol, -1);
} else if (flag != null && flag.equals("COL")) {
v = readExl(file, -1, rowORcol);
} else {
return null;
}
return v;
}
本站地址:http://www.bajiao123.com

