开发程序时需要用到 jxl.jar 下载地址:http://nchc.dl.sourceforge.net/sourceforge/jexcelapi/jexcelapi_2_6_6.zip
下载后将zip包解压,复制jxl.jar到你的project的lib包中。
本程序由两个类组成,可直接复制后运行
Excel.java
package
test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class Excel ... {
/** *//**
* 导出数据为XLS格式
* @param fileName 文件的名称,可以设为绝对路径,也可以设为相对路径
* @param content 数据的内容
*/
public static void exportExcel(String fileName, Vector<Person> content) ...{
WritableWorkbook wwb;
FileOutputStream fos;
try ...{
fos = new FileOutputStream(fileName);
wwb = Workbook.createWorkbook(fos);
WritableSheet ws = wwb.createSheet("三国志武将列表", 10); // 创建一个工作表
// 设置单元格的文字格式
WritableFont wf = new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD,false,
UnderlineStyle.NO_UNDERLINE,Colour.BLUE);
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf.setAlignment(Alignment.CENTRE);
ws.setRowView(1, 500);
// 填充数据的内容
Person[] p = new Person[content.size()];
for (int i = 0; i < content.size(); i++)...{
p[i] = (Person)content.get(i);
ws.addCell(new Label(1, i + 1, p[i].getName(), wcf));
ws.addCell(new Label(2, i + 1, p[i].getNickname(), wcf));
ws.addCell(new Label(3, i + 1, p[i].getPower(), wcf));
ws.addCell(new Label(4, i + 1, p[i].getWit(), wcf));
ws.addCell(new Label(5, i + 1, p[i].getPolity(), wcf));
ws.addCell(new Label(6, i + 1, p[i].getCharm(), wcf));
ws.addCell(new Label(7, i + 1, p[i].getStory(), wcf));
if(i == 0)
wcf = new WritableCellFormat();
}
wwb.write();
wwb.close();
} catch (IOException e)...{
} catch (RowsExceededException e)...{
} catch (WriteException e)...{}
}
/** *//**
* 从Excel文件里读取数据保存到Vector里
* @param fileName Excel文件的名称
* @return Vector对象,里面包含从Excel文件里获取到的数据
*/
public static Vector<Person> importExcel(String fileName)...{
Vector<Person> v = new Vector<Person>();
try ...{
Workbook book = Workbook.getWorkbook(new File(fileName));
Sheet sheet = book.getSheet(0); // 获得第一个工作表对象
int rows = sheet.getRows();
for(int i = 0; i < rows; i++) ...{
Cell [] cell = sheet.getRow(i);
if(cell.length == 0)
continue;
Person p = new Person();
p.setName(sheet.getCell(1, i).getContents());
p.setNickname(sheet.getCell(2, i).getContents());
p.setPower(sheet.getCell(3, i).getContents());
p.setWit(sheet.getCell(4, i).getContents());
p.setPolity(sheet.getCell(5, i).getContents());
p.setCharm(sheet.getCell(6, i).getContents());
p.setStory(sheet.getCell(7, i).getContents());
v.add(p);
}
book.close();
}catch(Exception e) ...{}
return v;
}
public static void main(String [] args)...{
String fileName = "test.xls";
String fileNameNew = "testNew.xls";
Person p0 = new Person("姓名","字","武力","智力","政治","魅力","英雄事迹");
Person p1 = new Person("赵云","子龙","98","84","83","87","单骑救主!!!");
Person p2 = new Person("马超","孟起","98","62","40","88","杀得曹操割须弃袍!!!");
Person p3 = new Person("诸葛亮","孔明","55","100","92","93","死后木偶退兵,锦囊杀魏延!!!");
Vector<Person> v = new Vector<Person>();
v.add(p0);
v.add(p1);
v.add(p2);
v.add(p3);
exportExcel(fileName, v);
System.out.println("成功导出数据到Excel文件(" + fileName + ")了!!!");
Vector<Person> vector = importExcel(fileName);
System.out.println("成功从Excel文件(" + fileName + ")导入数据!!!");
exportExcel(fileNameNew, vector);
System.out.println("成功将" + fileName + "里的数据手复制到" + fileNameNew + "中!!!");
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class Excel ... {
/** *//**
* 导出数据为XLS格式
* @param fileName 文件的名称,可以设为绝对路径,也可以设为相对路径
* @param content 数据的内容
*/
public static void exportExcel(String fileName, Vector<Person> content) ...{
WritableWorkbook wwb;
FileOutputStream fos;
try ...{
fos = new FileOutputStream(fileName);
wwb = Workbook.createWorkbook(fos);
WritableSheet ws = wwb.createSheet("三国志武将列表", 10); // 创建一个工作表
// 设置单元格的文字格式
WritableFont wf = new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD,false,
UnderlineStyle.NO_UNDERLINE,Colour.BLUE);
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf.setAlignment(Alignment.CENTRE);
ws.setRowView(1, 500);
// 填充数据的内容
Person[] p = new Person[content.size()];
for (int i = 0; i < content.size(); i++)...{
p[i] = (Person)content.get(i);
ws.addCell(new Label(1, i + 1, p[i].getName(), wcf));
ws.addCell(new Label(2, i + 1, p[i].getNickname(), wcf));
ws.addCell(new Label(3, i + 1, p[i].getPower(), wcf));
ws.addCell(new Label(4, i + 1, p[i].getWit(), wcf));
ws.addCell(new Label(5, i + 1, p[i].getPolity(), wcf));
ws.addCell(new Label(6, i + 1, p[i].getCharm(), wcf));
ws.addCell(new Label(7, i + 1, p[i].getStory(), wcf));
if(i == 0)
wcf = new WritableCellFormat();
}
wwb.write();
wwb.close();
} catch (IOException e)...{
} catch (RowsExceededException e)...{
} catch (WriteException e)...{}
}
/** *//**
* 从Excel文件里读取数据保存到Vector里
* @param fileName Excel文件的名称
* @return Vector对象,里面包含从Excel文件里获取到的数据
*/
public static Vector<Person> importExcel(String fileName)...{
Vector<Person> v = new Vector<Person>();
try ...{
Workbook book = Workbook.getWorkbook(new File(fileName));
Sheet sheet = book.getSheet(0); // 获得第一个工作表对象
int rows = sheet.getRows();
for(int i = 0; i < rows; i++) ...{
Cell [] cell = sheet.getRow(i);
if(cell.length == 0)
continue;
Person p = new Person();
p.setName(sheet.getCell(1, i).getContents());
p.setNickname(sheet.getCell(2, i).getContents());
p.setPower(sheet.getCell(3, i).getContents());
p.setWit(sheet.getCell(4, i).getContents());
p.setPolity(sheet.getCell(5, i).getContents());
p.setCharm(sheet.getCell(6, i).getContents());
p.setStory(sheet.getCell(7, i).getContents());
v.add(p);
}
book.close();
}catch(Exception e) ...{}
return v;
}
public static void main(String [] args)...{
String fileName = "test.xls";
String fileNameNew = "testNew.xls";
Person p0 = new Person("姓名","字","武力","智力","政治","魅力","英雄事迹");
Person p1 = new Person("赵云","子龙","98","84","83","87","单骑救主!!!");
Person p2 = new Person("马超","孟起","98","62","40","88","杀得曹操割须弃袍!!!");
Person p3 = new Person("诸葛亮","孔明","55","100","92","93","死后木偶退兵,锦囊杀魏延!!!");
Vector<Person> v = new Vector<Person>();
v.add(p0);
v.add(p1);
v.add(p2);
v.add(p3);
exportExcel(fileName, v);
System.out.println("成功导出数据到Excel文件(" + fileName + ")了!!!");
Vector<Person> vector = importExcel(fileName);
System.out.println("成功从Excel文件(" + fileName + ")导入数据!!!");
exportExcel(fileNameNew, vector);
System.out.println("成功将" + fileName + "里的数据手复制到" + fileNameNew + "中!!!");
}
}
Person.java
package
test;
public class Person ... {
private String name;
private String nickname;
private String power;
private String wit;
private String polity;
private String charm;
private String story;
public Person()...{
}
public Person(String name, String nickname, String power, String wit,
String polity, String charm, String story) ...{
super();
this.name = name;
this.nickname = nickname;
this.power = power;
this.wit = wit;
this.polity = polity;
this.charm = charm;
this.story = story;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getNickname() ...{
return nickname;
}
public void setNickname(String nickname) ...{
this.nickname = nickname;
}
public String getPower() ...{
return power;
}
public void setPower(String power) ...{
this.power = power;
}
public String getWit() ...{
return wit;
}
public void setWit(String wit) ...{
this.wit = wit;
}
public String getPolity() ...{
return polity;
}
public void setPolity(String polity) ...{
this.polity = polity;
}
public String getCharm() ...{
return charm;
}
public void setCharm(String charm) ...{
this.charm = charm;
}
public String getStory() ...{
return story;
}
public void setStory(String story) ...{
this.story = story;
}
}
public class Person ... {
private String name;
private String nickname;
private String power;
private String wit;
private String polity;
private String charm;
private String story;
public Person()...{
}
public Person(String name, String nickname, String power, String wit,
String polity, String charm, String story) ...{
super();
this.name = name;
this.nickname = nickname;
this.power = power;
this.wit = wit;
this.polity = polity;
this.charm = charm;
this.story = story;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getNickname() ...{
return nickname;
}
public void setNickname(String nickname) ...{
this.nickname = nickname;
}
public String getPower() ...{
return power;
}
public void setPower(String power) ...{
this.power = power;
}
public String getWit() ...{
return wit;
}
public void setWit(String wit) ...{
this.wit = wit;
}
public String getPolity() ...{
return polity;
}
public void setPolity(String polity) ...{
this.polity = polity;
}
public String getCharm() ...{
return charm;
}
public void setCharm(String charm) ...{
this.charm = charm;
}
public String getStory() ...{
return story;
}
public void setStory(String story) ...{
this.story = story;
}
}
运行程序后,你的project里将会自动生成一个test.xls文件,双击打开就可以看到导出的数据了。
本文发表后,许多网友来信说无法编译运行。鉴于此,特地对本文作以下补充。
其实本文的代码是在IDE中编译运行的,如果是用命令行窗口的模式编译运行的话,需要注意以下两点:
1、去除两个类中的第一行代码,即:package test;
2、配置好环境变量,特别是记得在classpath中设置jxl.jar的所在路径。