博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JExcelApi
阅读量:4051 次
发布时间:2019-05-25

本文共 4615 字,大约阅读时间需要 15 分钟。

-- Start

Java Excel API 简介

Java Excel API 是一个用来读写 excel 的开源框架。

 

下载 Java Excel API

你可以在 Google 中搜索 JExcelApi download,然后到官网去下载最新的 JExcelApi 包。然后把 jxl.jar 添加到自己的 classpath 中就可以了。

 

一个简单的例子

import java.io.File;import java.io.Serializable;import java.util.ArrayList;import java.util.Date;import java.util.GregorianCalendar;import java.util.List;import jxl.Workbook;import jxl.format.Border;import jxl.format.BorderLineStyle;import jxl.write.DateFormat;import jxl.write.DateTime;import jxl.write.Label;import jxl.write.WritableCellFormat;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;import jxl.write.WriteException;public class Test {	private static final int ID_INDEX = 0;	private static final int NAME_INDEX = 1;	private static final int BIRTHDAY_INDEX = 2;	private static final int SALARY_INDEX = 3;	private static final int ADDRESS_INDEX = 4;		private WritableCellFormat format;	private WritableCellFormat dateFormat;		public Test() {		format = new WritableCellFormat(WritableWorkbook.ARIAL_10_PT);		try {			format.setWrap(true);			format.setBorder(Border.ALL, BorderLineStyle.THIN);						dateFormat = new WritableCellFormat(new DateFormat("yyyy-MM-dd"));			dateFormat.setBorder(Border.ALL, BorderLineStyle.THIN);		} catch (WriteException e) {			e.printStackTrace();		}	}		public static void main(String[] args) throws Exception {		new Test().generateReport();	}		public void generateReport() throws Exception {		// 打开 workbook		WritableWorkbook workbook = Workbook.createWorkbook(new File("./test.xls"));				// 创建 Sheet		WritableSheet reportSheet = workbook.createSheet("report", 0);				// 设置列宽		setColumnWidth(reportSheet);				// 设置标题		int row = 0;		setTitle(reportSheet, row++);				// 设置每行		List
employees = getDataFromDB(); for (Employee employee : employees) { setRow(reportSheet, row++, employee); } // 关闭 workbook workbook.write(); workbook.close(); } private void setColumnWidth(WritableSheet sheet) { sheet.setColumnView(ID_INDEX, 20); sheet.setColumnView(NAME_INDEX, 20); sheet.setColumnView(BIRTHDAY_INDEX, 10); sheet.setColumnView(SALARY_INDEX, 10); sheet.setColumnView(ADDRESS_INDEX, 40); } private void setTitle(WritableSheet sheet, int row) throws Exception { sheet.addCell(new Label(ID_INDEX, row, "员工号", format)); sheet.addCell(new Label(NAME_INDEX, row, "姓名", format)); sheet.addCell(new Label(BIRTHDAY_INDEX, row, "出生年月日", format)); sheet.addCell(new Label(SALARY_INDEX, row, "工资", format)); sheet.addCell(new Label(ADDRESS_INDEX, row, "地址", format)); } private void setRow(WritableSheet sheet, int row, Employee employee) throws Exception { sheet.addCell(new jxl.write.Number(ID_INDEX, row, employee.getID(), format)); sheet.addCell(new Label(NAME_INDEX, row, employee.getName(), format)); sheet.addCell(new DateTime(BIRTHDAY_INDEX, row, employee.getBirthday(), dateFormat)); sheet.addCell(new jxl.write.Number(SALARY_INDEX, row, employee.getSalary(), format)); sheet.addCell(new Label(ADDRESS_INDEX, row, employee.getAddress(), format)); } private List
getDataFromDB () { List
r = new ArrayList
(); r.add(new Employee(1, "张三", new GregorianCalendar(1979, 11, 24).getTime(), Double.valueOf(1234.56), "辽宁大连")); r.add(new Employee(2, "李四", new GregorianCalendar(1980, 4, 4).getTime(), Double.valueOf(4321.65), "内蒙古鄂尔多斯")); return r; }}// Javabeanclass Employee implements Serializable { private static final long serialVersionUID = 7445838103191670245L; private Integer ID; private String name; private Date birthday; private Double salary; private String address; public Employee() { } public Employee(Integer ID, String name, Date birthday, Double salary, String address) { this.ID = ID; this.name = name; this.birthday = (birthday == null ? null : (Date) birthday.clone()); this.salary = salary; this.address = address; } public Integer getID() { return ID; } public void setID(Integer iD) { ID = iD; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday == null ? null : (Date) birthday.clone(); } public void setBirthday(Date birthday) { this.birthday = (birthday == null ? null : (Date) birthday.clone()); } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }}

 

更多例子

Java Excel API 还有好多其他的功能,在它发行包的的 src 文件夹下可以找到更多的例子。

 

-- -- 声 明:转载请注明出处

-- Last Updated on 2012-08-16
-- Written by ShangBo on 2012-07-22
-- End

 

你可能感兴趣的文章
C#中枚举类型在switch语句中值对照问题
查看>>
GridView中根据特殊标记设置不可编辑
查看>>
PowerDesinger中生成数据库时将域的内容转化为实际的字段
查看>>
通过模板将GridView导出为Excel
查看>>
迭代开发过程及一些原则
查看>>
Windows IIS服务器CA认证安装
查看>>
Asp.net 中Excel通过模板导出中发布问题
查看>>
64位机器配置CA认证一些小问题
查看>>
SVN自动完全备份
查看>>
开发中的Warning原来也很有用
查看>>
Silverlight Toolkit例子代码中缺少System.Windows.Controls.Samples.Common.dll的解决办法
查看>>
项目经理培训后的一点感概
查看>>
vc学习之窗口大小发生变更时使控件自动摆放到合适的位置
查看>>
Oracle 获取系统日期时间,导出数据库
查看>>
黄巢的菊花,非常喜欢,贴出来共享一下
查看>>
在光纤环网中的b/s与c/s的比对
查看>>
根据程序名称和程序路径判断程序是否启动
查看>>
史记翻译-汉初三杰-萧何-萧相国世家第二十三
查看>>
史记翻译-汉初三杰-张良-留侯世家第二十五
查看>>
史记翻译-汉初三杰-韩信-淮阴侯列传第三十二
查看>>