Example usage for org.apache.poi.ss.usermodel Sheet autoSizeColumn

List of usage examples for org.apache.poi.ss.usermodel Sheet autoSizeColumn

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Sheet autoSizeColumn.

Prototype

void autoSizeColumn(int column);

Source Link

Document

Adjusts the column width to fit the contents.

Usage

From source file:servlets.ReportesCSV.java

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Connection con = null;//from  w  w  w .  jav a2 s.com
    PreparedStatement ps = null;
    ResultSet rs = null;
    String fechaInicial = request.getParameter("fechaInicial");
    String fechaFinal = request.getParameter("fechaFinal");
    String url = getServletContext().getRealPath("/");
    String identificacion = request.getParameter("identificacion");
    String producto = request.getParameter("producto");
    String cotizacion = request.getParameter("cotizacion");
    int sede = Integer.parseInt(request.getParameter("sede"));
    String tipo = request.getParameter("tipo");
    String query = null;
    try {
        con = DBConnector.getInstance().getConnection();
        switch (tipo) {
        case "rotacion":
            query = "SELECT p.codigoInterno,p.nomProducto,sum(cantidad),mim.nombreMovimiento,p.costo "
                    + "FROM kalamarypos.inv_movimiento m "
                    + "inner join inv_movimiento_detalle md on md.inv_movimiento_numDoc = m.numDoc "
                    + "inner join cfg_mov_inventario_detalle mi on mi.idMovInventarioDetalle = m.cfg_mov_inventario_detalle_idMovInventarioDetalle "
                    + "inner join cfg_mov_inventario_maestro mim on mim.idMovInventarioMaestro = mi.cfg_mov_inventario_maestro_idMovInventarioMaestro "
                    + "inner join cfg_producto p on p.idProducto = md.cfg_producto_IdProducto "
                    + "where fecha between ? and ADDDATE(?, INTERVAL 13 DAY) "
                    + "and cfg_empresasede_idSede = ? "
                    + "group by p.codProducto,p.nomProducto,mim.nombreMovimiento " + "order by 3 desc";
            break;
        case "vc":
            query = "SELECT f.fecCrea,d.prefijoDoc, fc.fac_documentosmaster_numDocumento,ifnull(di.valorImpuesto,0) iva,f.subtotal-f.descuento as subtotal,f.total, "
                    + "concat(ti.abreviatura,' ',c.numDoc,' ',c.nom1Cliente,' ',c.nom2Cliente,' ',c.apellido1,' ',c.apellido2) as nombre "
                    + "FROM kalamarypos.fac_cartera_cliente fc "
                    + "inner join fac_documentosmaster f on fc.fac_documentosmaster_numDocumento = f.numDocumento "
                    + "inner join cfg_cliente c on c.idCliente = fc.cfg_cliente_idCliente "
                    + "inner join cfg_documento d on d.cfg_empresasede_idSede = f.cfg_empresasede_idSede "
                    + "inner join cfg_tipoidentificacion ti on ti.id = c.cfg_tipoidentificacion_id "
                    + "left join fac_documentoimpuesto di on di.fac_documentosmaster_numDocumento = fc.fac_documentosmaster_numDocumento and di.porcentajeImpuesto=16 and di.fac_documentosmaster_cfg_documento_idDoc=f.cfg_documento_idDoc "
                    + "where f.estado!='CANCELADA' and f.estado!='ANULADA' " + "and f.cfg_empresasede_idSede=? "
                    + "and d.codDocumento  !=6 " + "and f.cfg_documento_idDoc =d.idDoc ";
            if (!identificacion.equals("0")) {
                query = query + " and c.numDoc = ? ";
            }
            query = query + " order by 1 desc";
            break;
        case "productosVendidos":
            query = "SELECT c.nombreCategoria, " + "       r.nombreReferencia, " + "      m.nombreMarca, "
                    + "      p.codigoInterno, " + "      p.nomProducto, " + "      sum(fd.cantidad) as total, "
                    + "      max(f.fecCrea) as maxFeCrea " + "FROM kalamarypos.fac_cartera_cliente fc "
                    + "inner join fac_documentosmaster f on fc.fac_documentosmaster_numDocumento = f.numDocumento "
                    + "inner join cfg_documento d on d.cfg_empresasede_idSede = f.cfg_empresasede_idSede "
                    + "inner join fac_documentodetalle  fd on fd.fac_documentosmaster_cfg_documento_idDoc=f.cfg_documento_idDoc "
                    + "inner join cfg_producto p on p.idProducto = fd.cfg_producto_idProducto "
                    + "inner join cfg_categoriaproducto c on c.idCategoria = p.cfg_categoriaproducto_idCategoria "
                    + "inner join cfg_referenciaproducto r on r.idReferencia = p.cfg_referenciaproducto_idReferencia "
                    + "inner join cfg_marcaproducto m on m.idMarca = p.cfg_marcaproducto_idMarca "
                    + "where f.estado!='CANCELADA' and f.estado!='ANULADA' " + "and f.cfg_empresasede_idSede=? "
                    + "and fd.fac_documentosmaster_numDocumento=f.numDocumento "
                    + "and f.cfg_documento_idDoc =d.idDoc  "
                    + "and f.fecCrea between ? and ADDDATE(?, INTERVAL 1 DAY) "
                    + "and fd.fac_documentosmaster_numDocumento = f.numDocumento "
                    + "group by fd.cfg_producto_idProducto " + "order by 6 desc ";
            break;//productosVendidos
        case "ventasVendedores":
            query = "SELECT f.fecCrea,d.prefijoDoc, fc.fac_documentosmaster_numDocumento,ifnull(di.valorImpuesto,0) iva,f.subtotal-f.descuento as subtotal,f.total, "
                    + "concat('CC ',s.numDoc,' ',s.nom1Usuario,' ',s.nom2Usuario,' ',s.apellido1,' ',s.apellido2) as nombre "
                    + "FROM kalamarypos.fac_cartera_cliente fc "
                    + "inner join fac_documentosmaster f on fc.fac_documentosmaster_numDocumento = f.numDocumento "
                    + "inner join cfg_documento d on d.cfg_empresasede_idSede = f.cfg_empresasede_idSede "
                    + "inner join seg_usuario s on s.idUsuario = f.seg_usuario_idUsuario1 "
                    + " left join fac_documentoimpuesto di on di.fac_documentosmaster_numDocumento = fc.fac_documentosmaster_numDocumento and di.porcentajeImpuesto=16 and di.fac_documentosmaster_cfg_documento_idDoc=f.cfg_documento_idDoc "
                    + "where f.estado!='CANCELADA' and f.estado!='ANULADA' " + "and f.cfg_empresasede_idSede=? "
                    + "and f.fecCrea between ? and  ADDDATE(?, INTERVAL 13 DAY) "
                    + "and f.cfg_documento_idDoc =d.idDoc " + "and d.codDocumento  !=6 "
                    + "and s.cfg_rol_idrol=3 ";
            if (!identificacion.equals("0")) {
                if (!identificacion.equals("")) {
                    query = query + "and s.numDoc = ? ";
                }
            }
            query = query + " order by 1 desc";
            break;//Ventas vendedores
        case "productosA":
            query = "select c.nombreCategoria, " + "       r.nombreReferencia, " + "      m.nombreMarca, "
                    + "      p.codigoInterno, " + "       p.nomProducto, " + "      p.precio "
                    + "from cfg_producto p "
                    + "inner join cfg_empresasede s on s.cfg_empresa_idEmpresa = p.cfg_empresa_idEmpresa "
                    + "inner join cfg_categoriaproducto c on c.idCategoria = p.cfg_categoriaproducto_idCategoria "
                    + "inner join cfg_referenciaproducto r on r.idReferencia = p.cfg_referenciaproducto_idReferencia "
                    + "inner join cfg_marcaproducto m on m.idMarca = p.cfg_marcaproducto_idMarca "
                    + "where s.idSede =? ";
            if (producto != null) {
                if (!producto.equals("")) {
                    query = query + "and p.codigoInterno= ? ";
                }
            }

            break;//Productos
        case "servicios":
            query = "SELECT c.nombreCategoria, " + "       r.nombreReferencia, " + "      m.nombreMarca, "
                    + "      p.codigoInterno, " + "       p.codBarProducto , " + "      p.nomProducto, "
                    + "      p.costo, " + "        p.utilidad, " + "        p.precio, " + "      p.fecCrea "
                    + "FROM " + " cfg_producto p "
                    + "inner join cfg_empresasede s on s.cfg_empresa_idEmpresa = p.cfg_empresa_idEmpresa "
                    + "inner join cfg_categoriaproducto c on c.idCategoria = p.cfg_categoriaproducto_idCategoria "
                    + "inner join cfg_referenciaproducto r on r.idReferencia = p.cfg_referenciaproducto_idReferencia "
                    + "inner join cfg_marcaproducto m on m.idMarca = p.cfg_marcaproducto_idMarca "
                    + "where p.esServicio=1 " + "and s.idSede =? ";
            if (producto != null) {
                if (!producto.equals("")) {
                    query = query + "and p.codigoInterno= ? ";
                }
            }

            query = query + "order by 1 desc";
            break;//Servicios
        case "stock":
            query = "SELECT c.nombreCategoria, " + "       r.nombreReferencia, " + "      m.nombreMarca, "
                    + "      p.codigoInterno, " + "      p.nomProducto, " + "      co.existencia "
                    + "FROM kalamarypos.cfg_producto p "
                    + "inner join inv_consolidado co on co.cfg_producto_idProducto = p.idProducto "
                    + "inner join cfg_categoriaproducto c on c.idCategoria = p.cfg_categoriaproducto_idCategoria "
                    + "inner join cfg_referenciaproducto r on r.idReferencia = p.cfg_referenciaproducto_idReferencia "
                    + "inner join cfg_marcaproducto m on m.idMarca = p.cfg_marcaproducto_idMarca "
                    + "where co.cfg_empresasede_idSede=? ";
            if (producto != null) {
                if (!producto.equals("")) {
                    query = query + "and p.codigoInterno= ? ";
                }
            }

            query = query + " order by co.existencia desc";
            break;//stock
        case "productoClientes":
            query = "SELECT concat(ti.abreviatura,' ',c.numDoc,' ',c.nom1Cliente,' ',c.nom2Cliente,' ',c.apellido1,' ',c.apellido2) as nombre, "
                    + "fc.cfg_cliente_idCliente,fc.fac_documentosmaster_numDocumento,sum(cantidad) as total,fd.cfg_producto_idProducto, "
                    + "max(f.fecCrea),concat(ca.nombreCategoria,' ',r.nombreReferencia,' ',m.nombreMarca,' ',p.codigoInterno,' ',p.nomProducto) as producto "
                    + "FROM kalamarypos.fac_cartera_cliente fc "
                    + "inner join fac_documentosmaster f on fc.fac_documentosmaster_numDocumento = f.numDocumento "
                    + "inner join fac_documentodetalle  fd on fd.fac_documentosmaster_cfg_documento_idDoc=f.cfg_documento_idDoc "
                    + "inner join cfg_cliente c on c.idCliente = fc.cfg_cliente_idCliente "
                    + "inner join cfg_documento d on d.cfg_empresasede_idSede = f.cfg_empresasede_idSede "
                    + "inner join cfg_tipoidentificacion ti on ti.id = c.cfg_tipoidentificacion_id "
                    + "inner join cfg_producto p on p.idProducto = fd.cfg_producto_idProducto "
                    + "inner join cfg_categoriaproducto ca on ca.idCategoria = p.cfg_categoriaproducto_idCategoria "
                    + "inner join cfg_referenciaproducto r on r.idReferencia = p.cfg_referenciaproducto_idReferencia "
                    + "inner join cfg_marcaproducto m on m.idMarca = p.cfg_marcaproducto_idMarca "
                    + "where f.estado!='CANCELADA' and f.estado!='ANULADA' " + "and f.cfg_empresasede_idSede=? "
                    + "and fd.fac_documentosmaster_numDocumento = f.numDocumento " + "and d.codDocumento  =1 ";
            if (producto != null) {
                if (!producto.equals("")) {
                    query = query + "and p.codigoInterno= ? ";
                }
            }

            query = query + " group by fd.cfg_producto_idProducto " + "order by 4 desc";
            break;//productosClientes
        case "cotizacion":
            query = "SELECT p.codigoInterno, " + "p.nomProducto, " + "p.precio, "
                    + "ifnull(di.valorImpuesto,0) as iva, " + "f.total, "
                    + "concat(ti.abreviatura,' ',cl.numDoc,' ',cl.nom1Cliente,' ',cl.nom2Cliente,' ',cl.apellido1,' TELEFONO ',cl.tel1) as nombre, "
                    + "concat('No Cotizacin:',' ',d.prefijoDoc,' ', fc.fac_documentosmaster_numDocumento) cotizacion, "
                    + "f.fecCrea, concat('Vendedor:',s.nom1Usuario,' ',s.nom2Usuario,' ',s.apellido1,' ',s.apellido2) as vendedor "
                    + " " + "FROM kalamarypos.fac_documentosmaster fc "
                    + "inner join fac_documentosmaster f on fc.fac_documentosmaster_numDocumento = f.numDocumento "
                    + "inner join fac_documentodetalle  fd on fd.fac_documentosmaster_cfg_documento_idDoc=f.cfg_documento_idDoc "
                    + "inner join cfg_cliente cl on cl.idCliente = fc.cfg_cliente_idCliente "
                    + "inner join cfg_documento d on d.cfg_empresasede_idSede = f.cfg_empresasede_idSede "
                    + "inner join cfg_producto p on p.idProducto = fd.cfg_producto_idProducto "
                    + "inner join cfg_categoriaproducto c on c.idCategoria = p.cfg_categoriaproducto_idCategoria "
                    + "inner join cfg_referenciaproducto r on r.idReferencia = p.cfg_referenciaproducto_idReferencia "
                    + "inner join cfg_marcaproducto m on m.idMarca = p.cfg_marcaproducto_idMarca "
                    + "inner join cfg_tipoidentificacion ti on ti.id = cl.cfg_tipoidentificacion_id "
                    + "inner join seg_usuario s on s.idUsuario = f.seg_usuario_idUsuario1 "
                    + "left join fac_documentoimpuesto di on di.fac_documentosmaster_numDocumento = fc.fac_documentosmaster_numDocumento and di.porcentajeImpuesto=16 and di.fac_documentosmaster_cfg_documento_idDoc=f.cfg_documento_idDoc "
                    + "where f.estado!='CANCELADA' and f.estado!='ANULADA' "
                    + "and f.cfg_empresasede_idSede=? ";
            if (identificacion != null) {
                if (!identificacion.equals(""))
                    query = query + "and cl.numDoc =  ? ";
            }
            if (cotizacion != null) {
                if (!cotizacion.equals(""))
                    query = query + "and fc.fac_documentosmaster_numDocumento =  ? ";
            }
            query = query + " and d.codDocumento  =6 "
                    + "and fd.fac_documentosmaster_numDocumento=f.numDocumento "
                    + "and f.cfg_documento_idDoc = d.idDoc " + "and s.cfg_rol_idrol=3";
            break;//cotizacion
        case "vencimientosFacturas":
            query = "SELECT cc.numDoc, "
                    + "      concat(cc.nom1Cliente,' ',cc.nom2Cliente,' ',cc.apellido1,' ',cc.apellido2) cliente, "
                    + "       cc.tel1, " + "       fc.valor, " + "       fc.saldo, "
                    + "       fc.fecha_limite, " + "       concat(d.prefijoDoc,' ',fd.numDocumento) documento "
                    + "FROM kalamarypos.fac_cartera_cliente fc "
                    + "inner join fac_documentosmaster fd on fc.fac_documentosmaster_numDocumento = fd.numDocumento "
                    + "inner join cfg_cliente cc on cc.idCliente = fc.cfg_cliente_idCliente "
                    + "inner join cfg_documento d on d.cfg_empresasede_idSede = fd.cfg_empresasede_idSede "
                    + " where fc.estado='PENDIENTE' " + "and fecha_limite<=ADDDATE(now(), INTERVAL 15 DAY) "
                    + "and fd.cfg_documento_idDoc = fc.fac_documentosmaster_cfg_documento_idDoc "
                    + "and d.codDocumento  =1 " + "and fd.cfg_documento_idDoc =d.idDoc "
                    + "and fd.cfg_empresasede_idSede=? "
                    + "and fd.cfg_documento_idDoc = fc.fac_documentosmaster_cfg_documento_idDoc "
                    + "order by fecha_limite";
            break;//vencimiento factruas
        case "vencimientosSeparados":
            query = "SELECT cc.numDoc, "
                    + "      concat(cc.nom1Cliente,' ',cc.nom2Cliente,' ',cc.apellido1,' ',cc.apellido2) cliente, "
                    + "       cc.tel1, " + "       fc.valor, " + "       fc.saldo, "
                    + "       fc.fecha_limite, " + "       concat(d.prefijoDoc,' ',fd.numDocumento) documento "
                    + "FROM kalamarypos.fac_cartera_cliente fc "
                    + "inner join fac_documentosmaster fd on fc.fac_documentosmaster_numDocumento = fd.numDocumento "
                    + "inner join cfg_cliente cc on cc.idCliente = fc.cfg_cliente_idCliente "
                    + "inner join cfg_documento d on d.cfg_empresasede_idSede = fd.cfg_empresasede_idSede "
                    + " where fc.estado='PENDIENTE' " + "and fecha_limite<=ADDDATE(now(), INTERVAL 15 DAY) "
                    + "and fd.cfg_documento_idDoc = fc.fac_documentosmaster_cfg_documento_idDoc "
                    + "and d.codDocumento  =7 " + "and fd.cfg_empresasede_idSede=? "
                    + "and fd.cfg_documento_idDoc =d.idDoc "
                    + "and fd.cfg_documento_idDoc = fc.fac_documentosmaster_cfg_documento_idDoc "
                    + "order by fecha_limite";

            break;//vencimientoSeparados
        case "productosSinRotacion":
            query = "SELECT c.nombreCategoria, " + "       r.nombreReferencia, " + "      ma.nombreMarca, "
                    + "      p.codigoInterno, " + "      p.nomProducto, " + "       max(m.fecha) fechaultimo "
                    + " " + "FROM kalamarypos.inv_movimiento_detalle  im "
                    + "inner join inv_movimiento m on im.inv_movimiento_numDoc = m.numDoc "
                    + "left join cfg_producto p on p.idProducto = im.cfg_producto_idProducto "
                    + "inner join cfg_categoriaproducto c on c.idCategoria = p.cfg_categoriaproducto_idCategoria "
                    + "inner join cfg_referenciaproducto r on r.idReferencia = p.cfg_referenciaproducto_idReferencia "
                    + "inner join cfg_marcaproducto ma on ma.idMarca = p.cfg_marcaproducto_idMarca "
                    + "where cfg_empresasede_idSede=? " + "group by im.cfg_producto_idProducto "
                    + "having DATEDIFF(now(),max(m.fecha))>=90 " + "order by 6";
            break;//productosSinRotacion
        }
        ps = con.prepareStatement(query);
        ps.clearParameters();
        int indice = 1;
        switch (tipo) {
        case "rotacion":
            ps.setString(indice++, fechaInicial);
            ps.setString(indice++, fechaFinal);
            ps.setInt(indice++, sede);
            break;
        case "vc":
            ps.setInt(indice++, sede);
            if (!identificacion.equals("0")) {
                ps.setString(indice++, identificacion);
            }
            break;
        case "productosVendidos":
            ps.setInt(indice++, sede);
            ps.setString(indice++, fechaInicial);
            ps.setString(indice++, fechaFinal);

            break;
        case "ventasVendedores":
            ps.setInt(indice++, sede);
            ps.setString(indice++, fechaInicial);
            ps.setString(indice++, fechaFinal);
            if (!identificacion.equals("0")) {
                if (!identificacion.equals(""))
                    ps.setString(indice++, identificacion);
            }
            break;
        case "productosA":
            ps.setInt(indice++, sede);
            if (producto != null) {
                if (!producto.equals("")) {
                    ps.setString(indice++, producto);
                }
            }
            break;//Producros
        case "servicios":
            ps.setInt(indice++, sede);
            if (producto != null) {
                if (!producto.equals("")) {
                    ps.setString(indice++, producto);
                }
            }
            break;//servicios
        case "stock":
            ps.setInt(indice++, sede);
            if (producto != null) {
                if (!producto.equals("")) {
                    ps.setString(indice++, producto);
                }
            }
            break;//stock
        case "productoClientes":
            ps.setInt(indice++, sede);
            if (producto != null) {
                if (!producto.equals("")) {
                    ps.setString(indice++, producto);
                }
            }
            break;//productoClientes
        case "cotizacion":
            ps.setInt(indice++, sede);
            if (identificacion != null) {
                if (!identificacion.equals(""))
                    ps.setString(indice++, identificacion);
            }
            if (cotizacion != null) {
                if (!cotizacion.equals(""))
                    ps.setString(indice++, cotizacion);
            }
            break;
        case "vencimientosFacturas":
            ps.setInt(indice++, sede);
            break;
        case "vencimientosSeparados":
            ps.setInt(indice++, sede);
            break;
        case "productosSinRotacion":
            ps.setInt(indice++, sede);
            break;

        }
        rs = ps.executeQuery();
        Date fechaActual = new Date();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sd2 = new SimpleDateFormat("dd-MMM-yyyy");
        String rutaArchivo = url + "/informes/reportes/";
        String nombreArchivo = null;
        switch (tipo) {
        case "rotacion":
            nombreArchivo = "rotacionProducto_" + sd.format(fechaActual) + "_" + sede + ".xls";
            break;
        case "vc":
            nombreArchivo = "ventasPorCliente_" + sd.format(fechaActual) + "_" + sede + ".xls";
            break;
        case "productosVendidos":
            nombreArchivo = "productosVendidos" + sd.format(fechaActual) + "_" + sede + ".xls";
            break;
        case "ventasVendedores":
            nombreArchivo = "ventasVendedores" + sd.format(fechaActual) + "_" + sede + ".xls";
            break;
        case "productosA":
            nombreArchivo = "productos" + sd.format(fechaActual) + "_" + sede + ".xls";
            break;
        case "servicios":
            nombreArchivo = "servicios" + sd.format(fechaActual) + "_" + sede + ".xls";
            break;
        case "stock":
            nombreArchivo = "stock" + sd.format(fechaActual) + "_" + sede + ".xls";
            break;
        case "productoClientes":
            nombreArchivo = "ventasProductosClientes" + sd.format(fechaActual) + "_" + sede + ".xls";
            break;
        case "cotizacion":
            nombreArchivo = "cotizacion" + sd.format(fechaActual) + "_" + sede + ".xls";
            break;
        case "vencimientosFacturas":
            nombreArchivo = "vencimientoFacturas" + sd.format(fechaActual) + "_" + sede + ".xls";
            break;
        case "vencimientosSeparados":
            nombreArchivo = "vencimientoSeparados" + sd.format(fechaActual) + "_" + sede + ".xls";
            break;
        case "productosSinRotacion":
            nombreArchivo = "productosSinRotacion" + sd.format(fechaActual) + "_" + sede + ".xls";
            break;
        }

        rutaArchivo = rutaArchivo + nombreArchivo;
        File archivo1 = new File(rutaArchivo);
        if (archivo1.exists()) {
            archivo1.delete();
        }
        archivo1.createNewFile();
        HSSFWorkbook libro = new HSSFWorkbook();

        HSSFCellStyle cellStyle = libro.createCellStyle();
        HSSFFont font = libro.createFont();
        font.setFontName(HSSFFont.FONT_ARIAL);
        font.setFontHeightInPoints((short) 10);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setColor(HSSFColor.BLACK.index);
        font.setCharSet(HSSFFont.ANSI_CHARSET);
        cellStyle.setFont(font);
        /*Se inicializa el flujo de datos con el archivo xls*/
        FileOutputStream archi = new FileOutputStream(rutaArchivo);
        Sheet hoja;
        Cell celda;
        Row fila;
        int i = 0;
        switch (tipo) {
        case "rotacion":
            hoja = libro.createSheet("Rotacin de Producto");
            fila = hoja.createRow(i);
            celda = fila.createCell(0);
            celda.setCellValue("CODIGO PRODUCTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(1);
            celda.setCellValue("PRODUCTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(2);
            celda.setCellValue("CANTIDAD MOVIMIENTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(3);
            celda.setCellValue("TIPO MOVIMIENTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(4);
            celda.setCellValue("COSTO UNITARIO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(5);
            celda.setCellValue("COSTO TOTAL");
            celda.setCellStyle(cellStyle);

            i = i + 1;
            for (int j = 0; j <= 12; j++) {
                hoja.autoSizeColumn(j);
            }
            while (rs.next()) {
                fila = hoja.createRow(i);
                celda = fila.createCell(0);
                celda.setCellValue(rs.getString(1));
                celda = fila.createCell(1);
                celda.setCellValue(rs.getString(2));
                celda = fila.createCell(2);
                celda.setCellValue(rs.getInt(3));
                celda = fila.createCell(3);
                celda.setCellValue(rs.getString(4));
                celda = fila.createCell(4);
                celda.setCellValue(rs.getDouble(5));
                celda = fila.createCell(5);
                celda.setCellValue((rs.getDouble(5) * rs.getInt(3)));
                ////celda.getStringCellValue().getBytes(Charset.forName("UTF-8"));
                i = i + 1;
            }
            break;
        //En rotacin
        case "vc":
            hoja = libro.createSheet("Ventas por clientes");
            fila = hoja.createRow(i);
            celda = fila.createCell(0);
            celda.setCellValue("FECHA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(1);
            celda.setCellValue("DOCUMENTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(2);
            celda.setCellValue("CLIENTE");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(3);
            celda.setCellValue("SUBTOTAL");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(4);
            celda.setCellValue("IVA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(5);
            celda.setCellValue("COSTO TOTAL");
            celda.setCellStyle(cellStyle);

            i = i + 1;
            for (int j = 0; j <= 12; j++) {
                hoja.autoSizeColumn(j);
            }
            while (rs.next()) {
                fila = hoja.createRow(i);
                celda = fila.createCell(0);
                celda.setCellValue(sd2.format(rs.getDate(1)));
                celda = fila.createCell(1);
                celda.setCellValue(rs.getString(2) + " " + rs.getString(3));
                celda = fila.createCell(2);
                celda.setCellValue(rs.getString(7));
                celda = fila.createCell(3);
                celda.setCellValue(rs.getDouble(5));
                celda = fila.createCell(4);
                celda.setCellValue(rs.getDouble(4));
                celda = fila.createCell(5);
                celda.setCellValue(rs.getDouble(6));
                i = i + 1;
            }
            break;//Ventas por cliente

        case "productosVendidos":
            hoja = libro.createSheet("Productos Vendidos");
            fila = hoja.createRow(i);
            celda = fila.createCell(0);
            celda.setCellValue("CATEGORIA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(1);
            celda.setCellValue("REFERENCIA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(2);
            celda.setCellValue("MARCA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(3);
            celda.setCellValue("CODIGO INTERNO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(4);
            celda.setCellValue("PRODUCTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(5);
            celda.setCellValue("CANTIDAD VENDIDA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(6);
            celda.setCellValue("FECHA ULTIMA VENTA");
            celda.setCellStyle(cellStyle);

            i = i + 1;
            for (int j = 0; j <= 12; j++) {
                hoja.autoSizeColumn(j);
            }
            while (rs.next()) {
                fila = hoja.createRow(i);
                celda = fila.createCell(0);
                celda.setCellValue(rs.getString(1));

                celda = fila.createCell(1);
                celda.setCellValue(rs.getString(2));

                celda = fila.createCell(2);
                celda.setCellValue(rs.getString(3));

                celda = fila.createCell(3);
                celda.setCellValue(rs.getString(4));

                celda = fila.createCell(4);
                celda.setCellValue(rs.getString(5));

                celda = fila.createCell(5);
                celda.setCellValue(rs.getDouble(6));

                celda = fila.createCell(6);
                celda.setCellValue(sd2.format(rs.getDate(7)));
                i = i + 1;
            }
            break;//Productos Vendidos
        case "ventasVendedores":
            hoja = libro.createSheet("Ventas Vendedores");
            fila = hoja.createRow(i);
            celda = fila.createCell(0);
            celda.setCellValue("FECHA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(1);
            celda.setCellValue("VENDEDOR");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(2);
            celda.setCellValue("DOCUMENTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(3);
            celda.setCellValue("SUBTOTAL");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(4);
            celda.setCellValue("IVA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(5);
            celda.setCellValue("TOTAL");
            celda.setCellStyle(cellStyle);

            i = i + 1;
            for (int j = 0; j <= 12; j++) {
                hoja.autoSizeColumn(j);
            }
            while (rs.next()) {
                fila = hoja.createRow(i);
                celda = fila.createCell(0);
                celda.setCellValue(sd2.format(rs.getDate(1)));

                celda = fila.createCell(1);
                celda.setCellValue(rs.getString("nombre"));

                celda = fila.createCell(2);
                celda.setCellValue(rs.getString(2) + " " + rs.getString(3));

                celda = fila.createCell(3);
                celda.setCellValue(rs.getDouble("subtotal"));

                celda = fila.createCell(4);
                celda.setCellValue(rs.getDouble("IVA"));

                celda = fila.createCell(5);
                celda.setCellValue(rs.getDouble("TOTAL"));

                i = i + 1;
            }
            break;//ventas vendedores
        case "productosA":
            hoja = libro.createSheet("Informe Productos");
            fila = hoja.createRow(i);
            celda = fila.createCell(0);
            celda.setCellValue("CATEGORIA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(1);
            celda.setCellValue("REFERENCIA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(2);
            celda.setCellValue("MARCA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(3);
            celda.setCellValue("CODIGO INTERNO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(4);
            celda.setCellValue("PRODUCTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(5);
            celda.setCellValue("VALOR");
            celda.setCellStyle(cellStyle);

            i = i + 1;
            for (int j = 0; j <= 12; j++) {
                hoja.autoSizeColumn(j);
            }
            while (rs.next()) {
                fila = hoja.createRow(i);
                celda = fila.createCell(0);
                celda.setCellValue(rs.getString("nombreCategoria"));

                celda = fila.createCell(1);
                celda.setCellValue(rs.getString("nombreReferencia"));

                celda = fila.createCell(2);
                celda.setCellValue(rs.getString("nombreMarca"));

                celda = fila.createCell(3);
                celda.setCellValue(rs.getString("codigoInterno"));

                celda = fila.createCell(4);
                celda.setCellValue(rs.getString("nomProducto"));

                celda = fila.createCell(5);
                celda.setCellValue(rs.getDouble("PRECIO"));

                i = i + 1;
            }
            break;//productos
        case "servicios":
            hoja = libro.createSheet("Servicios");
            fila = hoja.createRow(i);
            celda = fila.createCell(0);
            celda.setCellValue("CATEGORIA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(1);
            celda.setCellValue("REFERENCIA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(2);
            celda.setCellValue("MARCA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(3);
            celda.setCellValue("CODIGO INTERNO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(4);
            celda.setCellValue("CODIGO DE BARRAS");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(5);
            celda.setCellValue("PRODUCTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(6);
            celda.setCellValue("COSTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(7);
            celda.setCellValue("UTILIDAD");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(8);
            celda.setCellValue("PRECIO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(9);
            celda.setCellValue("FECHA CREACION");
            celda.setCellStyle(cellStyle);

            i = i + 1;
            for (int j = 0; j <= 12; j++) {
                hoja.autoSizeColumn(j);
            }
            while (rs.next()) {
                fila = hoja.createRow(i);
                celda = fila.createCell(0);
                celda.setCellValue(rs.getString("nombreCategoria"));

                celda = fila.createCell(1);
                celda.setCellValue(rs.getString("nombreReferencia"));

                celda = fila.createCell(2);
                celda.setCellValue(rs.getString("nombreMarca"));

                celda = fila.createCell(3);
                celda.setCellValue(rs.getString("codigoInterno"));

                celda = fila.createCell(4);
                celda.setCellValue(rs.getString("codBarProducto"));

                celda = fila.createCell(5);
                celda.setCellValue(rs.getString("nomProducto"));

                celda = fila.createCell(6);
                celda.setCellValue(rs.getDouble("costo"));

                celda = fila.createCell(7);
                celda.setCellValue(rs.getDouble("utilidad"));

                celda = fila.createCell(8);
                celda.setCellValue(rs.getDouble("precio"));

                celda = fila.createCell(9);
                celda.setCellValue(sd2.format(rs.getDate("fecCrea")));

                i = i + 1;
            }

            break;//Servicios
        case "stock":
            hoja = libro.createSheet("Stock");
            fila = hoja.createRow(i);
            celda = fila.createCell(0);
            celda.setCellValue("CATEGORIA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(1);
            celda.setCellValue("REFERENCIA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(2);
            celda.setCellValue("MARCA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(3);
            celda.setCellValue("CODIGO INTERNO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(4);
            celda.setCellValue("PRODUCTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(5);
            celda.setCellValue("EXISTENCIA");
            celda.setCellStyle(cellStyle);

            i = i + 1;
            for (int j = 0; j <= 12; j++) {
                hoja.autoSizeColumn(j);
            }
            while (rs.next()) {
                fila = hoja.createRow(i);
                celda = fila.createCell(0);
                celda.setCellValue(rs.getString("nombreCategoria"));

                celda = fila.createCell(1);
                celda.setCellValue(rs.getString("nombreReferencia"));

                celda = fila.createCell(2);
                celda.setCellValue(rs.getString("nombreMarca"));

                celda = fila.createCell(3);
                celda.setCellValue(rs.getString("codigoInterno"));

                celda = fila.createCell(4);
                celda.setCellValue(rs.getString("nomProducto"));

                celda = fila.createCell(5);
                celda.setCellValue(rs.getLong("existencia"));

                i = i + 1;
            }

            break;//stock

        case "productoClientes":
            hoja = libro.createSheet("Ventas productos Clientes");
            fila = hoja.createRow(i);
            celda = fila.createCell(0);
            celda.setCellValue("CLIENTE");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(1);
            celda.setCellValue("PRODUCTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(2);
            celda.setCellValue("CANTIDAD VENDIDA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(3);
            celda.setCellValue("FECHA ULTIMA VENTA");
            celda.setCellStyle(cellStyle);

            i = i + 1;
            for (int j = 0; j <= 12; j++) {
                hoja.autoSizeColumn(j);
            }
            while (rs.next()) {
                fila = hoja.createRow(i);
                celda = fila.createCell(0);
                celda.setCellValue(rs.getString("nombre"));

                celda = fila.createCell(1);
                celda.setCellValue(rs.getString("producto"));

                celda = fila.createCell(2);
                celda.setCellValue(rs.getDouble("total"));

                celda = fila.createCell(3);
                celda.setCellValue(sd2.format(rs.getDate(6)));

                i = i + 1;
            }

            break;//productosClientes
        case "cotizacion":
            hoja = libro.createSheet("Ventas Cotizaciones");
            fila = hoja.createRow(i);
            celda = fila.createCell(0);
            celda.setCellValue("CLIENTE");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(1);
            celda.setCellValue("COTIZACION");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(2);
            celda.setCellValue("FECHA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(3);
            celda.setCellValue("CODIGO PRODUCTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(4);
            celda.setCellValue("PRODUCTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(5);
            celda.setCellValue("PRECIO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(6);
            celda.setCellValue("IVA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(7);
            celda.setCellValue("TOTAL");
            celda.setCellStyle(cellStyle);

            i = i + 1;
            for (int j = 0; j <= 12; j++) {
                hoja.autoSizeColumn(j);
            }
            while (rs.next()) {
                fila = hoja.createRow(i);
                celda = fila.createCell(0);
                celda.setCellValue(rs.getString("nombre"));

                celda = fila.createCell(1);
                celda.setCellValue(rs.getString("cotizacion"));

                celda = fila.createCell(2);
                celda.setCellValue(sd2.format(rs.getDate("fecCrea")));

                celda = fila.createCell(3);
                celda.setCellValue(rs.getString("codigoInterno"));

                celda = fila.createCell(4);
                celda.setCellValue(rs.getString("nomProducto"));

                celda = fila.createCell(5);
                celda.setCellValue(rs.getDouble("precio"));

                celda = fila.createCell(6);
                celda.setCellValue(rs.getString("iva"));

                celda = fila.createCell(7);
                celda.setCellValue(rs.getString("total"));

                i = i + 1;
            }

            break;//cotizacion

        case "vencimientosFacturas":

            hoja = libro.createSheet("Vencimiento Facturas");
            fila = hoja.createRow(i);
            celda = fila.createCell(0);
            celda.setCellValue("DOCUMENTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(1);
            celda.setCellValue("NUMERO IDENTIFICACION CLIENTE");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(2);
            celda.setCellValue("CLIENTE");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(3);
            celda.setCellValue("TELEFONO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(4);
            celda.setCellValue("VALOR");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(5);
            celda.setCellValue("SALDO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(6);
            celda.setCellValue("FECHA VENCIMIENTO");
            celda.setCellStyle(cellStyle);
            i = i + 1;
            for (int j = 0; j <= 12; j++) {
                hoja.autoSizeColumn(j);
            }
            while (rs.next()) {
                fila = hoja.createRow(i);
                celda = fila.createCell(0);
                celda.setCellValue(rs.getString("documento"));

                celda = fila.createCell(1);
                celda.setCellValue(rs.getString("numDoc"));

                celda = fila.createCell(2);
                celda.setCellValue(rs.getString("cliente"));

                celda = fila.createCell(3);
                celda.setCellValue(rs.getString("tel1"));

                celda = fila.createCell(4);
                celda.setCellValue(rs.getDouble("valor"));

                celda = fila.createCell(5);
                celda.setCellValue(rs.getDouble("saldo"));

                celda = fila.createCell(6);
                celda.setCellValue(sd2.format(rs.getDate("fecha_limite")));

                i = i + 1;
            }
            break;//vencimientofactuas
        case "vencimientosSeparados":
            hoja = libro.createSheet("Vencimiento Separados");
            fila = hoja.createRow(i);
            celda = fila.createCell(0);
            celda.setCellValue("DOCUMENTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(1);
            celda.setCellValue("NUMERO IDENTIFICACION CLIENTE");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(2);
            celda.setCellValue("CLIENTE");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(3);
            celda.setCellValue("TELEFONO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(4);
            celda.setCellValue("VALOR");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(5);
            celda.setCellValue("SALDO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(6);
            celda.setCellValue("FECHA VENCIMIENTO");
            celda.setCellStyle(cellStyle);
            i = i + 1;
            for (int j = 0; j <= 12; j++) {
                hoja.autoSizeColumn(j);
            }
            while (rs.next()) {
                fila = hoja.createRow(i);
                celda = fila.createCell(0);
                celda.setCellValue(rs.getString("documento"));

                celda = fila.createCell(1);
                celda.setCellValue(rs.getString("numDoc"));

                celda = fila.createCell(2);
                celda.setCellValue(rs.getString("cliente"));

                celda = fila.createCell(3);
                celda.setCellValue(rs.getString("tel1"));

                celda = fila.createCell(4);
                celda.setCellValue(rs.getDouble("valor"));

                celda = fila.createCell(5);
                celda.setCellValue(rs.getDouble("saldo"));

                celda = fila.createCell(6);
                celda.setCellValue(sd2.format(rs.getDate("fecha_limite")));

                i = i + 1;
            }
            break;//vencimientosSeprados
        case "productosSinRotacion":
            hoja = libro.createSheet("productos sin rotacin");
            fila = hoja.createRow(i);
            celda = fila.createCell(0);
            celda.setCellValue("CATEGORIA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(1);
            celda.setCellValue("REFERENCIA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(2);
            celda.setCellValue("MARCA");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(3);
            celda.setCellValue("CODIGO INTERNO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(4);
            celda.setCellValue("PRODUCTO");
            celda.setCellStyle(cellStyle);

            celda = fila.createCell(5);
            celda.setCellValue("FECHA ULTIMO MOVIMIENTO");
            celda.setCellStyle(cellStyle);

            i = i + 1;
            for (int j = 0; j <= 12; j++) {
                hoja.autoSizeColumn(j);
            }
            while (rs.next()) {
                fila = hoja.createRow(i);
                celda = fila.createCell(0);
                celda.setCellValue(rs.getString("nombreCategoria"));

                celda = fila.createCell(1);
                celda.setCellValue(rs.getString("nombreReferencia"));

                celda = fila.createCell(2);
                celda.setCellValue(rs.getString("nombreMarca"));

                celda = fila.createCell(3);
                celda.setCellValue(rs.getString("codigoInterno"));

                celda = fila.createCell(4);
                celda.setCellValue(rs.getString("nomProducto"));

                celda = fila.createCell(5);
                celda.setCellValue(sd2.format(rs.getDate("fechaultimo")));

                i = i + 1;
            }
            break;//ProductosSinRotacion
        }

        /*Escribimos en el libro*/
        libro.write(archi);
        /*Cerramos el flujo de datos*/
        archi.close();
        /*DESCARGAMOS EL ARCHIVO */
        File f;
        f = new File(rutaArchivo);
        int bit;
        InputStream in;
        ServletOutputStream out;
        response.setContentType("application/vnd.ms-excel"); //Tipo de fichero.
        response.setHeader("Content-Disposition", "attachment;filename=\"" + nombreArchivo + "\""); //Configurar cabecera http

        in = new FileInputStream(f);
        out = response.getOutputStream();

        bit = 256;
        while ((bit) >= 0) {
            bit = in.read();
            out.write(bit);
        }

        out.flush();
        out.close();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (con != null) {
                con.close();
            }
            DBConnector.getInstance().closeConnection();
        } catch (Exception e) {
        }
    }

}

From source file:tools.IOHelper.java

public static void plate1ResultSheet(ANAPlate plate, File outputFolder) throws IOException {

    File outputFile = new File(outputFolder, plate.getPlateId() + "_" + TIME + ".xlsx");
    Workbook excelFile = null;//w w  w.  ja  v a 2s .co  m
    //        if (outputFile.exists()) {
    //            try {
    //                excelFile = WorkbookFactory.create(outputFile);
    //            } catch (EncryptedDocumentException ex) {
    //                System.out.println("file with assigned name already exists but is encrypted...");
    //                Logger.getLogger(IOHelper.class.getName()).log(Level.SEVERE, null, ex);
    //            } catch (InvalidFormatException ex) {
    //                Logger.getLogger(IOHelper.class.getName()).log(Level.SEVERE, null, ex);
    //            }
    //        } else {
    //            outputFile.createNewFile();
    //            excelFile = new XSSFWorkbook();
    //        }
    if (outputFile.exists()) {
        outputFile.delete();
    }
    outputFile.createNewFile();
    excelFile = new XSSFWorkbook();
    if (excelFile == null) {
        throw new RuntimeException("fail to create the xlsx file");
    }
    int rowCount = plate.getSampleNumber(); //not including the 2 control samples + "_"+time
    String sheetName = plate.getPlateId();
    //create a working sheet 
    Sheet sheet = excelFile.createSheet(sheetName);
    //starting row & col
    int rowIndex = 0;
    int colIndex = 0;
    int totalCol = 0;
    int pos = 0, neg = 0, all = 0;
    XSSFFont fontTitle = (XSSFFont) excelFile.createFont();
    fontTitle.setFontHeightInPoints((short) 10);
    fontTitle.setFontName("Arial");
    fontTitle.setColor(IndexedColors.GREEN.getIndex());
    fontTitle.setBold(true);
    fontTitle.setItalic(false);
    XSSFCellStyle styleTitle = (XSSFCellStyle) excelFile.createCellStyle();
    styleTitle.setAlignment(CellStyle.ALIGN_CENTER);
    styleTitle.setFont(fontTitle);
    Cell cell0 = sheet.createRow(rowIndex++).createCell(0);
    cell0.setCellValue(plate.getPlateId() + " Summary"); //title
    cell0.setCellStyle(styleTitle);

    //        //optional set Cell Style
    //        CellStyle styleTitle = null;
    //        CellStyle style = null;
    Row row = sheet.createRow(rowIndex++); //names
    Cell column = row.createCell(colIndex++);
    column.setCellValue("Sample ID");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Chip Location");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Result");
    totalCol++;

    //        column = row.createCell(colIndex++);
    //        column.setCellValue("Positivity 0.3P");
    //        totalCol++;
    //        column = row.createCell(colIndex++);
    //        column.setCellValue("Positivity0.275P+0.5N");
    //        totalCol++;

    column = row.createCell(colIndex++);
    column.setCellValue("Signal");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Comments");
    totalCol++;

    for (ANATestResult result : plate.getTestResultList()) {
        row = sheet.createRow(rowIndex++); //data
        colIndex = 0;
        column = row.createCell(colIndex++);
        column.setCellValue(result.getJulien_barcode());
        column = row.createCell(colIndex++);
        column.setCellValue(result.getPillarPosition());
        column = row.createCell(colIndex++);
        if (result.getPositivity() == null) {
            column.setCellValue("Null Result");
        } else {
            if (ANA_Result.POSITIVE.equals(result.getPositivity())) {
                pos++;
            } else if (ANA_Result.NEGATIVE.equals(result.getPositivity())) {
                neg++;
            }
            all++;
            column.setCellValue(result.getPositivity().name());
        } //make sure all fis has not-null pn result

        //            column = row.createCell(colIndex++);
        //            if (result.positivity30 == null) {
        //                column.setCellValue("Null Result");
        //            } else {
        //                if (ANA_Result.POSITIVE.equals(result.positivity30)) {
        ////                    pos++;
        //                } else if (ANA_Result.NEGATIVE.equals(result.positivity30)) {
        ////                    neg++;
        //                }
        ////                all++;
        //                column.setCellValue(result.positivity30.name());
        //            }  //make sure all fis has not-null pn result
        //            column = row.createCell(colIndex++);
        //            if (result.positivityCombined == null) {
        //                column.setCellValue("Null Result");
        //            } else {
        //                if (ANA_Result.POSITIVE.equals(result.positivityCombined)) {
        ////                    pos++;
        //                } else if (ANA_Result.NEGATIVE.equals(result.positivityCombined)) {
        ////                    neg++;
        //                }
        ////                all++;
        //                column.setCellValue(result.positivityCombined.name());
        //            }  //make sure all fis has not-null pn result

        column = row.createCell(colIndex++);
        column.setCellValue(result.getFirstPlateSignal());
        //            if(result.getFirstPlateSignal()<0){
        //                column.setCellValue("ROI exception: unable to get signal for this sample");
        //            }else{
        //                column.setCellValue(result.getFirstPlateSignal());
        //            }
        column = row.createCell(colIndex++); //warning msg concat mthd; merge plateErr to sampErr
        column.setCellValue(result.concatWarningMsgs());
    }
    if (rowIndex - rowCount == 2) {
        for (int i = 0; i < totalCol; i++) {
            sheet.autoSizeColumn(i);
        }
    } else {
        System.out
                .println((rowIndex - 2) + " records are writen into file while " + rowCount + " are expected");
    }
    row = sheet.createRow(rowIndex++); //total
    colIndex = 0;
    column = row.createCell(colIndex++);
    column.setCellValue("all samples");
    column = row.createCell(colIndex++);
    column.setCellValue(all);
    column = row.createCell(colIndex++);
    column.setCellValue("positive samples");
    column = row.createCell(colIndex++);
    column.setCellValue(pos);
    column = row.createCell(colIndex++);
    column.setCellValue("negative samples");
    column = row.createCell(colIndex++);
    column.setCellValue(neg);

    column = row.createCell(colIndex++);
    column = row.createCell(colIndex++);
    column = row.createCell(colIndex++);
    column.setCellValue("PosCtrl");
    column = row.createCell(colIndex++);
    column.setCellValue(plate.getPosCtrl().getFirstPlateSignal());
    column = row.createCell(colIndex++);
    column.setCellValue("NegCtrl");
    column = row.createCell(colIndex++);
    column.setCellValue(plate.getNegCtrlSignal());

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(outputFile.getAbsolutePath());//,true
        excelFile.write(fos);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(IOHelper.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(IOHelper.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(IOHelper.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

From source file:tools.IOHelper.java

public static void plate2ResultSheet(ANAPlate plate, File outputFolder) throws IOException {

    File outputFile = new File(outputFolder, plate.getPlateId() + "_" + TIME + ".xlsx");
    Workbook excelFile = null;//from w w w  . j a va2  s  . c o  m
    if (outputFile.exists()) {
        outputFile.delete();
    }
    outputFile.createNewFile();
    excelFile = new XSSFWorkbook();
    if (excelFile == null) {
        throw new RuntimeException("fail to create the xlsx file");
    }
    int rowCount = plate.getSampleNumber(); //not including the 2 control samples + "_"+time
    String sheetName = plate.getPlateId();
    //create a working sheet 
    Sheet sheet = excelFile.createSheet(sheetName);
    //starting row & col
    int rowIndex = 0;
    int colIndex = 0;
    int totalCol = 0;
    int pos = 0, neg = 0, all = 0;
    XSSFFont fontTitle = (XSSFFont) excelFile.createFont();
    fontTitle.setFontHeightInPoints((short) 10);
    fontTitle.setFontName("Arial");
    fontTitle.setColor(IndexedColors.GREEN.getIndex());
    fontTitle.setBold(true);
    fontTitle.setItalic(false);
    XSSFCellStyle styleTitle = (XSSFCellStyle) excelFile.createCellStyle();
    styleTitle.setAlignment(CellStyle.ALIGN_CENTER);
    styleTitle.setFont(fontTitle);
    Cell cell0 = sheet.createRow(rowIndex++).createCell(0);
    cell0.setCellValue(plate.getPlateId() + " Summary"); //title
    cell0.setCellStyle(styleTitle);

    //        //optional set Cell Style
    //        CellStyle styleTitle = null;
    //        CellStyle style = null;

    //            add column:Sample,Chip Location,Signal,Positivity,sample titer,plate titer, pattern, No of Cells, enableWatershed,comment
    Row row = sheet.createRow(rowIndex++); //names
    Cell column = row.createCell(colIndex++);
    column.setCellValue("Sample ID");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Chip Location");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Signal");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Positivity");
    totalCol++;

    column = row.createCell(colIndex++);
    column.setCellValue("Sample Titer");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Plate Titer");
    totalCol++;

    column = row.createCell(colIndex++);
    column.setCellValue("Pattern");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Number of Cells");
    totalCol++;
    column = row.createCell(colIndex++);
    column.setCellValue("Comments");
    totalCol++;

    for (ANATestResult result : plate.getTestResultList()) {
        row = sheet.createRow(rowIndex++); //data
        colIndex = 0;
        column = row.createCell(colIndex++);
        column.setCellValue(result.getJulien_barcode());
        column = row.createCell(colIndex++);
        column.setCellValue(result.getPillarPosition());
        column = row.createCell(colIndex++);
        column.setCellValue(result.getSecondPlateSignal());
        column = row.createCell(colIndex++);
        if (result.getPositivity() == null) {
            column.setCellValue("Null Result");
        } else {
            if (ANA_Result.POSITIVE.equals(result.getPositivity())) {
                pos++;
            } else if (ANA_Result.NEGATIVE.equals(result.getPositivity())) {
                neg++;
            }
            all++;
            column.setCellValue(result.getPositivity().name());
        } //make sure all fis has not-null pn result

        column = row.createCell(colIndex++);
        if (result.getTiter() != null) {
            column.setCellValue(result.getTiter().name());
        }
        column = row.createCell(colIndex++);
        if (plate.getPosCtrl().getTiter() != null) {
            column.setCellValue(plate.getPosCtrl().getTiter().name());
        }
        //pattern, No of Cells, enableWatershed,comment
        column = row.createCell(colIndex++);
        if (result.getPattern() != null) {
            column.setCellValue(result.getPattern().name());
        }
        column = row.createCell(colIndex++);
        column.setCellValue(result.cellCount());
        column = row.createCell(colIndex++); //warning msg concat mthd; merge plateErr to sampErr
        column.setCellValue(result.concatWarningMsgs());
    }
    if (rowIndex - rowCount == 2) {
        for (int i = 0; i < totalCol; i++) {
            sheet.autoSizeColumn(i);
        }
    } else {
        System.out
                .println((rowIndex - 2) + " records are writen into file while " + rowCount + " are expected");
    }
    row = sheet.createRow(rowIndex++); //total
    colIndex = 0;
    column = row.createCell(colIndex++);
    column.setCellValue("all samples");
    column = row.createCell(colIndex++);
    column.setCellValue(all);
    column = row.createCell(colIndex++);
    column.setCellValue("positive samples");
    column = row.createCell(colIndex++);
    column.setCellValue(pos);
    column = row.createCell(colIndex++);
    column.setCellValue("negative samples");
    column = row.createCell(colIndex++);
    column.setCellValue(neg);

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(outputFile.getAbsolutePath());//,true
        excelFile.write(fos);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(IOHelper.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(IOHelper.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(IOHelper.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

From source file:ua.karazin.math.deansoffice.business.excel.CreateXLS.java

public byte[] CreateXLSfile(Map<Integer, Object[]> data, String FileName)
        throws FileNotFoundException, IOException {

    Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet("Sheet1");

    Set<Integer> keyset = data.keySet();
    int rownum = 0;
    for (Integer key : keyset) {
        Row row = sheet.createRow(rownum++);
        Object[] objArr = data.get(key);
        int cellnum = 0;
        for (Object obj : objArr) {
            Cell cell = row.createCell(cellnum++);
            if (obj instanceof String)
                cell.setCellValue((String) obj);
        }//from  w  ww.  j a v a2 s .  c o m
        int columns = objArr.length;
        for (int j = 0; j < columns; j++) {
            sheet.autoSizeColumn(j);
        }
    }

    FileOutputStream fileOut = new FileOutputStream(
            new File(System.getProperty("com.sun.aas.instanceRoot") + "/docroot/" + FileName + ".xls"));
    wb.write(fileOut);
    fileOut.close();

    Path path = Paths.get(System.getProperty("com.sun.aas.instanceRoot") + "/docroot/" + FileName + ".xls");
    byte[] filedata = Files.readAllBytes(path);
    System.out.println(data);
    return filedata;
}

From source file:uk.co.certait.htmlexporter.writer.excel.ExcelExporter.java

License:Apache License

protected void formatSheet(Sheet sheet) {
    int lastRowWithData = 0;

    for (int i = sheet.getLastRowNum(); i >= 0; --i) {
        if (sheet.getRow(i) != null && sheet.getRow(i).getPhysicalNumberOfCells() > 0) {
            lastRowWithData = i;//  w  w w .  ja v  a2 s. c o m
            break;
        }
    }

    for (int i = 0; i < sheet.getRow(lastRowWithData).getPhysicalNumberOfCells(); ++i) {
        sheet.autoSizeColumn(i);
    }

    for (int i = 0; i < sheet.getRow(sheet.getLastRowNum()).getPhysicalNumberOfCells(); ++i) {
        sheet.setColumnWidth(i, (int) (sheet.getColumnWidth(i) * 1.2));
    }
}

From source file:Usuario.Reportes.ReporteInventario.java

File imprimirInventario(String ruta) throws RemoteException {
    DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd__HH_mm_ss");
    java.util.Date date = new java.util.Date();

    File xls = new File(ruta + "\\Inventario" + dateFormat.format(date) + ".xls");
    if (!xls.exists()) {
        try {/*  www .  j  a v  a2s.  co m*/
            xls.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(Usuario.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    Workbook libro = new HSSFWorkbook();
    FileOutputStream archivo;
    Sheet hoja;
    ArrayList<ItemInventario> todos;
    try {
        archivo = new FileOutputStream(xls);
        hoja = libro.createSheet("Inventario");
        Usuario u = cliente.Cliente.conectarU();
        todos = u.itemInventarioAdmin();
        int i = 0;

        for (int j = 0; j < 8; j++) {
            Row fila = hoja.createRow(i);
            Cell aux;
            if (i == 0) {

                aux = fila.createCell(j);
                aux.setCellValue("Cdigo");
                j++;
                aux = fila.createCell(j);
                aux.setCellValue("Descripcin");
                j++;
                aux = fila.createCell(j);
                aux.setCellValue("Presentacin");
                j++;
                aux = fila.createCell(j);
                aux.setCellValue("Cantidad");
                j++;
                aux = fila.createCell(j);
                aux.setCellValue("Precio");
                j++;
                aux = fila.createCell(j);
                aux.setCellValue("Cert. Calidad");
                j++;
                aux = fila.createCell(j);
                aux.setCellValue("Cump. Especificaciones");
                j++;
                i++;
            }
        }

        for (ItemInventario t : todos) {
            Row fila = hoja.createRow(i);
            Cell aux;
            for (int j = 0; j < 8; j++) {
                aux = fila.createCell(j);
                aux.setCellValue(t.getNumero());
                hoja.autoSizeColumn(j);
                j++;
                aux = fila.createCell(j);
                aux.setCellValue(t.getDescripcion());
                hoja.autoSizeColumn(j);
                j++;
                aux = fila.createCell(j);
                aux.setCellValue(t.getPresentacion());
                hoja.autoSizeColumn(j);
                j++;
                aux = fila.createCell(j);
                aux.setCellValue(t.getCantidad());
                hoja.autoSizeColumn(j);
                j++;
                aux = fila.createCell(j);
                aux.setCellValue(t.getPrecio());
                hoja.autoSizeColumn(j);
                j++;
                aux = fila.createCell(j);
                aux.setCellValue(t.getcCalidad());
                hoja.autoSizeColumn(j);
                j++;
                aux = fila.createCell(j);
                aux.setCellValue(t.getCEsp());
                hoja.autoSizeColumn(j);
                j++;
            }
            i++;
        }

        libro.write(archivo);
        archivo.close();

    } catch (IOException ex) {
        System.out.println("Error funcion\"Imprimir Inventario\"");
        Logger.getLogger(Usuario.class.getName()).log(Level.SEVERE, null, ex);
    }

    return xls;
}

From source file:Usuario.Reportes.verProveedores.java

File imprimirProveedores(String ruta) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd__HH_mm_ss");
    java.util.Date date = new java.util.Date();

    File xls = new File(ruta + "\\ListaProveedores" + dateFormat.format(date) + ".xls");
    if (!xls.exists()) {
        try {/* w  w w.  j av a  2s. com*/
            xls.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(Usuario.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    Workbook libro = new HSSFWorkbook();
    FileOutputStream archivo;
    Sheet hoja;
    ArrayList<proveedor> todos;
    try {
        xls.createNewFile();
        archivo = new FileOutputStream(xls);
        hoja = libro.createSheet("Lista_Proveedores");
        Usuario u = cliente.Cliente.conectarU();
        todos = u.todosProveedores();
        int i = 0;
        for (int j = 0; j < 8; j++) {
            Row fila = hoja.createRow(i);
            Cell aux;
            if (i == 0) {
                aux = fila.createCell(j);
                aux.setCellValue("NIT");
                j++;
                aux = fila.createCell(j);
                aux.setCellValue("Nombre");
                j++;
                aux = fila.createCell(j);
                aux.setCellValue("Direccin");
                j++;
                aux = fila.createCell(j);
                aux.setCellValue("Telfono");
                j++;
                aux = fila.createCell(j);
                aux.setCellValue("Fax");
                j++;
                aux = fila.createCell(j);
                aux.setCellValue("Celular");
                j++;
                aux = fila.createCell(j);
                aux.setCellValue("Correo");
                j++;
                aux = fila.createCell(j);
                aux.setCellValue("Contacto");
                j++;
                i++;
            }
        }

        for (proveedor t : todos) {
            Row fila = hoja.createRow(i);
            Cell aux;
            for (int j = 0; j < 7; j++) {

                aux = fila.createCell(j);
                aux.setCellValue(t.getNIT());
                hoja.autoSizeColumn(j);

                j++;
                aux = fila.createCell(j);
                aux.setCellValue(t.getNombre());
                hoja.autoSizeColumn(j);
                j++;
                aux = fila.createCell(j);
                aux.setCellValue(t.getDireccion());
                hoja.autoSizeColumn(j);
                j++;
                aux = fila.createCell(j);
                aux.setCellValue(t.getTelefono());
                hoja.autoSizeColumn(j);
                j++;
                aux = fila.createCell(j);
                aux.setCellValue(t.getTelefax());
                hoja.autoSizeColumn(j);
                j++;
                aux = fila.createCell(j);
                aux.setCellValue(t.getCelular());
                hoja.autoSizeColumn(j);
                j++;
                aux = fila.createCell(j);
                aux.setCellValue(t.getCorreo());
                hoja.autoSizeColumn(j);
                j++;
                aux = fila.createCell(j);
                aux.setCellValue(t.getContacto());
                hoja.autoSizeColumn(j);
                j++;

            }
            i++;
        }

        libro.write(archivo);
        archivo.close();

    } catch (IOException ex) {
        Logger.getLogger(Usuario.class.getName()).log(Level.SEVERE, null, ex);
    }

    return xls;
}

From source file:vista.ui.VistaGenerarEstadisticas.java

License:Open Source License

/**
 * Este mtodo sirve para crear una nueva hoja de Excel en el libro de
 * trabajo seleccionado, no es trabajo de este mtodo guardar los cambios en
 * el sistema de archivos, slo agrega la hoja de Excel al libro de trabajo
 * en memoria.//from   w  ww  .j a  v  a2 s  . c om
 *
 * @param workbook el objeto Workbook al que se le adjuntar la nueva hoja
 * de Excel
 * @param nombre un String con el nombre que tendr la nueva hoja de Excel
 * @param datos un objeto TablaEstadisticas con los datos y los nombres de
 * columnas que sern agregados a la hoja
 */
private void crearHoja(Workbook workbook, String nombre, TablaEstadisticas datos) {
    //Crear la hoja
    CreationHelper createHelper = workbook.getCreationHelper();
    Sheet nuevaHoja = workbook.createSheet(WorkbookUtil.createSafeSheetName(nombre));

    //Escribir los nombres de las columnas en la hoja
    Row headers = nuevaHoja.createRow(0);

    for (int i = 0; i < datos.getColumnCount(); i++) {
        //Crear headers con estilo
        Cell cell = headers.createCell(i);
        CellStyle style = crearEstiloCelda(workbook, IndexedColors.BLACK, CellStyle.BORDER_THICK,
                CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);

        cell.setCellValue(createHelper.createRichTextString(datos.getColumnName(i)));
        cell.setCellStyle(style);

        //Autoajustar
        nuevaHoja.autoSizeColumn(i);
    }

    //Escribir los datos de la tabla en la hoja
    for (int i = 0; i < datos.getRowCount(); i++) {
        Row fila = nuevaHoja.createRow(i + 1);
        CellStyle style = crearEstiloCelda(workbook, IndexedColors.GREEN, CellStyle.BORDER_THIN, (short) -18,
                (short) -18);

        for (int j = 0; j < datos.getColumnCount(); j++) {
            Cell cell = fila.createCell(j);

            //Escribir de acuerdo al tipo de dato
            if (datos.getColumnClass(j).equals(String.class)) {
                cell.setCellValue(createHelper.createRichTextString(datos.getValueAt(i, j).toString()));

            } else if (datos.getColumnClass(j).equals(Integer.class)) {
                cell.setCellValue((int) datos.getValueAt(i, j));

            } else if (datos.getColumnClass(j).equals(Double.class)) {
                cell.setCellValue((double) datos.getValueAt(i, j));

            } else if (datos.getColumnClass(j).equals(Turno.class)) {
                cell.setCellValue(((Turno) datos.getValueAt(i, j)).toString());

            }

            //Agregar el estilo
            cell.setCellStyle(style);
        }
    }

}

From source file:workbench.db.exporter.XlsRowDataConverter.java

License:Apache License

private void writeInfoSheet() {
    Sheet info = workbook.getSheet(INFO_SHEETNAME);

    if (info == null) {
        info = workbook.createSheet(INFO_SHEETNAME);
        Row headRow = info.createRow(0);
        Cell cell = headRow.createCell(0);
        setCellValueAndStyle(cell, ResourceMgr.getString("TxtSheet"), true, false, 0);
        cell = headRow.createCell(1);//from  w ww  .j a  va  2s . c om
        setCellValueAndStyle(cell, "SQL", true, false, 1);
    } else {
        // move the info sheet to the end
        int count = workbook.getNumberOfSheets();
        workbook.setSheetOrder(info.getSheetName(), count - 1);
    }

    int rowNum = info.getLastRowNum() + 1;

    Row infoRow = info.createRow(rowNum);

    Cell name = infoRow.createCell(0);
    CellStyle nameStyle = workbook.createCellStyle();
    nameStyle.setAlignment(CellStyle.ALIGN_LEFT);
    nameStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    nameStyle.setWrapText(false);
    name.setCellValue(sheet.getSheetName());
    name.setCellStyle(nameStyle);
    info.autoSizeColumn(0);

    Cell sqlCell = infoRow.createCell(1);
    CellStyle sqlStyle = workbook.createCellStyle();
    sqlStyle.setAlignment(CellStyle.ALIGN_LEFT);
    sqlStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    sqlStyle.setWrapText(false);

    RichTextString s = workbook.getCreationHelper().createRichTextString(generatingSql);
    sqlCell.setCellValue(s);
    sqlCell.setCellStyle(sqlStyle);
}