有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ArrayList和itextpdf,表

我有一个30行的表和一个充满字符串的数组列表。表有两列,第一列类似于公司名称,第二列我需要从列表中写入字符串,第二列的每一行应该有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) 个答案