前几天无聊时http://www.oschina.net/中国开源社区里走走,发现了一款比较好用的excel操作组件,
用起来也比较简单
import java.io.*;
import jxl.*;
import jxl.write.*;
import jxl.format.*;
public class TestExcel {
public static void writeExcel(File f) throws Exception {
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(f);
jxl.write.WritableSheet ws = wwb.createSheet("TestSheet1", 0);
jxl.write.Label labelC = new jxl.write.Label(0, 0, "我爱中国");
ws.addCell(labelC);
jxl.write.WritableFont wfc = new jxl.write.WritableFont(
WritableFont.ARIAL, 20, WritableFont.BOLD, false,
UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.GREEN);
jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(
wfc);
wcfFC.setBackground(jxl.format.Colour.RED);
labelC = new jxl.write.Label(6, 0, "中国爱我a", wcfFC);
ws.addCell(labelC);
// 写入Exel工作表
wwb.write();
// 关闭Excel工作薄对象
wwb.close();
}
public static void readExcel(File os) throws Exception {
Workbook wb = Workbook.getWorkbook(os);
Sheet s = wb.getSheet("TestSheet1");
Cell c = s.getCell(6, 0);
System.out.println(c.getContents());
}
// 最好写一个这样的main方法来测试一下你的这个class是否写好了。
public static void main(String[] args) throws Exception {
File f = new File("c:\\kk1.xls");
// writeExcel(f);
// f.createNewFile();
// writeExcel(f);
readExcel(f);
}
}