首先创建一个实体
import java.io.Serializable;public class DataPojo implements Serializable { /**
 * 
 */
private static final long serialVersionUID = 6230244466642061219L;
/**
 * 序号
 */
private String dataId;
/**
 * 公司中文名
 */
private String cnName;
/**
 * 开户帐号
 */
private String bankAccount;
/**
 * 结算总额
 */
private String settleFee; public String getDataId() {
return dataId;
} public void setDataId(String dataId) {
this.dataId = dataId;
} public String getCnName() {
return cnName;
} public void setCnName(String cnName) {
this.cnName = cnName;
} public String getBankAccount() {
return bankAccount;
} public void setBankAccount(String bankAccount) {
this.bankAccount = bankAccount;
} public String getSettleFee() {
return settleFee;
} public void setSettleFee(String settleFee) {
this.settleFee = settleFee;
}}读取excel的一个类
import java.io.*;
import java.util.ArrayList;
import java.util.List;import jxl.*;public class ReadExcel {
public static List<DataPojo> readExcel() {
List<DataPojo> list = new ArrayList<DataPojo>();
try {
Workbook book = Workbook.getWorkbook(new File("1000条测试数据.xls"));
// 获得第一个工作表对象
Sheet sheet = book.getSheet(0);
int cols = sheet.getColumns(); // 获取excel 列数
int rows = sheet.getRows();// 获取excel行数
// 循环打印excel中的数据
// 如果excel中有标题如果不用读取标题的时候 则根据要读取的行开始
// 在开发中一搬我们都会给用户一个固定的模板 上传数据
// 所以excel中的哪一列是什么数据我们很清楚 for (int i = 2; i < rows; i++) {
DataPojo pojo = new DataPojo();
Cell cell1 = sheet.getCell(0, i);
String dataId = cell1.getContents();
pojo.setDataId(dataId); Cell cell2 = sheet.getCell(1, i);
String cnName = cell2.getContents();
pojo.setCnName(cnName); Cell cell3 = sheet.getCell(2, i);
String bankAccount = cell3.getContents();
pojo.setBankAccount(bankAccount); Cell cell4 = sheet.getCell(3, i);
String settleFee = cell4.getContents();
pojo.setSettleFee(settleFee); list.add(pojo);
}
book.close();
} catch (Exception e) {
System.out.println(e);
}
return list;
} public static void main(String args[]) {
readExcel();
}
}
导出的类
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import java.io.*;
import java.util.List;import jxl.*;
import jxl.write.*;public class CreateExcel {
public static void main(String args[]) {
List<DataPojo> list = ReadExcel.readExcel();
createExcel(list);
}

public static void createExcel(List<DataPojo> list) {
try {
File file = new File("测试1.xls");
// 打开文件
WritableWorkbook book = Workbook.createWorkbook(file);
// 生成名为“第一页”的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet("第一页", 0);
// 在label对象的构造子中指名单元格位置是第一列第一行(0,0)
// 以及单元格内容为test
Label label0 = new Label(0, 0, "2010年09月结算总额单", setTitle(20));
Label label1 = new Label(0, 1, "序号",setTitle(13));
Label label2 = new Label(1, 1, "公司中文名",setTitle(13));
Label label3 = new Label(2, 1, "开户帐号",setTitle(13));
Label label4 = new Label(3, 1, "结算总额(元)",setTitle(13));
// 将定义好的单元格添加到工作表中
sheet.addCell(label0);
//合并单元格
//从col1列到col2列   从row1行到row2行
//mergeCells(int col1, int row1, int col2, int row2)
sheet.mergeCells(0, 0, 3, 0);
sheet.addCell(label1);
sheet.addCell(label2);
sheet.addCell(label3);
sheet.addCell(label4);
//循环将数据录入到excel中
for (int i = 0; i < list.size(); i++) {
DataPojo dataPojo = list.get(i);
Label lab0 = new Label(0, i + 2, dataPojo.getDataId(),cellFormat(i));
Label lab1 = new Label(1, i + 2, dataPojo.getCnName(),cellFormat(i));
Label lab2 = new Label(2, i + 2, dataPojo.getBankAccount(),cellFormat(i));
Label lab3 = new Label(3, i + 2, dataPojo.getSettleFee(),cellFormat(i));
sheet.addCell(lab0);
sheet.addCell(lab1);
sheet.addCell(lab2);
sheet.addCell(lab3);
//sheet.setRowView(i + 2, 1000);//设置高度 一般都不用设置高度
//设置宽度 第一个参数是第几列 第二个参数是宽度
sheet.setColumnView(0, 20);
sheet.setColumnView(1, 25);
sheet.setColumnView(2, 20);
sheet.setColumnView(3, 20);
}
// 写入数据并关闭文件
book.write();
book.close();
System.out.println("生成的文件路径:" + file.getCanonicalPath());
// System.out.println(file.getAbsolutePath());
} catch (Exception e) {
System.out.println(e);
}
} /**
 * 设置背景色
 * 
 * @param point
 * @return
 * @throws WriteException
 */
public static  WritableCellFormat cellFormat(int dataId) throws WriteException {
Colour color = null;
if (dataId%2==0) { 
color = Colour.WHITE;
} else {
color = Colour.ROSE;
}
// 设置字体颜色
WritableFont font = new jxl.write.WritableFont(WritableFont.ARIAL, 10,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);
WritableCellFormat cellFormat = new jxl.write.WritableCellFormat(font);
// 设置单元格背景颜色
cellFormat.setBackground(color);
cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN); // 增加边框
 
return cellFormat;
}
public static WritableCellFormat setTitle(int fontSize) throws WriteException {
Colour color =  Colour.GRAY_25;  
// 设置字体颜色
WritableFont font = new jxl.write.WritableFont(WritableFont.ARIAL, fontSize,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);
WritableCellFormat cellFormat = new jxl.write.WritableCellFormat(font);
// 设置单元格背景颜色
cellFormat.setBackground(color);
// cellFormat.setShrinkToFit(true);
cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN); // 增加边框
return cellFormat;
}}更新的很简单import java.io.*;
import jxl.*;
import jxl.write.*;public class UpdateExcel {
public static void main(String args[]) {
updateExcel();
} public static void updateExcel() {
try {
// excel获得文件
Workbook wb = Workbook.getWorkbook(new File("测试.xls"));
// 打开一个文件的副本,并且指定数据写回到原文件
WritableWorkbook book = Workbook.createWorkbook(new File("测试.xls"),
wb);
// 添加一个工作表
WritableSheet sheet = book.createSheet("第二页", 1);
sheet.addCell(new Label(0, 0, "第二页的测试数据"));
book.write();
book.close();
} catch (Exception e) {
System.out.println(e);
}
}
}