开发程序时需要用到 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
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