Example usage for org.apache.poi.hssf.usermodel HSSFWorkbook write

List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook write

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFWorkbook write.

Prototype

private void write(POIFSFileSystem fs) throws IOException 

Source Link

Document

Writes the workbook out to a brand new, empty POIFS

Usage

From source file:action.CustomerReciptAction.java

public String customerRecipt() throws IOException {

    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();

    HttpSession session = request.getSession();
    CustomerOrder customerOrder = (CustomerOrder) request.getSession().getAttribute("orderRecordMap");
    Customer customer = (Customer) request.getSession().getAttribute("customerMap");
    List<ProductOrder> orderedProducts = (List<ProductOrder>) request.getSession()
            .getAttribute("orderedProductsMap");
    List<Product> products = (List<Product>) request.getSession().getAttribute("productsMap");

    HSSFWorkbook workBook = new HSSFWorkbook();
    SalesReport salesReport = new SalesReport();
    /** Date Time Format for Time Stamp */
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HH_mm");
    String dateFormat = simpleDateFormat.format(new Date());
    /**File Name for XLS File */
    String fileName = "Recipt_" + dateFormat;

    salesReport.customerReport(customerOrder, customer, orderedProducts, products, workBook, request);
    response.setContentType("application/vnd.ms-excel");
    /** Change attachment into inline, inorder to open the excel file in Readonly mode */
    response.setHeader("Content-disposition", "inline;filename=" + fileName + ".xls");

    workBook.write(response.getOutputStream());
    response.getOutputStream().close();/*from   w ww  . j a v  a  2  s.  c o  m*/
    return SUCCESS;
}

From source file:Adicionales.Abrir_xml.java

private boolean guardarArcXls(String strNomArc, List<String> archivos) {
    boolean blnRes = true;

    try {//from w  ww  .  j  av  a2 s . c  om
        // Se crea un nuevo libro de trabajo de EXCEL
        HSSFWorkbook workbook = new HSSFWorkbook();
        for (String archivo_n : archivos) {
            //cargamos el contenido del n-simo contenido
            File fileXML = new File(archivo_n);
            // Definimos una nueva instancia de un API para generar arbol XML
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            // Definimos nuevo arbol XML
            DocumentBuilder builder = factory.newDocumentBuilder();
            // Representamos el archivo XML especificado en el arbol
            Document document = builder.parse(fileXML);
            //System.out.println("etiquetar archivo "+archivos.indexOf(archivo_n)+" de "+nombres.size());
            ExportarEtiquetasXml(workbook, document, nombres.get(archivos.indexOf(archivo_n)));

        }
        try (FileOutputStream output = new FileOutputStream(new File(strNomArc))) {
            workbook.write(output);
        }
        Desktop.getDesktop().open(new File(strNomArc));

    } catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
    } catch (IOException e) {
        System.out.println(e.getMessage());
    } catch (ParserConfigurationException e) {
        System.out.println("ParserConfigurationException " + e.getMessage());
    } catch (SAXException e) {
        System.out.println("SAXException " + e.getMessage());
    }

    return blnRes;
}

