Example usage for org.apache.poi.hssf.usermodel HSSFDataFormat getFormat

List of usage examples for org.apache.poi.hssf.usermodel HSSFDataFormat getFormat

Introduction

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

Prototype

public String getFormat(short index) 

Source Link

Document

get the format string that matches the given format index

Usage

From source file:hr.restart.swing.raExtendedTable.java

License:Apache License

public void exportToXLS(File output) {
    String fname = output.getName();
    if (!fname.endsWith("xls") && fname.indexOf('.') < 0)
        output = new File(output.getParentFile(), fname + ".xls");
    System.out.println("exporting to XLS");
    HSSFWorkbook wb = new HSSFWorkbook();

    HSSFDataFormat df = wb.createDataFormat();

    String fontFamily = frmParam.getParam("sisfun", "excelFont", "Arial", "Font za export u Excel", true);
    if (fontFamily == null || fontFamily.length() == 0)
        fontFamily = "Arial";

    int fontSize = 10;
    String fontSizeTx = frmParam.getParam("sisfun", "excelFontSize", "10",
            "Veliina fonta za export u Excel, u tokama", true);
    if (fontSizeTx != null && Aus.getNumber(fontSizeTx) >= 6 && Aus.getNumber(fontSizeTx) <= 72)
        fontSize = Aus.getNumber(fontSizeTx);

    HSSFFont font = wb.createFont();//from  ww w  .j  a v  a  2  s  .  c  o  m
    font.setFontName(fontFamily);
    font.setFontHeightInPoints((short) fontSize);

    HSSFFont fontTitle = wb.createFont();
    fontTitle.setFontName(fontFamily);
    fontTitle.setFontHeightInPoints((short) (fontSize * 1.8));

    HSSFFont fontSubtitle = wb.createFont();
    fontSubtitle.setFontName(fontFamily);
    fontSubtitle.setFontHeightInPoints((short) (fontSize * 1.5));

    HSSFCellStyle csHeader = wb.createCellStyle();
    csHeader.setFont(font);
    csHeader.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
    csHeader.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    csHeader.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    csHeader.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    csHeader.setDataFormat(df.getFormat("text"));

    HSSFCellStyle csFooter = wb.createCellStyle();
    csFooter.setFont(font);
    csFooter.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
    csFooter.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    csFooter.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    csFooter.setAlignment(HSSFCellStyle.ALIGN_LEFT);
    csFooter.setDataFormat(df.getFormat("text"));

    HSSFCellStyle csFooterNum2 = wb.createCellStyle();
    csFooterNum2.setFont(font);
    csFooterNum2.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
    csFooterNum2.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    csFooterNum2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    csFooterNum2.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
    csFooterNum2.setDataFormat(df.getFormat("#,##0.00"));

    HSSFCellStyle csFooterNum = wb.createCellStyle();
    csFooterNum.setFont(font);
    csFooterNum.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
    csFooterNum.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    csFooterNum.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    csFooterNum.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
    csFooterNum.setDataFormat(df.getFormat("#"));

    HSSFCellStyle csDate = wb.createCellStyle();
    csDate.setFont(font);
    csDate.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    csDate.setDataFormat(df.getFormat("dd.mm.yyyy"));

    HSSFCellStyle csTitle = wb.createCellStyle();
    csTitle.setFont(fontTitle);
    csTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    csTitle.setDataFormat(df.getFormat("text"));

    HSSFCellStyle csSubtitle = wb.createCellStyle();
    csSubtitle.setFont(fontSubtitle);
    csSubtitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    csSubtitle.setDataFormat(df.getFormat("text"));

    HSSFCellStyle csNum2 = wb.createCellStyle();
    csNum2.setFont(font);
    csNum2.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
    csNum2.setDataFormat(df.getFormat("#,##0.00"));

    HSSFCellStyle csNum3 = wb.createCellStyle();
    csNum3.setFont(font);
    csNum3.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
    csNum3.setDataFormat(df.getFormat("#,##0.000"));

    HSSFCellStyle csNum = wb.createCellStyle();
    csNum.setFont(font);
    csNum.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
    csNum.setDataFormat(df.getFormat("#.#"));

    HSSFCellStyle csInt = wb.createCellStyle();
    csInt.setFont(font);
    csInt.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
    csInt.setDataFormat(df.getFormat("#"));

    HSSFCellStyle csText = wb.createCellStyle();
    csText.setFont(font);
    csText.setAlignment(HSSFCellStyle.ALIGN_LEFT);
    csText.setDataFormat(df.getFormat("text"));

    repDynamicProvider dp = repDynamicProvider.getInstance();
    boolean sums = dp.hasSumRow();
    int cols = getColumnModel().getColumnCount();
    int rows = getRowCount() - (sums ? 1 : 0);

    HSSFSheet sheet = wb.createSheet();
    HSSFRow row;
    HSSFCell cell;

    short cRow = 0;

    // header and title
    row = sheet.createRow(cRow = 0);
    cell = row.createCell((short) 0);
    cell.setCellStyle(csText);
    if (cols > 1)
        sheet.addMergedRegion(new Region(cRow, (short) 0, cRow, (short) (cols - 1)));
    cell.setEncoding(wb.ENCODING_UTF_16);
    cell.setCellValue(repMemo.getrepMemo().getOneLine());

    row = sheet.createRow(++cRow);
    cell = row.createCell((short) 0);
    cell.setCellStyle(csTitle);
    if (cols > 1)
        sheet.addMergedRegion(new Region(cRow, (short) 0, cRow, (short) (cols - 1)));
    cell.setEncoding(wb.ENCODING_UTF_16);
    if (dp.getTitle().length() > 0)
        cell.setCellValue(dp.getTitle().substring(1));
    else
        cell.setCellValue("");

    if (dp.getSubtitle().length() > 0) {
        row = sheet.createRow(++cRow);
        cell = row.createCell((short) 0);
        cell.setCellStyle(csSubtitle);
        if (cols > 1)
            sheet.addMergedRegion(new Region(cRow, (short) 0, cRow, (short) (cols - 1)));
        cell.setEncoding(wb.ENCODING_UTF_16);
        cell.setCellValue(dp.getSubtitle().substring(1));
    }

    for (short c = 0; c < cols; c++)
        sheet.setColumnWidth(c, (short) (getColumnModel().getColumn(c).getWidth() * 40));

    // sections
    row = sheet.createRow(++cRow);
    int secRow = 0, firstRow = 0;

    for (int r = 0; r < rows; r++) {
        if (r == 0) {
            row = sheet.createRow(++cRow);
            for (short c = 0; c < cols; c++) {
                cell = row.createCell(c);
                cell.setCellStyle(csHeader);
                cell.setEncoding(wb.ENCODING_UTF_16);
                cell.setCellValue(getColumnModel().getColumn(c).getHeaderValue().toString());
            }
            if (firstRow == 0)
                firstRow = cRow;
            secRow = cRow;
        }
        row = sheet.createRow(++cRow);
        for (short c = 0; c < cols; c++) {
            cell = row.createCell(c);
            Object o = getValueAt(r, c);
            if (o instanceof Number) {
                if (o instanceof BigDecimal) {
                    BigDecimal bd = (BigDecimal) o;
                    if (bd.scale() == 2)
                        cell.setCellStyle(csNum2);
                    else if (bd.scale() == 3)
                        cell.setCellStyle(csNum3);
                    else
                        cell.setCellStyle(csNum);
                    cell.setCellValue(bd.doubleValue());
                } else {
                    String t = dp.getValueAt(r, c);
                    if (Aus.isDigit(t)) {
                        cell.setCellStyle(csInt);
                        cell.setCellValue(((Number) o).doubleValue());
                    } else {
                        cell.setCellStyle(csText);
                        cell.setEncoding(wb.ENCODING_UTF_16);
                        cell.setCellValue(t);
                    }
                }
            } else if (o instanceof Date) {
                cell.setCellStyle(csDate);
                cell.setCellValue((Date) o);
            } else {
                cell.setCellStyle(csText);
                cell.setEncoding(wb.ENCODING_UTF_16);
                cell.setCellValue(dp.getValueAt(r, c));
            }
        }
    }
    System.out.println("sums " + sums);
    if (sums) {
        int non = 0;
        while (non < cols && dp.getValueAt(getRowCount() - 1, non).trim().length() == 0)
            ++non;
        if (non < cols) {
            System.out.println("creating row " + non);
            row = sheet.createRow(++cRow);

            if (non > 0) {
                cell = row.createCell((short) 0);
                cell.setCellStyle(csFooter);
                cell.setEncoding(wb.ENCODING_UTF_16);
                cell.setCellValue("U K U P N O");
                if (non > 1)
                    sheet.addMergedRegion(new Region(cRow, (short) 0, cRow, (short) (non - 1)));
            }
            for (short c = (short) non; c < cols; c++) {
                cell = row.createCell(c);
                Object o = getValueAt(rows - 1, c);
                if ((o instanceof BigDecimal) && ((BigDecimal) o).scale() == 2)
                    cell.setCellStyle(csFooterNum2);
                else
                    cell.setCellStyle(csFooterNum);
                if (dp.getValueAt(getRowCount() - 1, c).trim().length() != 0)
                    cell.setCellFormula("SUBTOTAL(9;" + xlsRange(firstRow + 1, cRow, c) + ")");
                else
                    cell.setCellValue("");
            }
        }
    }
    FileOutputStream out = null;

    try {
        out = new FileOutputStream(output);
        wb.write(out);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null)
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
}

