public class Factory implements Serializable {需要写入到xls文件中的东东:
private String id;
private String fullName;
private String typeName;
private String name;
private String contractor;
private String phone;
private String mobile;
private String fax;
private String inspector;
private String ctype;
private String state;
private Integer orderNo;
private String createBy;
private Date createTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContractor() {
return contractor;
}
public void setContractor(String contractor) {
this.contractor = contractor;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getFax() {
return fax;
}
public void setFax(String fax) {
this.fax = fax;
}
public String getInspector() {
return inspector;
}
public void setInspector(String inspector) {
this.inspector = inspector;
}
public String getCtype() {
return ctype;
}
public void setCtype(String ctype) {
this.ctype = ctype;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Integer getOrderNo() {
return orderNo;
}
public void setOrderNo(Integer orderNo) {
this.orderNo = orderNo;
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
}
public static Map<String,String> maps=new LinkedHashMap<String,String>();实现注入的代码:没有加样式,到时候有模板的:
static{
maps.put("id", "厂家编号");
maps.put("fullName", "厂家全称");
maps.put("name", "名称缩写");
maps.put("contractor", "联系人");
maps.put("phone", "电话");
maps.put("mobile", "手机");
maps.put("fax", " 真");
maps.put("inspector", "验货员");
maps.put("state", "状态");
maps.put("typeName", "类型");
}
public Field[] getFieldsByName(Map<String,String> maps) throws Exception, NoSuchFieldException{
Field[] fields=new Field[maps.size()];
Set<Entry<String,String>> entrySet = maps.entrySet();
int i=0;
for (Entry<String, String> entry : entrySet) {
fields[i]=Factory.class.getDeclaredField(entry.getKey());
fields[i].setAccessible(true);
i++;
}
return fields;//需要 射的字段;
}
@Test
public void test() throws Exception, Exception{
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
FactoryDao daoFactory = (FactoryDao) context.getBean("daoFactory");
List<Factory> list = daoFactory.find("from Factory o");
//创建表和工作sheet;
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
Field[] fieldsByName = getFieldsByName(POIUtils.maps);
System.out.println(fieldsByName.length);
//第一行存放名称;
HSSFRow nameRow = sheet.createRow(0);
short cellNo=0;
for(Map.Entry<String, String> me:POIUtils.maps.entrySet()){
nameRow.createCell(cellNo++).setCellValue(me.getValue());
//上面的是设置 题;
}
int rowNum=1;
for (Factory factory : list) {
HSSFRow createRow = sheet.createRow(rowNum++);//每次都得+1;
short column=0;
for (Field field : fieldsByName) {
HSSFCell cell = createRow.createCell(column++);//这个也必须得每次都 一
Object fieldValue = field.get(factory);
cell.setCellValue(fieldValue==null? " ":fieldValue.toString());
}
}
OutputStream fos = new FileOutputStream("D:/factory.xls");
wb.write(fos);
fos.flush();
fos.close();
}
<script type="text/javascript" id="wumiiRelatedItems"> </script>