mardi 10 mai 2016

why can't output all data

*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");

        } 

    }

Q: i have written two data then output by excel why can't output all data ,and there is no other problem。i hava searched on google for many times ,but no use. and source code has been tested on my project ,it worked. and i ompared it with mine, and there is no different between the two.





Aucun commentaire:

Enregistrer un commentaire