From source file:Almacen.Conciliacion.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
     // TODO add your handling code here:
     try {/*from   w  w  w.  j  a v a2  s.co m*/
         javax.swing.JFileChooser archivo = new javax.swing.JFileChooser();
         archivo.setFileFilter(new ExtensionFileFilter("Excel document (*.xls)", new String[] { "xls" }));
         String ruta = null;

         if (archivo.showSaveDialog(null) == archivo.APPROVE_OPTION) {
             ruta = archivo.getSelectedFile().getAbsolutePath();
             if (ruta != null) {
                 File archivoXLS = new File(ruta + ".xls");
                 Session session = HibernateUtil.getSessionFactory().openSession();
                 ArrayList datos = new ArrayList();
                 Query query = session.createSQLQuery(
                         "select compania.nombre, orden.tipo_nombre, orden.modelo, orden.no_serie, clientes.nombre as nombres,orden.id_orden \n"
                                 + "from orden inner join compania on compania.id_compania=orden.id_compania inner join clientes on clientes.id_clientes=orden.id_cliente\n"
                                 + "where orden.id_orden=" + Integer.parseInt(orden) + "");
                 query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
                 datos = (ArrayList) query.list();

                 //
                 Path FROM = Paths.get("imagenes/plantillaConciliacion.xls");
                 Path TO = Paths.get(ruta + ".xls");
                 //sobreescribir el fichero de destino, si existe, y copiar
                 // los atributos, incluyendo los permisos rwx
                 CopyOption[] options = new CopyOption[] { StandardCopyOption.REPLACE_EXISTING,
                         StandardCopyOption.COPY_ATTRIBUTES };
                 Files.copy(FROM, TO, options);

                 FileInputStream miPlantilla = new FileInputStream(archivoXLS);
                 POIFSFileSystem fsFileSystem = new POIFSFileSystem(miPlantilla);
                 HSSFWorkbook libro = new HSSFWorkbook(fsFileSystem);
                 libro.getSheet("Conciliacion").getRow(0).getCell(6)
                         .setCellValue("CONCILIACIN PARA FACTURACIN");

                 for (int i = 0; i < datos.size(); i++) {
                     java.util.HashMap map = (java.util.HashMap) datos.get(i);

                     libro.getSheet("Conciliacion").getRow(1).getCell(2)
                             .setCellValue(map.get("nombre").toString());
                     libro.getSheet("Conciliacion").getRow(2).getCell(2)
                             .setCellValue(map.get("tipo_nombre").toString());
                     libro.getSheet("Conciliacion").getRow(3).getCell(2)
                             .setCellValue(map.get("modelo").toString());
                     libro.getSheet("Conciliacion").getRow(4).getCell(2)
                             .setCellValue(map.get("no_serie").toString());
                     libro.getSheet("Conciliacion").getRow(5).getCell(2)
                             .setCellValue(map.get("nombres").toString());
                     libro.getSheet("Conciliacion").getRow(2).getCell(12)
                             .setCellValue(map.get("id_orden").toString());
                 }
                 HSSFCellStyle borde_d = libro.createCellStyle();
                 borde_d.setBorderBottom(CellStyle.BORDER_THIN);
                 borde_d.setBorderTop(CellStyle.BORDER_THIN);
                 borde_d.setBorderRight(CellStyle.BORDER_THIN);
                 borde_d.setBorderLeft(CellStyle.BORDER_THIN);
                 borde_d.setAlignment(CellStyle.ALIGN_RIGHT);

                 HSSFCellStyle borde_i = libro.createCellStyle();
                 borde_i.setBorderBottom(CellStyle.BORDER_THIN);
                 borde_i.setBorderTop(CellStyle.BORDER_THIN);
                 borde_i.setBorderRight(CellStyle.BORDER_THIN);
                 borde_i.setBorderLeft(CellStyle.BORDER_THIN);
                 borde_i.setAlignment(CellStyle.ALIGN_LEFT);

                 HSSFCellStyle borde_c = libro.createCellStyle();
                 borde_c.setBorderBottom(CellStyle.BORDER_THIN);
                 borde_c.setBorderTop(CellStyle.BORDER_THIN);
                 borde_c.setBorderRight(CellStyle.BORDER_THIN);
                 borde_c.setBorderLeft(CellStyle.BORDER_THIN);
                 borde_c.setAlignment(CellStyle.ALIGN_CENTER);

                 HSSFCellStyle borde_dr = libro.createCellStyle();
                 borde_dr.setBorderBottom(CellStyle.BORDER_THIN);
                 borde_dr.setBorderTop(CellStyle.BORDER_THIN);
                 borde_dr.setBorderRight(CellStyle.BORDER_THIN);
                 borde_dr.setBorderLeft(CellStyle.BORDER_THIN);
                 borde_dr.setAlignment(CellStyle.ALIGN_RIGHT);
                 borde_dr.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                 borde_dr.setFillBackgroundColor(HSSFColor.LIGHT_BLUE.index);
                 borde_dr.setFillForegroundColor(HSSFColor.YELLOW.index);

                 HSSFCellStyle borde_ir = libro.createCellStyle();
                 borde_ir.setBorderBottom(CellStyle.BORDER_THIN);
                 borde_ir.setBorderTop(CellStyle.BORDER_THIN);
                 borde_ir.setBorderRight(CellStyle.BORDER_THIN);
                 borde_ir.setBorderLeft(CellStyle.BORDER_THIN);
                 borde_ir.setAlignment(CellStyle.ALIGN_LEFT);
                 borde_ir.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                 borde_ir.setFillBackgroundColor(HSSFColor.LIGHT_BLUE.index);
                 borde_ir.setFillForegroundColor(HSSFColor.YELLOW.index);

                 HSSFCellStyle borde_cr = libro.createCellStyle();
                 borde_cr.setBorderBottom(CellStyle.BORDER_THIN);
                 borde_cr.setBorderTop(CellStyle.BORDER_THIN);
                 borde_cr.setBorderRight(CellStyle.BORDER_THIN);
                 borde_cr.setBorderLeft(CellStyle.BORDER_THIN);
                 borde_cr.setAlignment(CellStyle.ALIGN_CENTER);
                 borde_cr.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                 borde_cr.setFillBackgroundColor(HSSFColor.LIGHT_BLUE.index);
                 borde_cr.setFillForegroundColor(HSSFColor.YELLOW.index);

                 DecimalFormat formatoPorcentaje = new DecimalFormat("#,##0.00");

                 int miRenglon = 9;
                 int num_tab = t_datos.getRowCount();
                 for (int i = 0; i < num_tab; i++) {
                     for (int j = 0; j < 4; j++) {
                         int renglon = 0;
                         switch (j) {
                         case 0:
                             renglon = 8;
                             break;
                         case 1:
                             renglon = 10;
                             break;
                         case 2:
                             renglon = 11;
                             break;
                         case 3:
                             renglon = 12;
                             break;
                         }
                         if ((Double.parseDouble(t_datos.getValueAt(i, renglon).toString()) > 0
                                 && t_datos.getValueAt(i, 9).toString().compareTo("N") == 0)
                                 || ((Double.parseDouble(t_datos.getValueAt(i, renglon).toString()) > 0
                                         && renglon >= 10))
                                 || (renglon == 8
                                         && Double.parseDouble(t_datos.getValueAt(i, 10).toString()) <= 0
                                         && Double.parseDouble(t_datos.getValueAt(i, 11).toString()) <= 0
                                         && Double.parseDouble(t_datos.getValueAt(i, 12).toString()) <= 0)) {
                             if ((boolean) t_datos.getValueAt(i, 3) == true
                                     || (boolean) t_datos.getValueAt(i, 4) == true) {
                                 libro.getSheet("Conciliacion").createRow(miRenglon);
                                 //columna0
                                 if (t_datos.getValueAt(i, 5) == null) {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(0)
                                             .setCellValue("");
                                 } else {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(0)
                                             .setCellValue(t_datos.getValueAt(i, 5).toString());
                                 }

                                 //columna1
                                 if (t_datos.getValueAt(i, 6) == null) {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(1)
                                             .setCellValue("");
                                 } else {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(1)
                                             .setCellValue(t_datos.getValueAt(i, 6).toString());
                                 }

                                 //columna2
                                 libro.getSheet("Conciliacion").getRow(miRenglon).createCell(2)
                                         .setCellValue(t_datos.getValueAt(i, renglon).toString());

                                 //columna3
                                 if (t_datos.getValueAt(i, 14) == null) {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(3)
                                             .setCellValue("");
                                 } else {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(3)
                                             .setCellValue(t_datos.getValueAt(i, 14).toString());
                                 }

                                 //columna4
                                 libro.getSheet("Conciliacion").getRow(miRenglon).createCell(4)
                                         .setCellValue(t_datos.getValueAt(i, 2).toString());

                                 //columna5
                                 if (renglon == 8 && t_datos.getValueAt(i, 9).toString().compareTo("-") == 0)
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(5)
                                             .setCellValue("");
                                 else {
                                     switch (renglon) {
                                     case 8:
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(5)
                                                 .setCellValue("N");
                                         break;
                                     case 10:
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(5)
                                                 .setCellValue("D");
                                         break;
                                     case 11:
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(5)
                                                 .setCellValue("R");
                                         break;
                                     case 12:
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(5)
                                                 .setCellValue("M");
                                         break;
                                     }
                                 }

                                 //columna6
                                 libro.getSheet("Conciliacion").getRow(miRenglon).createCell(6)
                                         .setCellValue(formatoPorcentaje.format(t_datos.getValueAt(i, 15)));

                                 //columna7 $tot aut.
                                 double n;
                                 n = BigDecimal
                                         .valueOf(Double.parseDouble(t_datos.getValueAt(i, renglon).toString())
                                                 * Double.parseDouble(t_datos.getValueAt(i, 15).toString()))
                                         .setScale(2, RoundingMode.UP).doubleValue();
                                 libro.getSheet("Conciliacion").getRow(miRenglon).createCell(7)
                                         .setCellValue(formatoPorcentaje.format(n));

                                 //columna8
                                 libro.getSheet("Conciliacion").getRow(miRenglon).createCell(8)
                                         .setCellValue(formatoPorcentaje.format(t_datos.getValueAt(i, 16)));

                                 //columna9 $tot com
                                 n = BigDecimal
                                         .valueOf(Double.parseDouble(t_datos.getValueAt(i, renglon).toString())
                                                 * Double.parseDouble(t_datos.getValueAt(i, 16).toString()))
                                         .setScale(2, RoundingMode.UP).doubleValue();
                                 libro.getSheet("Conciliacion").getRow(miRenglon).createCell(9)
                                         .setCellValue(formatoPorcentaje.format(n));

                                 //columna10 11
                                 if (renglon == 8 && t_datos.getValueAt(i, 9).toString().compareTo("-") == 0) {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(10)
                                             .setCellValue("");
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(11)
                                             .setCellValue("");
                                 } else {
                                     switch (renglon) {
                                     case 8:
                                         n = BigDecimal.valueOf(
                                                 Double.parseDouble(t_datos.getValueAt(i, 16).toString()) / 0.9d)
                                                 .setScale(2, RoundingMode.UP).doubleValue();
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(11)
                                                 .setCellValue(formatoPorcentaje.format(n * Double.parseDouble(
                                                         t_datos.getValueAt(i, renglon).toString())));
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(10)
                                                 .setCellValue(formatoPorcentaje.format(n));
                                         break;
                                     case 10:
                                         n = BigDecimal
                                                 .valueOf(
                                                         Double.parseDouble(t_datos.getValueAt(i, 15).toString())
                                                                 * 0.72d)
                                                 .setScale(2, RoundingMode.UP).doubleValue();
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(11)
                                                 .setCellValue(formatoPorcentaje.format(n * Double.parseDouble(
                                                         t_datos.getValueAt(i, renglon).toString())));
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(10)
                                                 .setCellValue(formatoPorcentaje.format(n));
                                         break;
                                     case 11:
                                         n = BigDecimal
                                                 .valueOf(
                                                         Double.parseDouble(t_datos.getValueAt(i, 15).toString())
                                                                 * 0.65d)
                                                 .setScale(2, RoundingMode.UP).doubleValue();
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(11)
                                                 .setCellValue(formatoPorcentaje.format(n * Double.parseDouble(
                                                         t_datos.getValueAt(i, renglon).toString())));
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(10)
                                                 .setCellValue(formatoPorcentaje.format(n));
                                         break;
                                     case 12:
                                         n = BigDecimal
                                                 .valueOf(
                                                         Double.parseDouble(t_datos.getValueAt(i, 15).toString())
                                                                 * 0.65d)
                                                 .setScale(2, RoundingMode.UP).doubleValue();
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(11)
                                                 .setCellValue(formatoPorcentaje.format(n * Double.parseDouble(
                                                         t_datos.getValueAt(i, renglon).toString())));
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(10)
                                                 .setCellValue(formatoPorcentaje.format(n));
                                         break;
                                     }
                                 }

                                 //columna12
                                 if (t_datos.getValueAt(i, 18) == null) {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(12)
                                             .setCellValue("");
                                 } else {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(12)
                                             .setCellValue(t_datos.getValueAt(i, 18).toString());
                                 }

                                 //columna13
                                 if (t_datos.getValueAt(i, 19) == null) {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(13)
                                             .setCellValue("");
                                 } else {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(13)
                                             .setCellValue(t_datos.getValueAt(i, 19).toString());
                                 }

                                 //columna14
                                 libro.getSheet("Conciliacion").getRow(miRenglon).createCell(14)
                                         .setCellValue("V");

                                 if (renglon == 8 && t_datos.getValueAt(i, 9).toString().compareTo("-") == 0) {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(0)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(1)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(2)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(3)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(4)
                                             .setCellStyle(borde_i);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(5)
                                             .setCellStyle(borde_c);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(6)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(7)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(8)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(9)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(10)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(11)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(12)
                                             .setCellStyle(borde_i);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(13)
                                             .setCellStyle(borde_i);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(14)
                                             .setCellStyle(borde_d);
                                 } else {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(0)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(1)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(2)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(3)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(4)
                                             .setCellStyle(borde_ir);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(5)
                                             .setCellStyle(borde_cr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(6)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(7)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(8)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(9)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(10)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(11)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(12)
                                             .setCellStyle(borde_ir);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(13)
                                             .setCellStyle(borde_ir);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(14)
                                             .setCellStyle(borde_dr);
                                 }
                                 miRenglon++;
                             }
                         }
                     }
                 }
                 //font1.setColor(BaseColor.WHITE);
                 libro.getSheet("Conciliacion").createRow(miRenglon);
                 libro.getSheet("Conciliacion")
                         .addMergedRegion(new CellRangeAddress(miRenglon, miRenglon, 0, 14));
                 libro.getSheet("Conciliacion").getRow(miRenglon).createCell(0)
                         .setCellValue("Faltante en Vales");
                 libro.getSheet("Conciliacion").getRow(miRenglon).getCell(0).setCellStyle(borde_c);
                 miRenglon++;

                 for (int i = 0; i < num_tab; i++) {
                     for (int j = 0; j < 4; j++) {
                         int renglon = 0;
                         switch (j) {
                         case 0:
                             renglon = 8;
                             break;
                         case 1:
                             renglon = 10;
                             break;
                         case 2:
                             renglon = 11;
                             break;
                         case 3:
                             renglon = 12;
                             break;
                         }
                         if ((Double.parseDouble(t_datos.getValueAt(i, renglon).toString()) > 0
                                 && t_datos.getValueAt(i, 9).toString().compareTo("N") == 0)
                                 || ((Double.parseDouble(t_datos.getValueAt(i, renglon).toString()) > 0
                                         && renglon >= 10))
                                 || (renglon == 8
                                         && Double.parseDouble(t_datos.getValueAt(i, 10).toString()) <= 0
                                         && Double.parseDouble(t_datos.getValueAt(i, 11).toString()) <= 0
                                         && Double.parseDouble(t_datos.getValueAt(i, 12).toString()) <= 0)) {
                             if ((boolean) t_datos.getValueAt(i, 3) == false
                                     && (boolean) t_datos.getValueAt(i, 4) == false
                                     && t_datos.getValueAt(i, 5) != null) {
                                 libro.getSheet("Conciliacion").createRow(miRenglon);
                                 //columna0
                                 if (t_datos.getValueAt(i, 5) == null) {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(0)
                                             .setCellValue("");
                                 } else {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(0)
                                             .setCellValue(t_datos.getValueAt(i, 5).toString());
                                 }

                                 //columna1
                                 if (t_datos.getValueAt(i, 6) == null) {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(1)
                                             .setCellValue("");
                                 } else {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(1)
                                             .setCellValue(t_datos.getValueAt(i, 6).toString());
                                 }

                                 //columna2
                                 libro.getSheet("Conciliacion").getRow(miRenglon).createCell(2)
                                         .setCellValue(t_datos.getValueAt(i, renglon).toString());

                                 //columna3
                                 if (t_datos.getValueAt(i, 14) == null) {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(3)
                                             .setCellValue("");
                                 } else {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(3)
                                             .setCellValue(t_datos.getValueAt(i, 14).toString());
                                 }

                                 //columna4
                                 libro.getSheet("Conciliacion").getRow(miRenglon).createCell(4)
                                         .setCellValue(t_datos.getValueAt(i, 2).toString());

                                 //columna5
                                 if (renglon == 8 && t_datos.getValueAt(i, 9).toString().compareTo("-") == 0)
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(5)
                                             .setCellValue("");
                                 else {
                                     switch (renglon) {
                                     case 8:
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(5)
                                                 .setCellValue("N");
                                         break;
                                     case 10:
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(5)
                                                 .setCellValue("D");
                                         break;
                                     case 11:
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(5)
                                                 .setCellValue("R");
                                         break;
                                     case 12:
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(5)
                                                 .setCellValue("M");
                                         break;
                                     }
                                 }
                                 //columna6
                                 libro.getSheet("Conciliacion").getRow(miRenglon).createCell(6)
                                         .setCellValue(formatoPorcentaje.format(t_datos.getValueAt(i, 15)));

                                 //columna7 $tot aut.
                                 double n;
                                 n = BigDecimal
                                         .valueOf(Double.parseDouble(t_datos.getValueAt(i, renglon).toString())
                                                 * Double.parseDouble(t_datos.getValueAt(i, 15).toString()))
                                         .setScale(2, RoundingMode.UP).doubleValue();
                                 libro.getSheet("Conciliacion").getRow(miRenglon).createCell(7)
                                         .setCellValue(formatoPorcentaje.format(n));

                                 //columna8
                                 libro.getSheet("Conciliacion").getRow(miRenglon).createCell(8)
                                         .setCellValue(formatoPorcentaje.format(t_datos.getValueAt(i, 16)));

                                 //columna9 $tot com
                                 n = BigDecimal
                                         .valueOf(Double.parseDouble(t_datos.getValueAt(i, renglon).toString())
                                                 * Double.parseDouble(t_datos.getValueAt(i, 16).toString()))
                                         .setScale(2, RoundingMode.UP).doubleValue();
                                 libro.getSheet("Conciliacion").getRow(miRenglon).createCell(9)
                                         .setCellValue(formatoPorcentaje.format(n));

                                 //columna10 11
                                 if (renglon == 8 && t_datos.getValueAt(i, 9).toString().compareTo("-") == 0) {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(10)
                                             .setCellValue("");
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(11)
                                             .setCellValue("");
                                 } else {
                                     switch (renglon) {
                                     case 8:
                                         n = BigDecimal.valueOf(
                                                 Double.parseDouble(t_datos.getValueAt(i, 16).toString()) / 0.9d)
                                                 .setScale(2, RoundingMode.UP).doubleValue();
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(10)
                                                 .setCellValue(formatoPorcentaje.format(n * Double.parseDouble(
                                                         t_datos.getValueAt(i, renglon).toString())));
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(11)
                                                 .setCellValue(formatoPorcentaje.format(n));
                                         break;
                                     case 10:
                                         n = BigDecimal
                                                 .valueOf(
                                                         Double.parseDouble(t_datos.getValueAt(i, 15).toString())
                                                                 * 0.72d)
                                                 .setScale(2, RoundingMode.UP).doubleValue();
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(10)
                                                 .setCellValue(formatoPorcentaje.format(n * Double.parseDouble(
                                                         t_datos.getValueAt(i, renglon).toString())));
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(11)
                                                 .setCellValue(formatoPorcentaje.format(n));
                                         break;
                                     case 11:
                                         n = BigDecimal
                                                 .valueOf(
                                                         Double.parseDouble(t_datos.getValueAt(i, 15).toString())
                                                                 * 0.65d)
                                                 .setScale(2, RoundingMode.UP).doubleValue();
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(10)
                                                 .setCellValue(formatoPorcentaje.format(n * Double.parseDouble(
                                                         t_datos.getValueAt(i, renglon).toString())));
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(11)
                                                 .setCellValue(formatoPorcentaje.format(n));
                                         break;
                                     case 12:
                                         n = BigDecimal
                                                 .valueOf(
                                                         Double.parseDouble(t_datos.getValueAt(i, 15).toString())
                                                                 * 0.65d)
                                                 .setScale(2, RoundingMode.UP).doubleValue();
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(10)
                                                 .setCellValue(formatoPorcentaje.format(n * Double.parseDouble(
                                                         t_datos.getValueAt(i, renglon).toString())));
                                         libro.getSheet("Conciliacion").getRow(miRenglon).createCell(11)
                                                 .setCellValue(formatoPorcentaje.format(n));
                                         break;
                                     }
                                 }

                                 //columna12
                                 if (t_datos.getValueAt(i, 18) == null) {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(12)
                                             .setCellValue("");
                                 } else {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(12)
                                             .setCellValue(t_datos.getValueAt(i, 18).toString());
                                 }

                                 //columna13
                                 if (t_datos.getValueAt(i, 19) == null) {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(13)
                                             .setCellValue("");
                                 } else {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).createCell(13)
                                             .setCellValue(t_datos.getValueAt(i, 19).toString());
                                 }
                                 //columna14
                                 libro.getSheet("Conciliacion").getRow(miRenglon).createCell(14)
                                         .setCellValue("");
                                 if (renglon == 8 && t_datos.getValueAt(i, 9).toString().compareTo("-") == 0) {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(0)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(1)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(2)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(3)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(4)
                                             .setCellStyle(borde_i);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(5)
                                             .setCellStyle(borde_c);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(6)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(7)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(8)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(9)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(10)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(11)
                                             .setCellStyle(borde_d);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(12)
                                             .setCellStyle(borde_i);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(13)
                                             .setCellStyle(borde_i);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(14)
                                             .setCellStyle(borde_d);
                                 } else {
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(0)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(1)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(2)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(3)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(4)
                                             .setCellStyle(borde_ir);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(5)
                                             .setCellStyle(borde_cr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(6)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(7)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(8)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(9)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(10)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(11)
                                             .setCellStyle(borde_dr);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(12)
                                             .setCellStyle(borde_ir);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(13)
                                             .setCellStyle(borde_ir);
                                     libro.getSheet("Conciliacion").getRow(miRenglon).getCell(14)
                                             .setCellStyle(borde_dr);
                                 }
                                 miRenglon++;
                             }
                         }
                     }
                 }
                 FileOutputStream archivo1 = new FileOutputStream(archivoXLS);
                 libro.write(archivo1);
                 archivo1.close();
                 Desktop.getDesktop().open(archivoXLS);
             }
         }
     } catch (Exception e) {
         e.printStackTrace();
     }
 }

