Ver Mensaje Individual
  #2  
Antiguo 10-10-2006
Avatar de epuigdef
epuigdef epuigdef is offline
Miembro
 
Registrado: jul 2005
Posts: 196
Reputación: 19
epuigdef Va por buen camino
Ahí va:

Este código genera una tabla java con los resultados de una sentencia SQL:


Código:
privatevoid generarPDF2(ConsultasPortletSessionBean b, String nombre, String path) {
try {
GestorPropiedades gp = GestorPropiedades.getInstance("sql");
Connection conexion = gp.obtenerConexion("sql");
Statement mandato = conexion.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = mandato.executeQuery(b.getSentencia());
 
Document doc = new Document(PageSize.A4.rotate());
PdfWriter.getInstance(doc, new FileOutputStream(path + nombre));
 
int total = 0;
for(int i = 1;i<=rs.getMetaData().getColumnCount();i++)
total += rs.getMetaData().getColumnDisplaySize(i);
 
float[] widths = newfloat[rs.getMetaData().getColumnCount()];
 
 
for(int i = 1;i<=rs.getMetaData().getColumnCount();i++) {
widths[i-1] = 100*rs.getMetaData().getColumnDisplaySize(i) / total;
 
}
 
doc.open();
Font f = new Font(Font.HELVETICA, 18);
Paragraph par = new Paragraph(b.getConsultaCargada().getTitulo()+"\r\n\r\n", f);
 
doc.add(par);
 
 
PdfPTable table = new PdfPTable(rs.getMetaData().getColumnCount());
table.setWidths(widths);
for(short i = 1;i<=rs.getMetaData().getColumnCount();i++) {
table.addCell(rs.getMetaData().getColumnLabel(i));
}
 
 
 
while (rs.next()) {
for(short i = 1;i<=rs.getMetaData().getColumnCount();i++) {
table.addCell(rs.getString(i));
}
}
table.setWidthPercentage(100);
doc.add(table);
doc.close();
}
catch(Exception e) {
System.err.println(e.getMessage());
}
}

Un saludo

Edu
Responder Con Cita