假设我们定义一个POJO如下
package com.unity.excel.demo;
import java.util.Date;
import com.unity.excel.annotations.UColumn;
import com.unity.excel.annotations.UFormatter;
import com.unity.excel.annotations.UTable;
@UTable()
public class DemoObject {
@UColumn(Index = 1)
private String name;
@UColumn(Index = 2)
private int age;
@UColumn(Index = 3)
private double width;
@UColumn(Index = 4)
@UFormatter(FormatPartten = "yyyy-MM-dd")
private Date birthday;
public DemoObject() {
}
public DemoObject(String name) {
super();
this.name = name;
}
public DemoObject(String name, int age) {
super();
this.name = name;
this.age = age;
}
public DemoObject(String name, int age, double width) {
super();
this.name = name;
this.age = age;
this.width = width;
}
public DemoObject(String name, int age, double width, Date birthday) {
super();
this.name = name;
this.age = age;
this.width = width;
this.birthday = birthday;
}
public int getAge() {
return age;
}
public Date getBirthday() {
return birthday;
}
public String getName() {
return name;
}
public double getWidth() {
return width;
}
public void setAge(int age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void setName(String name) {
this.name = name;
}
public void setWidth(double width) {
this.width = width;
}
@Override
public String toString() {
return "DemoObject [name=" + name + ", age=" + age + ", width=" + width
+ ", birthday=" + birthday + "]";
}
}
上面代码中,我们看到了UTable UColumn 这两个注解,很明显这是支撑我们导出使用的注解。
回顾第一章的代码
private static void ExportTest() {
List<DemoObject> temp=new ArrayList<DemoObject>();
//---------------------------------姓名 ,年龄,腰围,生日
temp.add(new DemoObject("小妹",1,33.500,new Date()));
temp.add(new DemoObject("山炮",2,23.5,new Date()));
Unity u=new Unity97();
try {
u.exportUTable(temp, new FileOutputStream("src/temp.xls")).close();
} catch (IOException e) {
e.printStackTrace();
}
}
如此就完成了导出,那么导出的数据格式是怎么控制的呢?
来介绍一下 UTable 和 UColumn这两个注解
@UTable(EnableSequence=true,SequenceHead="序号",TitleRowsCount=1)
以上属性均是默认值,等效于@UTable()
EnableSequence 开启序号列 值 true | false 默认 true
SequenceHead 序号列标题
TitleRowsCount 标题行数量 //这个是读取时使用
@UColumn(Index=1)
Index代表导出时 字段所在列的索引位置,从1开始 因为 第0列是序号列
Head代表导出时标题列的文字内容 ,默认为字段定义名字
需要第三方类库清单
unity-excel-1.0.1.jar见附件