From source file:include.excel_export.XlsWriter.java

License:Open Source License

private void initCellTyles(HSSFWorkbook wb) {
    HSSFDataFormat df = wb.createDataFormat();
    short doubleFormat = df.getFormat("#,##0.00");
    short intFormat = df.getFormat("0");
    short dateFormat = df.getFormat("yyyy-mm-dd");
    doubleCellStyle = wb.createCellStyle();//? 
    doubleCellStyle.setDataFormat(doubleFormat);
    intCellStyle = wb.createCellStyle();
    intCellStyle.setDataFormat(intFormat);
    dateCellStyle = wb.createCellStyle();
    dateCellStyle.setDataFormat(dateFormat);
}

From source file:include.excel_import.Outter.java

License:Open Source License

private String getCellType(HSSFCell hssfcell) {
    HSSFDataFormat hssfdataformat = wb.createDataFormat();
    HSSFCellStyle hssfcellstyle = hssfcell.getCellStyle();
    short word0 = hssfcellstyle.getDataFormat();
    String s = hssfdataformat.getFormat(word0);
    String s1 = "";
    switch (hssfcell.getCellType()) {
    case 0: // '\0'
        if (s.indexOf("0_") == 0 || s.indexOf("0;") == 0 || s.indexOf("#,##0_") == 0 || s.indexOf("#,##0;") == 0
                || s.equals("0")) {
            s1 = "INT";
            break;
        }/*  w  w  w  .ja  va 2s. co m*/
        if (s.equals("yyyy\\-mm\\-dd") || s.equals("yyyy-mm-dd") || s.equals("yyyy/mm/dd") || s.equals("m/d/yy")
                || s.equals("0x1f")) {
            s1 = "DATE";
            break;
        }
        if (s.indexOf("#,##0.") == 0 || s.indexOf("0.0") == 0) {
            s1 = "DOUBLE";
            break;
        }
        if (s.equals("General"))
            s1 = "INT1";
        else
            s1 = "NUMBERIC";
        break;

    case 1: // '\001'
        if (s.equals("General")) {
            s1 = "STRING";
            break;
        }
        if (s.equals("@")) {
            s1 = "STRING";
            break;
        }
        if (s.indexOf("0_") == 0 || s.indexOf("0;") == 0 || s.indexOf("#,##0_") == 0 || s.indexOf("#,##0;") == 0
                || s.equals("0")) {
            s1 = "INT";
            break;
        }
        if (s.indexOf("#,##0.") == 0 || s.indexOf("0.0") == 0)
            s1 = "DOUBLE";
        else
            System.out.println(s);
        break;

    case 3: // '\003'
        s1 = "BLANK";
        break;

    case 2: // '\002'
        s1 = "FORMULA";
        break;

    case 5: // '\005'
        s1 = "ERROR";
        break;

    case 4: // '\004'
    default:
        s1 = "UNKNOWN";
        break;
    }
    return s1;
}

