java Excel 操作

try {
// 1.1 创建可写入的 Excel工作薄
WritableWorkbook writableWorkbook = Workbook
.createWorkbook(new File("test.xls"));
// 1.2 创建Excel工作表
WritableSheet writableSheet = writableWorkbook.createSheet(
"sheet1", 0);

// 2.1 添加Label对象
Label label00 = new Label(0, 0, "测试创建普通单元格label00");
writableSheet.addCell(label00);

// 2.2 添加带有字体和边框的Formatting的对象
// 2.2.1 设置字体的大小、颜色等样式
WritableFont writableFont = new WritableFont(WritableFont.TIMES,
10, WritableFont.BOLD, true);
WritableCellFormat writableCellFormat = new WritableCellFormat(
writableFont);
// 2.2.2 设置单元格的背景颜色
writableCellFormat.setBackground(jxl.format.Colour.GRAY_50);
// 2.2.3 设置单元格的边框
writableCellFormat.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.THICK, jxl.format.Colour.RED);
// 2.2.3 设置单元格中元素的对齐方式
writableCellFormat.setAlignment(Alignment.RIGHT);
// 2.2.4 添加Label对象
Label label01 = new Label(0, 1, "TIMES", writableCellFormat);
writableSheet.addCell(label01);
// 2.3.1 添加Number对象
jxl.write.Number label02 = new jxl.write.Number(0, 2, 3.1415926);
writableSheet.addCell(label02);
// 2.3.2 添加带有formatting的 Number对象
NumberFormat numberFormat = new NumberFormat("#.##");
WritableCellFormat writableCellFormat4NumBer = new WritableCellFormat(
numberFormat);
jxl.write.Number label12 = new jxl.write.Number(1, 2, 3.1415926,
writableCellFormat4NumBer);
writableSheet.addCell(label12);
// 2.4 添加Boolean对象
jxl.write.Boolean label03 = new jxl.write.Boolean(0, 3, false);
jxl.write.Boolean label13 = new jxl.write.Boolean(1, 3, true);
writableSheet.addCell(label03);
writableSheet.addCell(label13);
// 2.5.1 添加DateTime对象
DateTime label04 = new DateTime(0, 4, new java.util.Date());
writableSheet.addCell(label04);
// 2.5.2 添加带有formatting的DateFormat对象
DateFormat dateFormat = new DateFormat("dd MM yyyy hh:mm:ss");
WritableCellFormat writableCellFormat4DateTime = new WritableCellFormat(
dateFormat);
DateTime label05 = new DateTime(1, 4, new java.util.Date(),
writableCellFormat4DateTime);
writableSheet.addCell(label05);
// 2.6 添加WritableHyperlink对象
WritableHyperlink writableHyperlink = new WritableHyperlink(0, 5,
new URL("http://my.oschina.net/wbw/blog"));
writableSheet.addHyperlink(writableHyperlink);
// 2.7 添加Formual对象
Formula formula = new Formula(0, 6, "CONCATENATE(A1,A2,A3)");
writableSheet.addCell(formula);

// 3.1 写入Exel工作表
writableWorkbook.write();

// 4.1 关闭Excel工作薄对象
writableWorkbook.close();
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}