Foros del Web » Programación para mayores de 30 ;) » Java »

Descargar y dar formato a un excel

Estas en el tema de Descargar y dar formato a un excel en el foro de Java en Foros del Web. Buenos dias tengo un codigo que genera un excel pero lo genera sin formato sin titulos de los campos y se genera en el servidor, ...
  #1 (permalink)  
Antiguo 30/06/2016, 09:37
 
Fecha de Ingreso: febrero-2015
Ubicación: tepoztlan
Mensajes: 72
Antigüedad: 9 años, 2 meses
Puntos: 0
Descargar y dar formato a un excel

Buenos dias tengo un codigo que genera un excel pero lo genera sin formato sin titulos de los campos y se genera en el servidor, entonces yo quiero remediar eso haciendo que tenga titulos de los campos, que tenga un buen formato de autoajustar al texto y que se descargue a la pc de quien lo genere mi codigo es asi

Código Java:
Ver original
  1. File file = new File("//home//juridico//Generado.xls");
  2.    
  3.    
  4.     public Generar_Consulta(){
  5.        
  6.          this.url = "jdbc:mysql://localhost/"+this.db;
  7.          try{
  8.              Class.forName("com.mysql.jdbc.Driver");
  9.              
  10.              
  11.              
  12.             conn = DriverManager.getConnection( this.url, this.user , this.password );
  13.          if ( conn!=null ){
  14.             System.out.println("Conexión a la base de datos "+this.db+"...... Listo ");
  15.          }
  16.       }catch(SQLException e){
  17.          System.err.println( e.getMessage() );
  18.       }catch(ClassNotFoundException e){
  19.          System.err.println( e.getMessage() );
  20.       }
  21.    
  22.     }
  23.    
  24.    
  25.     public void WriteExcel(String anhos){
  26.        
  27.         int row=0;
  28.        
  29.          WritableFont wf = new WritableFont( WritableFont.ARIAL, 12, WritableFont.NO_BOLD );
  30.         WritableCellFormat cf = new WritableCellFormat(wf);    
  31.        
  32.         //Interfaz para una hoja de cálculo
  33.         WritableSheet excelSheet = null;
  34.         WritableWorkbook workbook = null;
  35.        
  36.         //Establece la configuración regional para generar la hoja de cálculo
  37.         WorkbookSettings wbSettings = new WorkbookSettings();
  38.         wbSettings.setLocale(new Locale("en", "EN"));
  39.        
  40.         try {
  41.             workbook = Workbook.createWorkbook( file, wbSettings );
  42.             //hoja con nombre de la tabla
  43.             workbook.createSheet( "convenios", 0 );
  44.             excelSheet = workbook.getSheet(0);
  45.             System.out.println(  "creando hoja excel.....Listo"  );            
  46.         } catch (IOException ex) {
  47.             System.err.println( ex.getMessage() );
  48.         }
  49.        
  50.        
  51.         //Consulta SQL
  52.         String sql = ("select convenios.folio, convenios.contra_pa, convenios.desc_ser, convenios.total_civa, convenios.total_siva, convenios.f_ini, convenios.f_conclu, convenios.f_rec, convenios.f_vobo, convenios.f_ent_catn, convenios.reg_res, convenios.entre_a, convenios.rubx, convenios.obs, convenios.orig, convenios.anhos, res_contra.nombre, res_contra.cargo, res_contra.direccion, res_contra.tel, tipo_doc.tipo_doc, subcoord.nomb_subc, tipo_rec.tipo_recu, clasif_recu.clasi, clasif_recu.tipo_clasi, fianza.fianza, coord.nomb_coord, nacionalidad.nacionalidad from convenios join res_contra on res_contra.id_resp=convenios.id_resp join tipo_doc on tipo_doc.id_tipo_doc=convenios.id_tipo_doc join subcoord on subcoord.id_subc=convenios.id_subc join tipo_rec on tipo_rec.id_recu=convenios.id_recu join clasif_recu on clasif_recu.id_clasi=convenios.id_clasi join fianza on fianza.id_fian=convenios.id_fian join coord on coord.id_coord=convenios.id_coord join nacionalidad on nacionalidad.id_nac=convenios.id_nac where convenios.anhos = "+anhos+"");
  53.         try{
  54.             PreparedStatement pstm = conn.prepareStatement(sql);
  55.              ResultSet res = pstm.executeQuery();
  56.              System.out.println(  "obteniendo registros.....Listo"  );
  57.              
  58.               while(res.next()){
  59.                   jxl.write.Label ci = new jxl.write.Label(27, row, res.getString("anhos"), cf);
  60.                   Label folio = new Label(1, row, res.getString("folio") , cf);
  61.                   Label tipo_rec = new Label (2, row, res.getString("tipo_recu") , cf);
  62.                   Label clasi = new Label(3, row, res.getString("clasi") , cf);
  63.                   Label nomb_coord = new Label(4, row, res.getString("nomb_coord") , cf);
  64.                   Label contra_pa = new Label (5, row, res.getString("contra_pa") , cf);
  65.                   Label nomb_subc = new Label(6, row, res.getString("nomb_subc") , cf);
  66.                   Label nombre = new Label (7, row, res.getString("nombre") , cf);
  67.                   Label cargo = new Label (8, row, res.getString("cargo") , cf);
  68.                   Label direccion = new Label (9, row, res.getString("direccion") , cf);
  69.                   Label tel = new Label (10, row, res.getString("tel") , cf);
  70.                   Label tipo_doc = new Label (11, row, res.getString("tipo_doc") , cf);
  71.                   Label desc_ser = new Label (12, row, res.getString("desc_ser") , cf);
  72.                   Label total_civa = new Label (13, row, res.getString("total_civa") , cf);
  73.                   Label total_siva = new Label (14, row, res.getString("total_siva") , cf);
  74.                   Label f_ini = new Label (15, row, res.getString("f_ini") , cf);
  75.                   Label f_conclu = new Label (16, row, res.getString("f_conclu") , cf);
  76.                   Label f_rec = new Label (17, row, res.getString("f_rec") , cf);
  77.                   Label f_vobo = new Label (18, row, res.getString("f_vobo"), cf);
  78.                   Label f_ent_catn = new Label (19, row, res.getString("f_ent_catn") , cf);
  79.                   Label reg_res = new Label (20, row, res.getString("reg_res") , cf);
  80.                   Label nacionalidad = new Label(21, row, res.getString("nacionalidad") , cf);
  81.                   Label fianza = new Label (22, row, res.getString("fianza") , cf);
  82.                   Label entre_a = new Label (23, row, res.getString("entre_a"), cf);
  83.                   Label rubx = new Label(24, row, res.getString("rubx") , cf);
  84.                   Label obs = new Label(25, row, res.getString("obs") , cf);
  85.                   Label orig = new Label (26, row, res.getString("orig") , cf);
  86.                  
  87.                   row++;
  88.                  
  89.                   try{
  90.                      
  91.                       excelSheet.addCell(ci);
  92.                       excelSheet.addCell(folio);
  93.                       excelSheet.addCell(tipo_rec);
  94.                       excelSheet.addCell(clasi);
  95.                       excelSheet.addCell(nomb_coord);
  96.                       excelSheet.addCell(contra_pa);
  97.                       excelSheet.addCell(nomb_subc);
  98.                       excelSheet.addCell(nombre);
  99.                       excelSheet.addCell(cargo);
  100.                       excelSheet.addCell(direccion);
  101.                       excelSheet.addCell(tel);
  102.                       excelSheet.addCell(tipo_doc);
  103.                       excelSheet.addCell(desc_ser);
  104.                       excelSheet.addCell(total_civa);
  105.                       excelSheet.addCell(total_siva);
  106.                       excelSheet.addCell(f_ini);
  107.                       excelSheet.addCell(f_conclu);
  108.                       excelSheet.addCell(f_rec);
  109.                       excelSheet.addCell(f_vobo);
  110.                       excelSheet.addCell(f_ent_catn);
  111.                       excelSheet.addCell(reg_res);
  112.                       excelSheet.addCell(nacionalidad);
  113.                       excelSheet.addCell(fianza);
  114.                       excelSheet.addCell(entre_a);
  115.                       excelSheet.addCell(rubx);
  116.                       excelSheet.addCell(obs);
  117.                       excelSheet.addCell(orig);
  118.                      
  119.                   }
  120.                    catch (WriteException ex) {
  121.                      System.err.println(  ex.getMessage() );
  122.                  }
  123.               }
  124.              res.close();        
  125.          }catch( SQLException e ){
  126.             System.err.println( e.getMessage() );
  127.         }
  128.          
  129.         //Escribe el archivo excel en disco
  130.         try {
  131.             workbook.write();
  132.             workbook.close();
  133.             System.out.println(  "Escribiendo en disco....Listo"  );        
  134.         } catch (IOException ex) {
  135.             System.err.println(  ex.getMessage() );
  136.         }
  137.         catch (WriteException ex) {
  138.            System.err.println(  ex.getMessage() );
  139.         }
  140.        
  141.         System.out.println(  "Proceso completado...."  );
  142.     }
__________________
"Las piedras me las quito caminando y las pedradas tambien"

Etiquetas: campo, excel, formato, mysql, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 01:21.