From source file:neg.JRViewerComercial.java

License:Open Source License

private void btnExcelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcelActionPerformed

    String queryInf = this.jasperPrint.getProperty("query");

    try {//from w  ww.j a v  a 2  s  .c  o m
        // Se crea el libro excel
        HSSFWorkbook libro = new HSSFWorkbook();
        //Se crea la hoja
        HSSFSheet hoja = libro.createSheet("Informe");
        //Numero de fila de la hoja Excel
        int num_fila = 1;
        crearCabeceraHojaExcel(libro, hoja);

        HSSFCellStyle cs2 = libro.createCellStyle();

        cs2.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        cs2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cs2.setBottomBorderColor(HSSFColor.BLACK.index);
        cs2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cs2.setLeftBorderColor(HSSFColor.BLACK.index);
        cs2.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cs2.setRightBorderColor(HSSFColor.BLACK.index);
        cs2.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cs2.setTopBorderColor(HSSFColor.BLACK.index);

        HSSFCellStyle cs3 = libro.createCellStyle();
        HSSFDataFormat format = libro.createDataFormat();
        cs3.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
        cs3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cs3.setBottomBorderColor(HSSFColor.BLACK.index);
        cs3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cs3.setLeftBorderColor(HSSFColor.BLACK.index);
        cs3.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cs3.setRightBorderColor(HSSFColor.BLACK.index);
        cs3.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cs3.setTopBorderColor(HSSFColor.BLACK.index);
        cs3.setDataFormat(format.getFormat("#,##0"));

        //Datos
        ResultSet rs = CSDesktop.datos.select(queryInf);

        crearFilaHojaExcel(libro, hoja, num_fila, rs, cs2, cs3);
        FileOutputStream elFichero = null;
        elFichero = new FileOutputStream("c:\\AplicacionCarSet\\informe_comercial.xls");
        libro.write(elFichero);
        elFichero.close();
        elFichero.flush();
        String property = "java.io.tmpdir";
        String tempDir = System.getProperty(property);
        System.out.println("OS current temporary directory is " + tempDir);
        String file = new String("C:\\AplicacionCarSet\\informe_comercial.xls");
        Process p = Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + file);

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

    //System.out.println("Vamoooooos: "+queryInf);

}

From source file:neg.JRViewerComercial.java

License:Open Source License

private static void crearFilaHojaExcel(HSSFWorkbook libro, HSSFSheet hoja, int num_fila, ResultSet rs,
        HSSFCellStyle cs2, HSSFCellStyle cs3) throws SQLException, UnknownHostException {
    HSSFRow fila = null;/*  w w w . j a v a2s . co  m*/
    HSSFCell celda = null;
    HSSFRichTextString texto = null;
    int num_fila_aux = 2;
    while (rs.next()) {
        // Se crea una fila dentro de la hoja
        fila = hoja.createRow(num_fila);

        //Celda del Tarifa Cliente
        HSSFDataFormat format = libro.createDataFormat();

        HSSFCellStyle style = libro.createCellStyle();
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setRightBorderColor(HSSFColor.BLACK.index);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setTopBorderColor(HSSFColor.BLACK.index);

        style.setDataFormat(format.getFormat("#,##0"));

        //Celda de cliente
        celda = fila.createCell((short) 0);
        celda.setCellStyle(cs2);
        String cliente = rs.getString("cl_nombre");
        texto = new HSSFRichTextString(cliente);
        celda.setCellStyle(cs3);
        celda.setCellValue(texto);

        celda.setCellStyle(style);

        //Celda de enero
        celda = fila.createCell((short) 1);
        celda.setCellStyle(cs3);
        double enero = rs.getDouble("enero");
        celda.setCellStyle(cs3);
        celda.setCellValue(enero);
        style.setDataFormat(format.getFormat("00"));

        //Celda de febrero
        celda = fila.createCell((short) 2);
        double febrero = rs.getDouble("febrero");
        celda.setCellStyle(cs3);
        celda.setCellValue(febrero);
        style.setDataFormat(format.getFormat("00"));

        //Celda de marzo
        celda = fila.createCell((short) 3);
        celda.setCellStyle(cs2);
        double marzo = rs.getDouble("marzo");
        celda.setCellStyle(cs3);
        celda.setCellValue(marzo);
        style.setDataFormat(format.getFormat("00"));

        //Celda de abril
        celda = fila.createCell((short) 4);
        double abril = rs.getDouble("abril");
        celda.setCellStyle(cs3);
        celda.setCellValue(abril);
        style.setDataFormat(format.getFormat("00"));

        //Celda de mayo
        celda = fila.createCell((short) 5);
        double mayo = rs.getDouble("mayo");
        celda.setCellStyle(cs3);
        celda.setCellValue(mayo);
        style.setDataFormat(format.getFormat("00"));

        //Celda de junio
        celda = fila.createCell((short) 6);
        double junio = rs.getDouble("junio");
        celda.setCellStyle(cs3);
        celda.setCellValue(junio);

        //Celda del julio
        celda = fila.createCell((short) 7);
        double julio = rs.getDouble("julio");
        celda.setCellStyle(cs3);
        celda.setCellValue(julio);
        style.setDataFormat(format.getFormat("00"));

        //Celda de agosto
        celda = fila.createCell((short) 8);
        celda.setCellStyle(cs2);
        double agosto = rs.getDouble("agosto");
        celda.setCellStyle(cs3);
        celda.setCellValue(agosto);
        style.setDataFormat(format.getFormat("00"));

        //Celda de septiembre
        celda = fila.createCell((short) 9);
        double septiembre = rs.getDouble("septiembre");
        celda.setCellStyle(cs3);
        celda.setCellValue(septiembre);
        style.setDataFormat(format.getFormat("00"));

        //Celda de octubre
        celda = fila.createCell((short) 10);
        celda.setCellStyle(cs2);
        double octubre = rs.getDouble("octubre");
        celda.setCellStyle(cs3);
        celda.setCellValue(octubre);
        style.setDataFormat(format.getFormat("00"));

        //Celda de noviembre
        celda = fila.createCell((short) 11);
        double noviembre = rs.getDouble("noviembre");
        celda.setCellStyle(cs3);
        celda.setCellValue(noviembre);
        style.setDataFormat(format.getFormat("00"));

        //Celda de diciembre
        celda = fila.createCell((short) 12);
        celda.setCellStyle(cs2);
        double diciembre = rs.getDouble("diciembre");
        celda.setCellStyle(cs3);
        celda.setCellValue(diciembre);
        style.setDataFormat(format.getFormat("00"));

        //Celda de la tarifa de cliente
        celda = fila.createCell((short) 13);
        celda.setCellValue(rs.getDouble("ta_cliente"));

        //Celda de margen
        celda = fila.createCell((short) 14);
        double t_cliente = Double.parseDouble(rs.getString("ta_cliente"));
        double t_proveedor = Double.parseDouble(rs.getString("ta_proveedor"));
        double margen = t_cliente - t_proveedor;
        style.setDataFormat(format.getFormat("00.00"));
        celda.setCellValue(margen);

        //Celda de MG %
        celda = fila.createCell((short) 15);
        String porCientoPedido = (!rs.getString("ta_cliente").equals("0"))
                ? new java.text.DecimalFormat("#,##0").format(((t_cliente - t_proveedor) / t_cliente) * 100)
                        .concat("% ")
                : "0% ";
        texto = new HSSFRichTextString(porCientoPedido);
        celda.setCellStyle(cs3);
        celda.setCellValue(texto);

        //Celda de mg pedido
        celda = fila.createCell((short) 16);
        double num_pedidos = Double.parseDouble(rs.getString("num_pedido"));
        style.setDataFormat(format.getFormat("00"));
        double mg_pedido = ((t_cliente - t_proveedor) / num_pedidos);
        celda.setCellStyle(cs3);
        celda.setCellValue(mg_pedido);

        //Celda del num. pedido
        celda = fila.createCell((short) 17);
        int num_pedido = rs.getInt("num_pedido");
        celda.setCellStyle(cs3);
        celda.setCellValue(num_pedido);

        //Se incrementa el numero de fila
        num_fila++;
        num_fila_aux++;
    }
}

