ArrayList和itextpdf,表

时间:2014-06-25 16:18:52

标签: java list arraylist jxl itextpdf

我有30行的表和数组列表中的字符串。表有2列,第一列是公司名称,第二列我需要从列表中编写字符串,第二列的每一行应该有10个元素,如何我这样做了吗?

PDF输出应该是这样的:

Company name |||| DESTINATION      
AirFrance |||| Tokio,London,Istanbul,New Nork,Paris,Vienna,OSLO,Belgrade,Budapest,Amsterdam.

每家公司都应从列表中读取10个目的地。

这可能很容易,我只能看到我试图计算列表元素并以某种方式对其进行分区,但它没有用。

顺便说一下,我使用itextpdf库,然后从excel中读取这些数据。

这是代码:

public class ReadExcel {
    private String inputfile;
    private List<Avio> avioni;

    public ReadExcel() {

        this.avioni = new ArrayList<Avio>();
    }


public List<Avio> getAvioni() {
    return avioni;
}

public void setAvioni(List<Avio> avioni) {
    this.avioni = avioni;
}

public String getInputfile() {
    return inputfile;
}

public void setInputfile(String inputfile) {
    this.inputfile = inputfile;
}

public void citaj(String naziv) {
    Avio a = new Avio();

    try {
        Workbook w = Workbook.getWorkbook(new File(naziv));
        Sheet sheet = w.getSheet(0);
        // System.out.println(sheet.getRows());
        for (int i = 1; i < sheet.getRows(); i++) {
            Avio tmpAvio = new Avio();

            tmpAvio.setDrzava(sheet.getCell(0, i).getContents());
            System.out.println();
            tmpAvio.setNazivKomp(sheet.getCell(1, i).getContents());
            tmpAvio.setVlasnik(sheet.getCell(2, i).getContents());
            tmpAvio.setBrAviona(Integer.parseInt(sheet.getCell(3, i)
                    .getContents()));
            tmpAvio.setGod(Integer.parseInt(sheet.getCell(4, i)
                    .getContents()));

            tmpAvio.setDatum(sheet.getCell(6,i).getContents());
            tmpAvio.setModelAviona(sheet.getCell(7,i).getContents());
            tmpAvio.setKapetan(sheet.getCell(8,i).getContents());
            tmpAvio.setJMBG(sheet.getCell(9,i).getContents());
            tmpAvio.setIskustvo(sheet.getCell(10,i).getContents());
            tmpAvio.setBrPutnika(sheet.getCell(11,i).getContents());
            tmpAvio.setLet(sheet.getCell(12,i).getContents());

            avioni.add(tmpAvio);


        }

        Sheet sheet1 = w.getSheet(1);

        List<String> destinacije = new ArrayList<String>();
        for (int i = 0; i < sheet1.getRows(); i++) {
            for (int j = 0; j < sheet1.getColumns(); j++) {

                destinacije.add(sheet1.getCell(j, i).getContents());
                System.out.println(sheet1.getCell(j,i).getContents()+i+","+j);
            }
            avioni.get(i).setDestinacije(destinacije);
            System.out.println(avioni.get(i));
        }


    }

    catch (BiffException e1) {

        e1.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }


}

public void kreirajPDF(String naziv) {

    Document d = new Document();
    try {
        PdfWriter.getInstance(d, new FileOutputStream(new File(naziv)));
        d.open();

        File fontFile = new File("swansea.ttf");
        BaseFont unicode = BaseFont.createFont(fontFile.getAbsolutePath(),
                BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        Font titleFont = new Font(unicode, 16, Font.BOLD);
        titleFont.setColor(BaseColor.WHITE);
        Font textFont = new Font(unicode, 11, Font.NORMAL);
        Font headerFont = new Font(unicode, 14, Font.BOLD);

        Paragraph p1 = new Paragraph("Evropske Avio Kompanije", titleFont);
        p1.setAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
        Image img = Image.getInstance("lufthansa2.jpg");
        img.setAlignment(Image.MIDDLE |Image.TEXTWRAP);
        p1.add(img);
        d.add(p1);
        //d.add(new Paragraph("DESTINACIJE"));


        Paragraph p2 = new Paragraph("Destinacije", titleFont);
        p2.setAlignment(com.itextpdf.text.Element.ALIGN_CENTER);            
        d.add(p2);
        d.add(new Paragraph(" "));

        Paragraph p3 = new Paragraph("Piloti", titleFont);
        p3.setAlignment(com.itextpdf.text.Element.ALIGN_CENTER);            
        d.add(p3);

        kreiranjeTabele(d, textFont, headerFont, "Pilot");
        d.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private void kreiranjeTabele(Document d, Font textFont, Font headerFont,
        String smjer) throws DocumentException {
    PdfPTable table = new PdfPTable(5);

    PdfPCell c1 = new PdfPCell(new Phrase("Drzava", headerFont));
    table.addCell(c1);

    PdfPCell c2 = new PdfPCell(new Phrase("Kompanija", headerFont));
    table.addCell(c2);

    PdfPCell c3 = new PdfPCell(new Phrase("Vlasnik", headerFont));
    table.addCell(c3);

    PdfPCell c4 = new PdfPCell(new Phrase("Broj Aviona", headerFont));
    table.addCell(c4);

    PdfPCell c5 = new PdfPCell(new Phrase("Godina", headerFont));
    table.addCell(c5);

    for (Avio s : avioni) {
        table.setWidthPercentage(110);

        // Paragraph p1 = new Paragraph(s.toString(),
        // textFont);Integer.toString(i)
        table.addCell(new Phrase(s.getDrzava(), textFont));
        table.addCell(new Phrase(s.getNazivKomp(), textFont));
        table.addCell(new Phrase(s.getVlasnik(), textFont));
        table.addCell(new Phrase(Integer.toString(s.getBrAviona()),
                textFont));
        table.addCell(new Phrase(Integer.toString(s.getGod()), textFont));

    }




    PdfPTable table1 = new PdfPTable(5);
    PdfPCell c11 = new PdfPCell(new Phrase("Kompanija", headerFont));
    table1.addCell(c11);

    PdfPCell c22 = new PdfPCell(new Phrase("Avion", headerFont));
    table1.addCell(c22);

    PdfPCell c33 = new PdfPCell(new Phrase("Datum", headerFont));
    table1.addCell(c33);

    PdfPCell c44 = new PdfPCell(new Phrase("Destinacija", headerFont));
    table1.addCell(c44);

    PdfPCell c55 = new PdfPCell(new Phrase("Trajanje leta", headerFont));
    table1.addCell(c55);
    for (Avio s1 : avioni) {
        table1.setWidthPercentage(110);
        table1.addCell(new Phrase(s1.getNazivKomp(), textFont));
        table1.addCell(new Phrase(s1.getModelAviona(), textFont));
        table1.addCell(new Phrase(s1.getDatum(), textFont));

    //  table1.addCell(new Phrase(s1.getDestinacije(), textFont));
        table1.addCell(new Phrase(s1.getLet(), textFont));

    }
    PdfPTable table2 = new PdfPTable(5);
    PdfPCell c111 = new PdfPCell(new Phrase("Pilot", headerFont));
    table2.addCell(c111);

    PdfPCell c222 = new PdfPCell(new Phrase("Drzava", headerFont));
    table2.addCell(c222);

    PdfPCell c333 = new PdfPCell(new Phrase("JMBG", headerFont));
    table2.addCell(c333);

    PdfPCell c444 = new PdfPCell(new Phrase("Iskustvo", headerFont));
    table2.addCell(c444);

    PdfPCell c555 = new PdfPCell(new Phrase("Avion", headerFont));
    table2.addCell(c555);
    for (Avio s2 : avioni) {
        table2.setWidthPercentage(110);
        table2.addCell(new Phrase(s2.getKapetan(), textFont));
        table2.addCell(new Phrase(s2.getDrzava(), textFont));
        table2.addCell(new Phrase(s2.getJMBG(), textFont));
        table2.addCell(new Phrase(s2.getIskustvo(), textFont));
        table2.addCell(new Phrase(s2.getModelAviona(), textFont));

    }

    d.add(table);
    d.newPage();        
    d.add(table1);
    d.newPage();        
    d.add(table2);
}

希望有人可以提供帮助。提前谢谢。

0 个答案:

没有答案