java - 为什么不能输出全部数据
本文介绍了java - 为什么不能输出全部数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
为什么不能输出所有数据?
package com.demon.util;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import com.demon.bean.Book;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class ExcelUtil {
public static void excelOut(ArrayList ar, String str) {
WritableWorkbook book = null;
try {
book = Workbook.createWorkbook(new File(str));
WritableSheet sheet = book.createSheet("sheet", 0);
for (int i = 0; i < ar.size(); i++) {
Object ob = ar.get(i);
Class cl = ob.getClass();
Field[] fi = cl.getDeclaredFields();
for (int j = 0; j < fi.length; j++) {
fi[j].setAccessible(true);
Label la = new Label(j, i, String.valueOf(fi[j].get(ob)));
sheet.addCell(la);
}
book.write();
}
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
book.close();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ArrayList<Book> ar=new ArrayList<Book>();
Book bo=new Book();
bo.setId(1);
bo.setName("月子");
bo.setType("生活");
Book bo2=new Book();
bo2.setId(2);
bo2.setName("日子");
bo2.setType("生活");
ar.add(bo);
ar.add(bo2);
ExcelUtil.excelOut(ar, "/Users/piepie/Desktop/boos.xls");
}
}
解决方案
得到正解,需要将 book.write(); 放入下一级大括号中
这篇关于java - 为什么不能输出全部数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文