From source file:neg.JRViewerComercialProveedor.java

License:Open Source License

private void btnExcelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcelActionPerformed

    String queryInf = this.jasperPrint.getProperty("query");

    try {/*from   w w  w. ja va2 s.c  o  m*/
        // Se crea el libro excel
        HSSFWorkbook libro = new HSSFWorkbook();
        //Se crea la hoja
        HSSFSheet hoja = libro.createSheet("Informe");
        //Numero de fila de la hoja Excel
        int num_fila = 1;
        crearCabeceraHojaExcel(libro, hoja);

        HSSFCellStyle cs2 = libro.createCellStyle();

        cs2.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        cs2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cs2.setBottomBorderColor(HSSFColor.BLACK.index);
        cs2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cs2.setLeftBorderColor(HSSFColor.BLACK.index);
        cs2.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cs2.setRightBorderColor(HSSFColor.BLACK.index);
        cs2.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cs2.setTopBorderColor(HSSFColor.BLACK.index);

        HSSFCellStyle cs3 = libro.createCellStyle();
        HSSFDataFormat format = libro.createDataFormat();
        cs3.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
        cs3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cs3.setBottomBorderColor(HSSFColor.BLACK.index);
        cs3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cs3.setLeftBorderColor(HSSFColor.BLACK.index);
        cs3.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cs3.setRightBorderColor(HSSFColor.BLACK.index);
        cs3.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cs3.setTopBorderColor(HSSFColor.BLACK.index);
        cs3.setDataFormat(format.getFormat("#,##0"));

        //Datos
        ResultSet rs = CSDesktop.datos.select(queryInf);

        crearFilaHojaExcel(libro, hoja, num_fila, rs, cs2, cs3);
        FileOutputStream elFichero = null;
        elFichero = new FileOutputStream("c:\\AplicacionCarSet\\informe_comercial_proveedor.xls");
        libro.write(elFichero);
        elFichero.close();
        elFichero.flush();
        String property = "java.io.tmpdir";
        String tempDir = System.getProperty(property);
        System.out.println("OS current temporary directory is " + tempDir);
        String file = new String("C:\\AplicacionCarSet\\informe_comercial_proveedor.xls");
        Process p = Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + file);

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

    //System.out.println("Vamoooooos: "+queryInf);

}

From source file:neg.JRViewerComercialProveedor.java

License:Open Source License

