生成EXCEL小实例(JXL API) 比较老的API 实用小功能
java 导出EXCEL JXL是比较老的导出EXCEL文件的API了 其依赖的JAR包 :
导出实例代码:
public exportExcel() throws Exception {
try {
File f = new File("D:\\AAA.xls");
if (f.exists()) {
f.delete();
f.createNewFile();
}
WritableWorkbook wwb = Workbook.createWorkbook(f); // 建立excel文件
WritableSheet ws = wwb.createSheet("Sheet1", 10);// 创建一个工作表
// 设置单元格的文字格式
WritableFont wf = new WritableFont(WritableFont.ARIAL, 12,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setVerticalAlignment(VerticalAlignment.BOTTOM);
wcf.setAlignment(jxl.format.Alignment.CENTRE);
// 行与列的索引都是从 0 开始
ws.setRowView(0, 500); // 行高度
ws.setColumnView(0, 20);// 列宽度
ws.setColumnView(1, 20);// 列宽度
ws.setColumnView(2, 20);// 列宽度
// 标题头
Label l1 = new Label(0, 0, "T1", wcf);
Label l2 = new Label(1, 0, "T2", wcf);
Label l3 = new Label(2, 0, "T3", wcf);
//对象填充
ws.addCell(l1);
ws.addCell(l2);
ws.addCell(l3);
// 准备excel数据
Label Ltemp = null;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3; j++) {
Ltemp = new Label(j, i, "cell" + i, wcf); // 列值, 行值, 内容,配置对象
ws.addCell(Ltemp);
}
}
wwb.write();
wwb.close();
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e1) {
e1.printStackTrace();
} catch (WriteException e2) {
e2.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
try {
exportExcel ee = new exportExcel();
} catch (Exception e) {
e.printStackTrace();
}
}
如有疑问 请留言 欢迎提供建议
评论已有 0 条