From source file:android_connector.ConfigWindowController.java

/**
 * Speichert die Ergebnisse der Lufe als Excel-Tabelle.
 *
 * @author Quelle:/*from www .  j a v  a  2 s . co  m*/
 * http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/
 * @param programmende True, wenn das Programm beendet werden soll, sonst
 * false.
 *
 */
public void printResults(boolean programmende) {
    /**
     * Zahl der Messtore.
     */
    int tore = Integer.parseInt(messtore.getText());
    /**
     * Gibt die gespeicherten Werte der DB an. Jedes Unterarray
     * reprsentiert einen Datensatz. Der zweite Index ist bei 0 die
     * Startnummer, 1 der Name, 2 die Kategorie, 3 Lauf 1, 4 Lauf 2.
     */
    String[][] werte = (new MySQLConnection(host.getText(), port.getText(), db.getText(), user.getText(),
            pw.getText(), null)).getAuswertung(starter, tore);
    //FileChooser einsetzen, um Speicherort zu ermitteln
    FileChooser fc = new FileChooser();
    //Titel anzeigen
    fc.setTitle("Speicherort fr die Auswertung:");
    //standardmig im Home-Verzeichnis starten
    fc.setInitialDirectory(new File(System.getProperty("user.home")));
    //Alle-Dateien-Filter entfernen
    fc.setSelectedExtensionFilter(null);
    //FileFilter fr Exceldateien hinzufgen
    //nur "alte" Excel-Dateien knnen geschrieben werden!
    fc.getExtensionFilters()
            .addAll(new FileChooser.ExtensionFilter("Microsoft Excel 1997-2003 Dokument (.xls)", "*.xls"));
    //Dateien einlesen
    File returnVal = fc.showSaveDialog(primaryStage);
    //prfen, ob Datei zurckgegeben --> eine gewhlt; muss aber nicht existieren
    if (returnVal != null) {
        /**
         * Reprsentation der Excel-Datei.
         */
        HSSFWorkbook workbook = new HSSFWorkbook();
        /**
         * Alle Kategorien, die angegeben sind.
         */
        List<String> kategorien = getKategorien();
        //alle Kategorien durchgehen, fr alle eine Mappe der DB fllen.
        for (String kategorie : kategorien) {
            /**
             * Reprsentiert die Mappe, in der die Daten landen.
             */
            HSSFSheet sheet;
            //Prfen, ob die Lnge des Kategorienamens zwischen 1 und 31 liegt --> sonst ungltig
            if (!kategorie.isEmpty() && kategorie.length() < 31) {
                //wenn ja: Kategoriename ist Mappenname
                sheet = workbook.createSheet(kategorie);
                //sonst: Standardname
            } else {
                sheet = workbook.createSheet("namenlose Kategorie");
            }

            Map<String, List<String>> data = new HashMap<String, List<String>>();
            List<String> label = new ArrayList<>();
            label.add("Startnummer");
            label.add("Name");
            label.add("Kategorie");
            label.add("reine Laufzeit- Lauf 1");
            label.add("Gesamtstrafen- Lauf 1");
            label.add("Laufzeit insgesamt- Lauf 1");
            for (int i = 0; i < tore; i++) {
                label.add("Strafe Tor " + (i + 1));
            }
            label.add("reine Laufzeit- Lauf 2");
            label.add("Gesamtstrafen- Lauf 2");
            label.add("Laufzeit insgesamt- Lauf 2");
            for (int i = 0; i < tore; i++) {
                label.add("Strafe Tor " + (i + 1));
            }
            data.put("0", label);

            for (int i = 0; i < werte.length; i++) {
                //data.put(""+(i+2), werte[i]);
                if (werte[i][2] != null) {
                    if (werte[i][2].equals(kategorie)) {
                        List<String> angaben = new ArrayList<>();
                        angaben.add(werte[i][0]);
                        angaben.add(werte[i][1]);
                        angaben.add(werte[i][2]);
                        String lauf1 = werte[i][3];
                        if (werte[i][3] != null && !werte[i][3].isEmpty()) {
                            angaben.addAll(extractList(lauf1));
                        }
                        if (werte[i][4] != null && !werte[i][4].isEmpty()) {
                            angaben.addAll(extractList(werte[i][4]));
                        }
                        data.put("" + (i + 1), angaben);
                    }
                }
            }
            Set<String> keyset = data.keySet();
            int rownum = 0;
            for (String key : keyset) {
                Row row = sheet.createRow(rownum++);
                List<String> objArr = data.get(key);
                int cellnum = 0;
                for (Object obj : objArr) {
                    Cell cell = row.createCell(cellnum++);
                    if (obj instanceof Date) {
                        cell.setCellValue((Date) obj);
                    } else if (obj instanceof Boolean) {
                        cell.setCellValue((Boolean) obj);
                    } else if (obj instanceof String) {
                        cell.setCellValue((String) obj);
                    } else if (obj instanceof Double) {
                        cell.setCellValue((Double) obj);
                    }
                }
            }
            for (int i = 0; i < 9 + 2 * tore; i++) {
                sheet.autoSizeColumn(i);
            }
        }
        try {
            FileOutputStream out = new FileOutputStream(returnVal);
            workbook.write(out);
            out.close();
            System.out.println("Excel written successfully..");
            out = new FileOutputStream(System.getProperty("user.home") + "/Kanu-s.a.M.-Notfall.xls");
            workbook.write(out);
            out.close();

        } catch (FileNotFoundException e) {
            MySQLConnection.staticExceptionDialog(e, "Schreibfehler",
                    "Auswertung konnte nicht geschrieben werden",
                    "Die Excel-Datei mit der Auswertung konnte nicht geschrieben werden. Bitte versuchen Sie es erneut!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (programmende) {
        Alert alert = new Alert(AlertType.INFORMATION);
        alert.setTitle("Programmende");
        alert.setHeaderText("Der Programmablauf ist beendet.");
        alert.setContentText(
                "Das Programm hat seine Aufgabe erfllt und wird nun beendet. Vielen Dank fr die Benutzung des Softwaresystems!");
        alert.showAndWait();
        Platform.exit();
    }
}

From source file:attandance.standalone.manager.AttandanceManager.java

private void outputAttandance(List<LateRecord> lates, List<AbsenceRecord> absences) {
    // webbookExcel  
    HSSFWorkbook wb = new HSSFWorkbook();
    // webbooksheet,Excelsheet  
    HSSFSheet sheet = wb.createSheet("");
    int width = ((int) (20 * 1.14388)) * 256;
    sheet.setColumnWidth(0, width);//  ww  w. j a va2 s.co m
    sheet.setColumnWidth(1, width);
    sheet.setColumnWidth(2, width);
    sheet.setColumnWidth(3, width);
    // sheet0,??poiExcel?short  
    HSSFRow row = sheet.createRow((int) 0);
    // ?   
    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ?  

    HSSFCell cell = row.createCell((short) 0);
    cell.setCellValue("??");
    cell.setCellStyle(style);
    cell = row.createCell((short) 1);
    cell.setCellValue("");
    cell.setCellStyle(style);
    cell = row.createCell((short) 2);
    cell.setCellValue("?");
    cell.setCellStyle(style);
    cell = row.createCell((short) 3);
    cell.setCellValue("?");
    cell.setCellStyle(style);

    // ? ??   
    int i = 0;
    for (; i < lates.size(); i++) {
        row = sheet.createRow((int) i + 1);
        LateRecord lateStaff = lates.get(i);
        // ?  
        row.createCell((short) 0).setCellValue(lateStaff.getStaffName());
        row.createCell((short) 1).setCellValue(lateStaff.getCaculateDateString());
        cell = row.createCell((short) 2);
        cell.setCellValue(new SimpleDateFormat(DateHelper.DATE_FORMAT).format(lateStaff.getLateDate()));
        row.createCell((short) 3).setCellValue(lateStaff.getLateTimeDesc());
    }
    for (int j = 0; j < absences.size(); j++) {
        row = sheet.createRow((int) i + j + 1);
        AbsenceRecord absenceStaff = absences.get(j);
        // ?  
        row.createCell((short) 0).setCellValue(absenceStaff.getStaffName());
        row.createCell((short) 1).setCellValue(absenceStaff.getCaculateDateString());
        cell = row.createCell((short) 2);
        cell.setCellValue(
                new SimpleDateFormat(DateHelper.ONLY_DATE_FORMAT).format(absenceStaff.getAbsenceDate()));
        row.createCell((short) 3).setCellValue("?");
    }
    // ?  
    try {
        String fileName = "C:/xhuxing-private/" + new Date(System.currentTimeMillis()).getMonth()
                + ".xls";
        FileOutputStream fout = new FileOutputStream(fileName);
        wb.write(fout);
        fout.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:attheraces.ExportarParaExcel.java

private void criarUnicoArquivo() {
    FileOutputStream fos = null;// w  w  w  .  jav a  2  s .  c  o m
    try {
        HSSFWorkbook workbook = new HSSFWorkbook();
        File f = new File(caminho);

        fos = new FileOutputStream(f);

        int i = 1;
        HSSFSheet sheet = workbook.createSheet("Attherace");
        criarCabecalho(sheet);
        for (Informacoes inf : listagem) {
            addLinha(sheet, inf, i);
            i++;
        }

        workbook.write(fos);
    } catch (Exception e) {
        TrataException.fatal(e);
    } finally {
        try {
            fos.flush();
            fos.close();
        } catch (Exception e) {
            TrataException.fatal(e);
        }
    }
}

From source file:attheraces.ExportarParaExcel.java

private void criarUmArquivoPorMes() {
    try {// w w  w . j  a v  a  2  s  .c o m
        String mesAnoAnterior = "";
        int i = 1;
        HSSFSheet sheet = null;
        HSSFWorkbook workbook = null;
        File f = null;
        FileOutputStream fos = null;
        for (Informacoes inf : listagem) {

            String mesAno = DataUtils.getDataFormatada(inf.getData(), "MM-yy");

            if (!mesAno.equals(mesAnoAnterior)) {

                if (workbook != null) {
                    workbook.write(fos);
                    fos.flush();
                    fos.close();
                }

                workbook = new HSSFWorkbook();
                f = new File(caminho + mesAno + ".xls");
                fos = new FileOutputStream(f);
                sheet = workbook.createSheet("Attheraces");
                i = 1;

                criarCabecalho(sheet);
            }

            addLinha(sheet, inf, i);

            mesAnoAnterior = mesAno;
            i++;
        }

        workbook.write(fos);
        fos.flush();
        fos.close();

    } catch (Exception e) {
        TrataException.fatal(e);
    }
}

From source file:attheraces.ExportarParaExcel.java

private void criarUmArquivoPorMesSeparadoEmAbas() {
    try {/*from w  w w .  j a  va2s  .  c o m*/
        String diaAnterior = "";
        String mesAnoAnterior = "";
        int i = 0;
        HSSFSheet sheet = null;
        HSSFWorkbook workbook = null;
        File f = null;
        FileOutputStream fos = null;
        for (Informacoes inf : listagem) {

            String dia = DataUtils.getDataFormatada(inf.getData(), "dd");
            String mesAno = DataUtils.getDataFormatada(inf.getData(), "MM-yy");

            if (!mesAno.equals(mesAnoAnterior)) {

                if (workbook != null) {
                    workbook.write(fos);
                    fos.flush();
                    fos.close();
                }

                workbook = new HSSFWorkbook();
                f = new File(caminho + mesAno + ".xls");
                fos = new FileOutputStream(f);
            }

            if (!dia.equals(diaAnterior)) {
                i = 0;
                sheet = workbook.createSheet(dia.split("/")[0]);
                criarCabecalho(sheet);
                i++;
            }

            addLinha(sheet, inf, i);

            diaAnterior = dia;
            mesAnoAnterior = mesAno;
            i++;
        }

        workbook.write(fos);
        fos.flush();
        fos.close();

    } catch (Exception e) {
        TrataException.fatal(e);
    }
}

From source file:BaseDatos.ExportarDatos.java

public void CrearExel() throws IOException {

    FileOutputStream archivoSalida = null;

    try {//from   w  ww.jav  a2 s. co  m
        Statement conexion = ConectarMysql.obtenerConexion().createStatement(ResultSet.CONCUR_READ_ONLY,
                ResultSet.TYPE_FORWARD_ONLY);
        HSSFWorkbook libro = new HSSFWorkbook();
        HSSFSheet hoja = libro.createSheet("reporte");
        String SqlVentas = "select * from ventas_realizadas";
        ResultSet resultado = conexion.executeQuery(SqlVentas);

        HSSFRow fila = hoja.createRow((short) 0);
        HSSFCell celda = fila.createCell(0);
        celda.setCellValue("nombre del producto");
        celda = fila.createCell(1);
        celda.setCellValue("fabricante");
        celda = fila.createCell(2);
        celda.setCellValue("contenedor");
        celda = fila.createCell(3);
        celda.setCellValue("cantidad");
        celda = fila.createCell(4);
        celda.setCellValue("precio Compra");
        celda = fila.createCell(5);
        celda.setCellValue("precio Venta");
        celda = fila.createCell(6);
        celda.setCellValue("fecha");
        if (resultado.first()) {
            int contador = 1;

            do {
                filaExel.clear();
                fila = hoja.createRow((short) contador);

                obtenerDatos(resultado.getInt(2));
                cantidad = resultado.getString(3);
                precioCompra = resultado.getString(4);
                precioVenta = resultado.getString(5);
                fecha = resultado.getString(6);
                filaExel.add(nombreProducto);
                filaExel.add(fabricante);
                filaExel.add(contenedor);
                filaExel.add(cantidad);
                filaExel.add(precioCompra);
                filaExel.add(precioVenta);
                filaExel.add(fecha);
                for (int a = 0; a < filaExel.size(); a++) {
                    celda = fila.createCell(a);
                    if (a == 3) {
                        celda.setCellValue(Integer.parseInt(filaExel.get(a)));
                    } else if (a == 4 || a == 5) {
                        celda.setCellValue(Double.parseDouble(filaExel.get(a)));
                    } else {
                        celda.setCellValue(filaExel.get(a));
                    }
                }
                this.operacion.setText("operando : " + contador);
                contador++;
            } while (resultado.next());

        }
        String direccionCompleta = "C:\\Users\\zombozo\\Documents\\ventas.xls";
        File archivo = new File(direccionCompleta);
        archivoSalida = new FileOutputStream(archivo);
        libro.write(archivoSalida);
        archivoSalida.close();
        conexion.close();
        JOptionPane.showMessageDialog(rootPane,
                "Creado Correctamente, busque el archivo en la carpeta documentos");
    } catch (Exception e) {
        JOptionPane.showMessageDialog(rootPane, "Ocurrio un error 1: " + e);
    }
}

From source file:BaseDatos.ExportarDatos.java

private void CrearExelCompras() {
    FileOutputStream archivoSalida = null;

    try {//  www .j  ava 2 s .c  o  m
        Statement conexion = ConectarMysql.obtenerConexion().createStatement(ResultSet.CONCUR_READ_ONLY,
                ResultSet.TYPE_FORWARD_ONLY);
        HSSFWorkbook libro = new HSSFWorkbook();
        HSSFSheet hoja = libro.createSheet("reporte");
        String SqlVentas = "select * from logCompras";
        ResultSet resultado = conexion.executeQuery(SqlVentas);

        HSSFRow fila = hoja.createRow((short) 0);
        HSSFCell celda = fila.createCell(0);
        celda.setCellValue("nombre del producto");
        celda = fila.createCell(1);
        celda.setCellValue("fabricante");
        celda = fila.createCell(2);
        celda.setCellValue("contenedor");
        celda = fila.createCell(3);
        celda.setCellValue("cantidad");
        celda = fila.createCell(4);
        celda.setCellValue("precio Compra");
        celda = fila.createCell(5);
        celda.setCellValue("precio Venta");
        celda = fila.createCell(6);
        celda.setCellValue("fecha");
        if (resultado.first()) {
            int contador = 1;

            do {
                filaExel.clear();
                fila = hoja.createRow((short) contador);

                obtenerDatos(resultado.getInt(2));
                cantidad = resultado.getString(3);
                precioCompra = resultado.getString(4);
                precioVenta = resultado.getString(5);
                fecha = resultado.getString(6);
                filaExel.add(nombreProducto);
                filaExel.add(fabricante);
                filaExel.add(contenedor);
                filaExel.add(cantidad);
                filaExel.add(precioCompra);
                filaExel.add(precioVenta);
                filaExel.add(fecha);
                for (int a = 0; a < filaExel.size(); a++) {
                    celda = fila.createCell(a);
                    if (a == 3) {
                        celda.setCellValue(Integer.parseInt(filaExel.get(a)));
                    } else if (a == 4 || a == 5) {
                        celda.setCellValue(Double.parseDouble(filaExel.get(a)));
                    } else {
                        celda.setCellValue(filaExel.get(a));
                    }
                }
                this.operacion.setText("operando : " + contador);
                contador++;
            } while (resultado.next());

        }
        String direccionCompleta = "C:\\Users\\zombozo\\Documents\\compras.xls";
        File archivo = new File(direccionCompleta);
        archivoSalida = new FileOutputStream(archivo);
        libro.write(archivoSalida);
        archivoSalida.close();
        JOptionPane.showMessageDialog(rootPane,
                "Creado Correctamente, busque el archivo en la carpeta documentos");
        conexion.close();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(rootPane, "Ocurrio un error 1: " + e);
    }
}