private static void crearFilaHojaExcel(HSSFWorkbook libro, HSSFSheet hoja, int num_fila, ResultSet rs,
        HSSFCellStyle cs2, HSSFCellStyle cs3) throws SQLException, UnknownHostException {
    HSSFRow fila = null;/*  w  ww. j  a  v a2  s  .  c om*/
    HSSFCell celda = null;
    HSSFRichTextString texto = null;
    int num_fila_aux = 2;
    while (rs.next()) {
        // Se crea una fila dentro de la hoja
        fila = hoja.createRow(num_fila);

        //Celda del Tarifa Cliente
        HSSFDataFormat format = libro.createDataFormat();

        HSSFCellStyle style = libro.createCellStyle();
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setRightBorderColor(HSSFColor.BLACK.index);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setTopBorderColor(HSSFColor.BLACK.index);

        style.setDataFormat(format.getFormat("#,##0"));

        //Celda de Proveedor
        celda = fila.createCell((short) 0);
        celda.setCellStyle(cs2);
        String proveedor = rs.getString("pr_nombre_fiscal");
        texto = new HSSFRichTextString(proveedor);
        celda.setCellStyle(cs3);
        celda.setCellValue(texto);

        celda.setCellStyle(style);

        //Celda de enero
        celda = fila.createCell((short) 1);
        celda.setCellStyle(cs3);
        double enero = rs.getDouble("enero_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(enero);
        style.setDataFormat(format.getFormat("00"));

        //Celda de febrero
        celda = fila.createCell((short) 2);
        double febrero = rs.getDouble("febrero_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(febrero);
        style.setDataFormat(format.getFormat("00"));

        //Celda de marzo
        celda = fila.createCell((short) 3);
        celda.setCellStyle(cs2);
        double marzo = rs.getDouble("marzo_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(marzo);
        style.setDataFormat(format.getFormat("00"));

        //Celda de abril
        celda = fila.createCell((short) 4);
        double abril = rs.getDouble("abril_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(abril);
        style.setDataFormat(format.getFormat("00"));

        //Celda de mayo
        celda = fila.createCell((short) 5);
        double mayo = rs.getDouble("mayo_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(mayo);
        style.setDataFormat(format.getFormat("00"));

        //Celda de junio
        celda = fila.createCell((short) 6);
        double junio = rs.getDouble("junio_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(junio);

        //Celda del julio
        celda = fila.createCell((short) 7);
        double julio = rs.getDouble("julio_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(julio);
        style.setDataFormat(format.getFormat("00"));

        //Celda de agosto
        celda = fila.createCell((short) 8);
        celda.setCellStyle(cs2);
        double agosto = rs.getDouble("agosto_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(agosto);
        style.setDataFormat(format.getFormat("00"));

        //Celda de septiembre
        celda = fila.createCell((short) 9);
        double septiembre = rs.getDouble("septiembre_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(septiembre);
        style.setDataFormat(format.getFormat("00"));

        //Celda de octubre
        celda = fila.createCell((short) 10);
        celda.setCellStyle(cs2);
        double octubre = rs.getDouble("octubre_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(octubre);
        style.setDataFormat(format.getFormat("00"));

        //Celda de noviembre
        celda = fila.createCell((short) 11);
        double noviembre = rs.getDouble("noviembre_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(noviembre);
        style.setDataFormat(format.getFormat("00"));

        //Celda de diciembre
        celda = fila.createCell((short) 12);
        celda.setCellStyle(cs2);
        double diciembre = rs.getDouble("diciembre_pr");
        celda.setCellStyle(cs3);
        celda.setCellValue(diciembre);
        style.setDataFormat(format.getFormat("00"));

        //Celda de la tarifa de Proveedor
        celda = fila.createCell((short) 13);
        celda.setCellValue(rs.getDouble("ta_proveedor"));

        //Celda de % turismo
        celda = fila.createCell((short) 14);
        celda.setCellStyle(cs3);
        double t_proveedor = Double.parseDouble(rs.getString("ta_proveedor"));
        double factor = Integer.parseInt(rs.getString("factor"));

        int num_pedido = Integer.parseInt(rs.getString("num_pedido"));
        String turismo = (!rs.getString("factor").equals("0"))
                ? new java.text.DecimalFormat("#,##0").format((factor / num_pedido) * 100).concat(" % ")
                : "0 % ";
        style.setDataFormat(format.getFormat("00.00"));
        celda.setCellValue(turismo);

        //Celda de % funciona
        celda = fila.createCell((short) 15);
        celda.setCellStyle(cs3);
        double funciona = Integer.parseInt(rs.getString("estado_ve"));

        String estado_ve = (!rs.getString("estado_ve").equals("0"))
                ? new java.text.DecimalFormat("#,##0").format((funciona / num_pedido) * 100).concat(" % ")
                : "0 % ";
        celda.setCellStyle(cs3);
        celda.setCellValue(estado_ve);

        //Celda % Gra Unitaria
        celda = fila.createCell((short) 16);
        celda.setCellStyle(cs3);
        double soporte = Integer.parseInt(rs.getString("soporte"));

        String grua = (!rs.getString("soporte").equals("0"))
                ? new java.text.DecimalFormat("#,##0").format((soporte / num_pedido) * 100).concat(" % ")
                : "0 % ";

        celda.setCellStyle(cs3);
        celda.setCellValue(grua);

        //Celda del num. pedido
        celda = fila.createCell((short) 17);
        celda.setCellStyle(cs3);
        celda.setCellValue(num_pedido);

        //Se incrementa el numero de fila
        num_fila++;
        num_fila_aux++;
    }
}

From source file:neg.JRViewerInformeUnitario.java

License:Open Source License

private static void crearFilaHojaExcel(HSSFWorkbook libro, HSSFSheet hoja, int num_fila, ResultSet rs,
        HSSFCellStyle cs2, HSSFCellStyle cs3) throws SQLException, UnknownHostException {
    HSSFRow fila = null;//w  w  w .  ja v  a2 s  .co  m
    HSSFCell celda = null;
    HSSFRichTextString texto = null;
    int num_fila_aux = 2;

    for (int i = 0; i < 4; i++) {
        //System.out.println();
        /*}
        while (rs.next())
        {*/
        if (i == 0) {
            // Se crea una fila dentro de la hoja
            fila = hoja.createRow(num_fila);

            //Celda de ttulo
            celda = fila.createCell((short) 0);
            celda.setCellStyle(cs2);
            texto = new HSSFRichTextString("Facturacin");
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de enero
            celda = fila.createCell((short) 1);
            String enero = rs.getString("enero");
            texto = new HSSFRichTextString(enero);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de febrero
            celda = fila.createCell((short) 2);
            String febrero = rs.getString("febrero");
            texto = new HSSFRichTextString(febrero);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de marzo
            celda = fila.createCell((short) 3);
            celda.setCellStyle(cs2);
            String marzo = rs.getString("marzo");
            texto = new HSSFRichTextString(marzo);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de abril
            celda = fila.createCell((short) 4);
            String abril = rs.getString("abril");
            texto = new HSSFRichTextString(abril);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de mayo
            celda = fila.createCell((short) 5);
            celda.setCellStyle(cs2);
            String mayo = rs.getString("mayo");
            texto = new HSSFRichTextString(mayo);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de junio
            celda = fila.createCell((short) 6);
            String junio = rs.getString("junio");
            texto = new HSSFRichTextString(junio);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda del julio
            celda = fila.createCell((short) 7);
            String julio = rs.getString("julio");
            texto = new HSSFRichTextString(julio);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de agosto
            celda = fila.createCell((short) 8);
            celda.setCellStyle(cs2);
            String agosto = rs.getString("agosto");
            texto = new HSSFRichTextString(agosto);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de septiembre
            celda = fila.createCell((short) 9);
            String septiembre = rs.getString("septiembre");
            texto = new HSSFRichTextString(septiembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de octubre
            celda = fila.createCell((short) 10);
            celda.setCellStyle(cs2);
            String octubre = rs.getString("octubre");
            texto = new HSSFRichTextString(octubre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de noviembre
            celda = fila.createCell((short) 11);
            String noviembre = rs.getString("noviembre");
            texto = new HSSFRichTextString(noviembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de diciembre
            celda = fila.createCell((short) 12);
            celda.setCellStyle(cs2);
            String diciembre = rs.getString("diciembre");
            texto = new HSSFRichTextString(diciembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

        } else if (i == 1) {
            // Se crea una fila dentro de la hoja
            fila = hoja.createRow(num_fila);

            //Celda de ttulo
            celda = fila.createCell((short) 0);
            celda.setCellStyle(cs2);
            texto = new HSSFRichTextString("Margen pedido");
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de enero
            celda = fila.createCell((short) 1);
            String enero = rs.getString("mg_pe_enero");
            texto = new HSSFRichTextString(enero);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de febrero
            celda = fila.createCell((short) 2);
            String febrero = rs.getString("mg_pe_febrero");
            texto = new HSSFRichTextString(febrero);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de marzo
            celda = fila.createCell((short) 3);
            celda.setCellStyle(cs2);
            String marzo = rs.getString("mg_pe_marzo");
            texto = new HSSFRichTextString(marzo);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de abril
            celda = fila.createCell((short) 4);
            String abril = rs.getString("mg_pe_abril");
            texto = new HSSFRichTextString(abril);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de mayo
            celda = fila.createCell((short) 5);
            celda.setCellStyle(cs2);
            String mayo = rs.getString("mg_pe_mayo");
            texto = new HSSFRichTextString(mayo);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de junio
            celda = fila.createCell((short) 6);
            String junio = rs.getString("mg_pe_junio");
            texto = new HSSFRichTextString(junio);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda del julio
            celda = fila.createCell((short) 7);
            String julio = rs.getString("mg_pe_julio");
            texto = new HSSFRichTextString(julio);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de agosto
            celda = fila.createCell((short) 8);
            celda.setCellStyle(cs2);
            String agosto = rs.getString("mg_pe_agosto");
            texto = new HSSFRichTextString(agosto);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de septiembre
            celda = fila.createCell((short) 9);
            String septiembre = rs.getString("mg_pe_septiembre");
            texto = new HSSFRichTextString(septiembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de octubre
            celda = fila.createCell((short) 10);
            celda.setCellStyle(cs2);
            String octubre = rs.getString("mg_pe_octubre");
            texto = new HSSFRichTextString(octubre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de noviembre
            celda = fila.createCell((short) 11);
            String noviembre = rs.getString("mg_pe_noviembre");
            texto = new HSSFRichTextString(noviembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de diciembre
            celda = fila.createCell((short) 12);
            celda.setCellStyle(cs2);
            String diciembre = rs.getString("mg_pe_diciembre");
            texto = new HSSFRichTextString(diciembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

        } else if (i == 2) {
            // Se crea una fila dentro de la hoja
            fila = hoja.createRow(num_fila);

            //Celda de ttulo
            celda = fila.createCell((short) 0);
            celda.setCellStyle(cs2);
            texto = new HSSFRichTextString("");
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

        } else if (i == 3) {
            // Se crea una fila dentro de la hoja
            fila = hoja.createRow(num_fila);
            //Celda de ttulo
            celda = fila.createCell((short) 0);
            celda.setCellStyle(cs2);
            texto = new HSSFRichTextString("NM. PEDIDOS");
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de enero
            celda = fila.createCell((short) 1);
            String enero = rs.getString("num_enero");
            texto = new HSSFRichTextString(enero);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de febrero
            celda = fila.createCell((short) 1);
            String febrero = rs.getString("num_febrero");
            texto = new HSSFRichTextString(febrero);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de marzo
            celda = fila.createCell((short) 2);
            celda.setCellStyle(cs2);
            String marzo = rs.getString("num_marzo");
            texto = new HSSFRichTextString(marzo);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de abril
            celda = fila.createCell((short) 3);
            String abril = rs.getString("num_abril");
            texto = new HSSFRichTextString(abril);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de mayo
            celda = fila.createCell((short) 4);
            celda.setCellStyle(cs2);
            String mayo = rs.getString("num_mayo");
            texto = new HSSFRichTextString(mayo);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de junio
            celda = fila.createCell((short) 5);
            String junio = rs.getString("num_junio");
            texto = new HSSFRichTextString(junio);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda del julio
            celda = fila.createCell((short) 6);
            String julio = rs.getString("num_julio");
            texto = new HSSFRichTextString(julio);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de agosto
            celda = fila.createCell((short) 7);
            celda.setCellStyle(cs2);
            String agosto = rs.getString("num_agosto");
            texto = new HSSFRichTextString(agosto);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de septiembre
            celda = fila.createCell((short) 8);
            String septiembre = rs.getString("num_septiembre");
            texto = new HSSFRichTextString(septiembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de octubre
            celda = fila.createCell((short) 9);
            celda.setCellStyle(cs2);
            String octubre = rs.getString("num_octubre");
            texto = new HSSFRichTextString(octubre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de noviembre
            celda = fila.createCell((short) 10);
            String noviembre = rs.getString("num_noviembre");
            texto = new HSSFRichTextString(noviembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de diciembre
            celda = fila.createCell((short) 11);
            celda.setCellStyle(cs2);
            String diciembre = rs.getString("num_diciembre");
            texto = new HSSFRichTextString(diciembre);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de MG
            celda = fila.createCell((short) 12);
            double t_cliente = Double.parseDouble(rs.getString("ta_cliente"));
            double t_proveedor = Double.parseDouble(rs.getString("ta_proveedor"));

            String porCientoPedido = new java.text.DecimalFormat("#,##0")
                    .format(((t_cliente - t_proveedor) / t_cliente) * 100).concat("% ");
            texto = new HSSFRichTextString(porCientoPedido);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda de mg pedido
            celda = fila.createCell((short) 13);
            String mg_pedido = rs.getString("mg_pedido");
            texto = new HSSFRichTextString(mg_pedido);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda del num. pedido
            celda = fila.createCell((short) 14);
            String num_pedido = rs.getString("num_pedido");
            texto = new HSSFRichTextString(num_pedido);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);

            //Celda del ta cliente
            celda = fila.createCell((short) 15);
            String ta_cliente = rs.getString("ta_cliente");
            texto = new HSSFRichTextString(ta_cliente);
            celda.setCellStyle(cs3);
            celda.setCellValue(texto);
        }
        //Celda de Tarifa Cliente
        HSSFDataFormat format = libro.createDataFormat();
        HSSFCellStyle style = libro.createCellStyle();
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setRightBorderColor(HSSFColor.BLACK.index);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setTopBorderColor(HSSFColor.BLACK.index);

        style.setDataFormat(format.getFormat("00"));

        celda.setCellStyle(style);

        //Se incrementa el numero de fila
        num_fila++;
        num_fila_aux++;
    }
}

From source file:org.anyframe.logmanager.web.LogManagerController.java

License:Apache License

/**
 * log data export for excel file type//from www  . j  av  a 2 s  .co m
 * 
 * @param searchCondition
 * @param model
 * @param request
 * @return
 * @throws Exception
 */
@RequestMapping(params = "method=xlsExport")
public void xlsExport(LogSearchCondition searchCondition, Model model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    searchCondition.setPageIndex(-1);
    searchCondition.setCollection(searchCondition.getRepositoryName());

    String fileName = null;
    String sDate = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", new Locale("ko_KR"));
    sDate = sdf.format(new Date());
    StringBuffer sb = new StringBuffer();
    sb.append(searchCondition.getAppName().substring(searchCondition.getAppName().lastIndexOf("/") + 1));
    sb.append("_").append(searchCondition.getCollection()).append("_").append(sDate).append(".xls");
    fileName = sb.toString();

    SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-ddHHmm");
    logger.debug("from:{}",
            searchCondition.getFromDate() + searchCondition.getFromHour() + searchCondition.getFromMinute());
    logger.debug("to:{}",
            searchCondition.getToDate() + searchCondition.getToHour() + searchCondition.getToMinute());
    if (searchCondition.isUseFromDate())
        searchCondition.setFromDateTime(dateTimeFormat.parse(searchCondition.getFromDate()
                + searchCondition.getFromHour() + searchCondition.getFromMinute()));
    if (searchCondition.isUseToDate())
        searchCondition.setToDateTime(dateTimeFormat.parse(
                searchCondition.getToDate() + searchCondition.getToHour() + searchCondition.getToMinute()));

    List<LogDataMap> resultList = service.searchAnalysisLog(searchCondition);

    response.reset();
    response.setContentType("application/x-msexcel;charset=MS949");
    // response.setContentType("application/octet-stream");
    String userAgent = request.getHeader("User-Agent");

    if (userAgent.indexOf("MSIE 5.5") > -1) {
        response.setHeader("Content-Disposition", "filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\";");
    } else if (userAgent.indexOf("MSIE") > -1) {
        response.setHeader("Content-Disposition",
                "attachment; filename=\"" + java.net.URLEncoder.encode(fileName, "UTF-8") + "\";");
    } else {
        response.setHeader("Content-Disposition",
                "attachment; filename=\"" + new String(fileName.getBytes("euc-kr"), "latin1") + "\";");
    }
    response.setHeader("Content-Description", "JSP Generated Data");
    response.setHeader("Content-Transfer-Encoding", "binary;");
    response.setHeader("Pragma", "no-cache;");
    response.setHeader("Expires", "-1;");

    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet(fileName);

    OutputStream fileOut = null;
    try {
        fileOut = response.getOutputStream();
        HSSFRow row = null;
        HSSFRow headerRow = null;

        HSSFDataFormat df = workbook.createDataFormat();

        HSSFCellStyle headerStyle = workbook.createCellStyle();
        HSSFFont boldFont = workbook.createFont();
        boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        headerStyle.setFont(boldFont);
        headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFCellStyle dateStyle = workbook.createCellStyle();
        dateStyle.setDataFormat(df.getFormat("yyyy-mm-dd h:mm:ss.000"));
        dateStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        dateStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFCellStyle messageStyle = workbook.createCellStyle();
        messageStyle.setWrapText(true);
        messageStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        messageStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFCell cell;
        HSSFCell headerCell;

        short width = 265;

        Iterator<String> j = null;
        String key = null;
        int cellIndex = 0;
        int listSize = 0;

        String level = null;
        Date timestamp = null;
        String message = null;

        if (resultList != null) {
            listSize = resultList.size();
            for (int i = 0; i < listSize; i++) {
                LogDataMap log = (LogDataMap) resultList.get(i);
                if (i == 0) {
                    headerRow = sheet.createRow(i); // level header
                    sheet.setColumnWidth(0, 7 * width);
                    headerCell = headerRow.createCell(0);
                    HSSFRichTextString headerValue = new HSSFRichTextString("level");
                    headerCell.setCellValue(headerValue);
                    headerCell.setCellStyle(headerStyle);

                    headerCell = headerRow.createCell(1); // time stamp header
                    sheet.setColumnWidth(1, 24 * width);
                    headerValue = new HSSFRichTextString("timestamp");
                    headerCell.setCellValue(headerValue);
                    headerCell.setCellStyle(headerStyle);

                    headerCell = headerRow.createCell(2); // message header
                    sheet.setColumnWidth(2, 70 * width);
                    headerValue = new HSSFRichTextString("message");
                    headerCell.setCellValue(headerValue);
                    headerCell.setCellStyle(headerStyle);
                }

                row = sheet.createRow(i + 1);

                // level
                level = (String) log.get("level");
                cell = row.createCell(0);
                cell.setCellStyle(style);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(level);

                // timestamp
                timestamp = (Date) log.get("timestamp");
                cell = row.createCell(1);
                cell.setCellStyle(dateStyle);
                cell.setCellValue(timestamp);

                // message
                message = (String) log.get("message");
                HSSFRichTextString messageValue = new HSSFRichTextString(message);
                cell = row.createCell(2);
                cell.setCellStyle(messageStyle);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(messageValue);

                cellIndex = 3;
                j = log.keySet().iterator();
                while (j.hasNext()) {
                    key = j.next();
                    if ("_id".equals(key) || "message".equals(key) || "timestamp".equals(key)
                            || "level".equals(key)) {
                        continue;
                    }
                    //logger.debug("key=" + key);
                    if (i == 0) {
                        sheet.setColumnWidth(cellIndex, 20 * width);

                        headerCell = headerRow.createCell(cellIndex);
                        HSSFRichTextString headerValue = new HSSFRichTextString(key);
                        headerCell.setCellValue(headerValue);
                        headerCell.setCellStyle(headerStyle);
                    }
                    cell = row.createCell(cellIndex);
                    Object value = log.get(key);
                    if (value instanceof Date) {
                        cell.setCellStyle(dateStyle);
                        cell.setCellValue((Date) value);
                    } else {
                        cell.setCellStyle(style);
                        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                        cell.setCellValue((String) log.get(key));
                    }

                    cellIndex++;
                }
            }
            workbook.write(fileOut);
        }
    } catch (Exception e) {
        throw e;
    } finally {
        try {
            if (fileOut != null) {
                fileOut.flush();
                fileOut.close();
            }
        } catch (IOException ex) {
            logger.warn(ex.getMessage(), ex);
        }
    }
}

From source file:org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements.EPStyle.java

License:Apache License

/**
 * Override of Initialize() implementation
 * @param attributes the array of Attribute instances; may be empty, will
 *                  never be null/*from w ww  . j a v  a 2  s.c  o m*/
 * @param parent the parent ElementProcessor; may be null
 * @exception IOException if anything is wrong
 */
public void initialize(final Attribute[] attributes, final ElementProcessor parent) throws IOException {
    super.initialize(attributes, parent);

    EPStyleRegion sregion = (EPStyleRegion) parent;

    if (sregion.isValid()) {
        Hashtable colorhash = sregion.getColorHash();

        HSSFCellStyle style = sregion.getStyle();
        short cnvhalign = convertAlignment(getHorizontalAlignment().getCode());
        style.setAlignment(cnvhalign);
        short cnvvalign = convertVAlignment(getVerticalAlignment().getCode());
        style.setVerticalAlignment(cnvvalign);
        style.setFillPattern((short) getShade());

        Workbook workbook = getWorkbook();
        HSSFDataFormat dataformat = workbook.createDataFormat();
        if (getShade() == 1) {
            // TODO: change to constant when upgrade to new HSSF
            // solid w/foreground, bg doesn't matter
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("shade = 1");
            }
            HSSFColor color = (HSSFColor) colorhash.get(getBackgroundColor().toString());
            if (color == null) {
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("s1 BG couldn't find color for " + getBackgroundColor().toString());
                }
                color = new HSSFColor.WHITE();
            }
            style.setFillForegroundColor(color.getIndex());
            color = (HSSFColor) colorhash.get(getPatternColor().toString());
            if (color == null) {
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("s1 PC couldn't find color for " + getPatternColor().toString());
                }
                color = new HSSFColor.BLACK();
            }
            style.setFillBackgroundColor(color.getIndex());
        } else {
            HSSFColor color = (HSSFColor) colorhash.get(getBackgroundColor().toString());
            if (color == null) {
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("BG couldn't find color for " + getBackgroundColor().toString());
                }
                color = new HSSFColor.BLACK();
            }
            style.setFillBackgroundColor(color.getIndex());
            color = (HSSFColor) colorhash.get(getPatternColor().toString());
            if (color == null) {
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("PC couldn't find color for " + getPatternColor().toString());
                }
                color = new HSSFColor.WHITE();
            }
            style.setFillForegroundColor(color.getIndex());
        }
        style.setWrapText(getWrapText());
        style.setLocked(true);

        String format = null;
        try {
            format = getFormat();
        } catch (NullPointerException e) {
            format = _general_format;
        }

        if (!_general_format.equals(format)) {
            short valuenumber;
            format = kludgeForGnumericMisformats(format);
            format = kludgeForGnumericDateDivergence(format);
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("setting format to " + format);
            }
            Object o = workbook.getValidate(format, dataformat.getFormat(format));
            Short sh = null;
            sh = (Short) o;
            valuenumber = sh.shortValue();
            style.setDataFormat(valuenumber);
        }
    } else {
        invalid = true;
    }
}