/** * Creates a Table object based on this TableAttributes object. * * @return a com.lowagie.text.Table object * @throws BadElementException */ public Table createTable() throws BadElementException { if (content.isEmpty()) throw new BadElementException("Trying to create a table without rows."); SimpleCell row = (SimpleCell) content.get(0); SimpleCell cell; int columns = 0; for (Iterator i = row.getContent().iterator(); i.hasNext(); ) { cell = (SimpleCell) i.next(); columns += cell.getColspan(); } float[] widths = new float[columns]; float[] widthpercentages = new float[columns]; Table table = new Table(columns); table.setAlignment(alignment); table.setSpacing(cellspacing); table.setPadding(cellpadding); table.cloneNonPositionParameters(this); int pos; for (Iterator rows = content.iterator(); rows.hasNext(); ) { row = (SimpleCell) rows.next(); pos = 0; for (Iterator cells = row.getContent().iterator(); cells.hasNext(); ) { cell = (SimpleCell) cells.next(); table.addCell(cell.createCell(row)); if (cell.getColspan() == 1) { if (cell.getWidth() > 0) widths[pos] = cell.getWidth(); if (cell.getWidthpercentage() > 0) widthpercentages[pos] = cell.getWidthpercentage(); } pos += cell.getColspan(); } } float sumWidths = 0f; for (int i = 0; i < columns; i++) { if (widths[i] == 0) { sumWidths = 0; break; } sumWidths += widths[i]; } if (sumWidths > 0) { table.setWidth(sumWidths); table.setLocked(true); table.setWidths(widths); } else { for (int i = 0; i < columns; i++) { if (widthpercentages[i] == 0) { sumWidths = 0; break; } sumWidths += widthpercentages[i]; } if (sumWidths > 0) { table.setWidths(widthpercentages); } } if (width > 0) { table.setWidth(width); table.setLocked(true); } else if (widthpercentage > 0) { table.setWidth(widthpercentage); } return table; }
// int sportId = 1; public void SelPlaWD(String filePath, String fileName) { SelectPlayerDAO sd = new SelectPlayerDAO(); // ArrayList departList = new ArrayList(); ArrayList stuDepartList = new ArrayList(); ArrayList teaDepartList = new ArrayList(); // ArrayList selectSportsid=new ArrayList(); // departList=sd.selectDepartment(sportId); // selectSportsid=sd.selectSportsid(); stuDepartList = sd.slectStuDepidBySid(sportId); teaDepartList = sd.selectDepartment(sportId); Document document = new Document(PageSize.A4); try { RtfWriter2.getInstance(document, new FileOutputStream(filePath + fileName)); document.open(); for (int i = 0; i < stuDepartList.size(); i++) { int id = Integer.parseInt(stuDepartList.get(i).toString()); // int sid = Integer.parseInt(selectSportsid.get(i).toString()); ArrayList playBoy = new ArrayList(); ArrayList playGirl = new ArrayList(); // ArrayList playTeacher = new ArrayList(); // int type=sd.selectDepartmentType(id); // if(type==1){ // 查询学生男子组的姓名和号码 playBoy = sd.selectPlayersByDept(1, id, sportId); String groupname = "男子组"; String departname = sd.selectDepartmentName(id); Paragraph p = new Paragraph(departname, new Font(Font.BOLD, 18, Font.BOLD, new Color(0, 0, 0))); p.setAlignment(1); document.add(p); // document.add(new Paragraph(groupname)); Table table = new Table(8); Cell cc5 = new Cell(groupname); cc5.setColspan(8); cc5.setBorderWidth(0); table.addCell(cc5); // 指定表格为八列 table.setBorder(0); table.setBorderWidth(0); table.setBorderColor(Color.WHITE); table.setPadding(0); table.setSpacing(0); for (int a = 0; a < playBoy.size(); a++) { PlayerPojo pojp = (PlayerPojo) playBoy.get(a); Cell cc = new Cell(pojp.getPlayernum()); cc.setBorderWidth(0); table.addCell(cc); Cell ce = new Cell(pojp.getPlayername()); ce.setBorderWidth(0); table.addCell(ce); } // document.add(p1); document.add(table); // 查询学生女子组的姓名和号码 Table table1 = new Table(8); // 指定表格为八列 table1.setBorder(0); table1.setBorderWidth(0); table1.setBorderColor(Color.WHITE); table1.setPadding(0); table1.setSpacing(0); playGirl = sd.selectPlayersByDept(0, id, sportId); String groupname1 = "女子组"; // document.add(new Paragraph(groupname1)); Cell cc6 = new Cell(groupname1); cc6.setColspan(8); cc6.setBorderWidth(0); table1.addCell(cc6); for (int a = 0; a < playGirl.size(); a++) { PlayerPojo pojp = (PlayerPojo) playGirl.get(a); Cell cc1 = new Cell(pojp.getPlayernum()); cc1.setBorderWidth(0); table1.addCell(cc1); Cell ce1 = new Cell(pojp.getPlayername()); ce1.setBorderWidth(0); table1.addCell(ce1); } document.add(table1); document.add(new Paragraph()); document.add(new Paragraph()); // } else{ } for (int i = 0; i < teaDepartList.size(); i++) { int id = Integer.parseInt(teaDepartList.get(i).toString()); ArrayList playTeacher = sd.selectPlayersByDept1(id, sportId); String groupname = "教工组"; String departname = sd.selectDepartmentName(id); Paragraph p = new Paragraph(departname, new Font(Font.BOLD, 18, Font.BOLD, new Color(0, 0, 0))); p.setAlignment(1); document.add(p); // document.add(new Paragraph(groupname)); Table table2 = new Table(8); Cell cc6 = new Cell(groupname); cc6.setColspan(8); cc6.setBorderWidth(0); table2.addCell(cc6); // 指定表格为八列 table2.setBorder(0); table2.setBorderWidth(0); table2.setBorderColor(Color.WHITE); table2.setPadding(0); table2.setSpacing(0); for (int a = 0; a < playTeacher.size(); a++) { PlayerPojo pojp = (PlayerPojo) playTeacher.get(a); Cell cc = new Cell(pojp.getPlayernum()); cc.setBorderWidth(0); table2.addCell(cc); Cell ce = new Cell(pojp.getPlayername()); ce.setBorderWidth(0); table2.addCell(ce); } document.add(table2); } // 查询教工组的姓名和号码 // document.add(p1); document.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }