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

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

Introduction

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

Prototype

public HSSFWorkbook() 

Source Link

Document

Creates new HSSFWorkbook from scratch (start here!)

Usage

From source file:Account.java

public void writetoexcel() throws FileNotFoundException, IOException {
    refresh();/*from   w  w  w.  ja v  a2s. c o  m*/
    selection();
    DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
    DefaultTableModel dt = (DefaultTableModel) jTable2.getModel();
    DefaultTableModel d = (DefaultTableModel) jTable3.getModel();
    Workbook wb = new HSSFWorkbook();
    //XSSFWorkbook wb = new XSSFWorkbook();
    CreationHelper createhelper = wb.getCreationHelper();
    Sheet sheet = wb.createSheet("new sheet");
    Row row = null;
    Cell cell = null;
    row = sheet.createRow(0);
    cell = row.createCell(0);
    cell.setCellValue(jTextField1.getText());
    cell = row.createCell(6);
    cell.setCellValue("Replacement");

    row = sheet.createRow(1);
    cell = row.createCell(0);
    cell.setCellValue("Date");
    cell = row.createCell(6);
    cell.setCellValue("Date");

    cell = row.createCell(1);
    cell.setCellValue("Goods");
    cell = row.createCell(2);
    cell.setCellValue("Quantity");
    cell = row.createCell(3);
    cell.setCellValue("Rate");
    cell = row.createCell(4);
    cell.setCellValue("Total");

    cell = row.createCell(7);
    cell.setCellValue("Goods");
    cell = row.createCell(8);
    cell.setCellValue("Quantity");
    cell = row.createCell(9);
    cell.setCellValue("Rate");
    cell = row.createCell(10);
    cell.setCellValue("Total");

    int t = dtm.getRowCount();
    int m = dt.getRowCount();
    int y = d.getRowCount();
    int i, z;
    if (dtm.getValueAt(t - 1, 1) != null)
        t++;
    if (dt.getValueAt(m - 1, 1) != null)
        m++;
    if (d.getValueAt(y - 1, 0) != null)
        y++;
    if (t < m) {
        z = m;
        for (i = 0; i < t - 1; i++) {
            row = sheet.createRow(i + 2);
            for (int j = 0; j < dtm.getColumnCount(); j++) {

                cell = row.createCell(j);
                if (j == 4)
                    cell.setCellValue((Long) dtm.getValueAt(i, j));
                else if (j == 0 || j == 1)
                    cell.setCellValue((String) dtm.getValueAt(i, j));
                else
                    cell.setCellValue((Integer) dtm.getValueAt(i, j));

                cell = row.createCell(j + 6);
                if (j == 4)
                    cell.setCellValue((Long) dt.getValueAt(i, j));
                else if (j == 0 || j == 1)
                    cell.setCellValue((String) dt.getValueAt(i, j));
                else
                    cell.setCellValue(Integer.parseInt(dt.getValueAt(i, j).toString()));
            }
        }
        //i--;
        row = sheet.createRow(t + 2);
        cell = row.createCell(2);
        cell.setCellValue(jTextField3.getText());
        cell = row.createCell(4);
        cell.setCellValue(Long.parseLong(jTextField4.getText()));

        row = sheet.createRow(t + 3);
        cell = row.createCell(2);
        cell.setCellValue(jTextField5.getText());
        cell = row.createCell(4);
        cell.setCellValue(Long.parseLong(jTextField6.getText()));

        row = sheet.createRow(t + 4);
        cell = row.createCell(2);
        cell.setCellValue(jTextField7.getText());
        cell = row.createCell(4);
        cell.setCellValue(Long.parseLong(jTextField8.getText()));

        row = sheet.createRow(t + 5);
        cell = row.createCell(2);
        cell.setCellValue("Total Replacement");
        cell = row.createCell(4);
        cell.setCellValue(Long.parseLong(jTextField10.getText()));

        row = sheet.createRow(t + 6);
        cell = row.createCell(2);
        cell.setCellValue("Total");
        cell = row.createCell(4);
        cell.setCellValue(Long.parseLong(jTextField13.getText()));

        row = sheet.createRow(t + 7);
        cell = row.createCell(2);
        cell.setCellValue("Payment Total");
        cell = row.createCell(4);
        cell.setCellValue(Long.parseLong(jTextField16.getText()));

        row = sheet.createRow(t + 8);
        cell = row.createCell(2);
        cell.setCellValue("New Grand Total");
        cell = row.createCell(4);
        cell.setCellValue(Long.parseLong(jTextField20.getText()));

        int k = i;
        while (i <= m - 1) {
            row = sheet.createRow(i + 2);
            if (i == k + 1) {
                cell = row.createCell(2);
                cell.setCellValue(jTextField3.getText());
                cell = row.createCell(4);
                cell.setCellValue(Long.parseLong(jTextField4.getText()));
            }

            if (i == k + 2) {
                cell = row.createCell(2);
                cell.setCellValue(jTextField5.getText());
                cell = row.createCell(4);
                cell.setCellValue(Long.parseLong(jTextField6.getText()));

            }

            if (i == k + 3) {
                cell = row.createCell(2);
                cell.setCellValue(jTextField7.getText());
                cell = row.createCell(4);
                cell.setCellValue(Long.parseLong(jTextField8.getText()));
            }

            if (i == k + 4) {
                cell = row.createCell(2);
                cell.setCellValue("Total Replacement");
                cell = row.createCell(4);
                cell.setCellValue(Long.parseLong(jTextField10.getText()));
            }

            if (i == k + 5) {
                cell = row.createCell(2);
                cell.setCellValue("Total");
                cell = row.createCell(4);
                cell.setCellValue(Long.parseLong(jTextField13.getText()));
            }

            if (i == k + 6) {
                cell = row.createCell(2);
                cell.setCellValue("Payment Total");
                cell = row.createCell(4);
                cell.setCellValue(Long.parseLong(jTextField16.getText()));
            }
            if (i == k + 7) {
                cell = row.createCell(2);
                cell.setCellValue("New Grand Total");
                cell = row.createCell(4);
                cell.setCellValue(Long.parseLong(jTextField20.getText()));
            }
            if (i == m - 1) {
                cell = row.createCell(8);
                cell.setCellValue("Total Replacement");
                cell = row.createCell(10);
                cell.setCellValue(Long.parseLong(jTextField10.getText()));
            } else {
                for (int j = 0; j < dtm.getColumnCount(); j++) {

                    cell = row.createCell(j + 6);
                    if (j == 4)
                        cell.setCellValue((Long) dt.getValueAt(i, j));
                    else if (j == 0 || j == 1)
                        cell.setCellValue((String) dt.getValueAt(i, j));
                    else
                        cell.setCellValue(Integer.parseInt(dt.getValueAt(i, j).toString()));
                }
            }
            i++;
        }
        row = sheet.createRow(m + 9);
        cell = row.createCell(0);
        cell.setCellValue("Payment");
        row = sheet.createRow(m + 10);
        cell = row.createCell(0);
        cell.setCellValue("Date");
        cell = row.createCell(1);
        cell.setCellValue("Payments");
        for (int u = 0; u < y - 1; u++) {
            row = sheet.createRow(u + m + 11);
            for (int j = 0; j < d.getColumnCount(); j++) {

                cell = row.createCell(j);
                if (j == 0)
                    cell.setCellValue((String) d.getValueAt(u, j));
                else
                    cell.setCellValue(Integer.parseInt(d.getValueAt(u, j).toString()));
            }
        }
        row = sheet.createRow(m + 11 + y);
        cell = row.createCell(0);
        cell.setCellValue("Payment Total");
        cell = row.createCell(1);
        cell.setCellValue(Long.parseLong(jTextField16.getText()));
    } else {
        for (i = 0; i < m - 1; i++) {
            row = sheet.createRow(i + 2);
            for (int j = 0; j < dtm.getColumnCount(); j++) {

                cell = row.createCell(j);
                if (j == 4)
                    cell.setCellValue((Long) dtm.getValueAt(i, j));
                else if (j == 0 || j == 1)
                    cell.setCellValue((String) dtm.getValueAt(i, j));
                else
                    cell.setCellValue((Integer) dtm.getValueAt(i, j));

                cell = row.createCell(j + 6);
                if (j == 4)
                    cell.setCellValue((Long) dt.getValueAt(i, j));
                else if (j == 0 || j == 1)
                    cell.setCellValue((String) dt.getValueAt(i, j));
                else
                    cell.setCellValue(Integer.parseInt(dt.getValueAt(i, j).toString()));
            }
        }
        //i--;
        int k = i;
        while (i <= t - 1) {
            row = sheet.createRow(i + 2);
            if (i == k) {
                cell = row.createCell(8);
                cell.setCellValue("Total Replacement");
                cell = row.createCell(10);
                cell.setCellValue(Long.parseLong(jTextField10.getText()));
            }
            if (i != t - 1) {
                for (int j = 0; j < dtm.getColumnCount(); j++) {

                    cell = row.createCell(j);
                    if (j == 4)
                        cell.setCellValue((Long) dtm.getValueAt(i, j));
                    else if (j == 0 || j == 1)
                        cell.setCellValue((String) dtm.getValueAt(i, j));
                    else
                        cell.setCellValue((Integer) dtm.getValueAt(i, j));
                }
            }
            i++;
        }
        row = sheet.createRow(t + 2);
        cell = row.createCell(2);
        cell.setCellValue(jTextField3.getText());
        cell = row.createCell(4);
        cell.setCellValue(Long.parseLong(jTextField4.getText()));

        row = sheet.createRow(t + 3);
        cell = row.createCell(2);
        cell.setCellValue(jTextField5.getText());
        cell = row.createCell(4);
        cell.setCellValue(Long.parseLong(jTextField6.getText()));

        row = sheet.createRow(t + 4);
        cell = row.createCell(2);
        cell.setCellValue(jTextField7.getText());
        cell = row.createCell(4);
        cell.setCellValue(Long.parseLong(jTextField8.getText()));

        row = sheet.createRow(t + 5);
        cell = row.createCell(2);
        cell.setCellValue("Total Replacement");
        cell = row.createCell(4);
        cell.setCellValue(Long.parseLong(jTextField10.getText()));

        row = sheet.createRow(t + 6);
        cell = row.createCell(2);
        cell.setCellValue("Total");
        cell = row.createCell(4);
        cell.setCellValue(Long.parseLong(jTextField13.getText()));

        row = sheet.createRow(t + 7);
        cell = row.createCell(2);
        cell.setCellValue("Payment Total");
        cell = row.createCell(4);
        cell.setCellValue(Long.parseLong(jTextField16.getText()));

        row = sheet.createRow(t + 8);
        cell = row.createCell(2);
        cell.setCellValue("New Grand Total");
        cell = row.createCell(4);
        cell.setCellValue(Long.parseLong(jTextField20.getText()));

        row = sheet.createRow(t + 10);
        cell = row.createCell(0);
        cell.setCellValue("Payment");
        row = sheet.createRow(t + 11);
        cell = row.createCell(0);
        cell.setCellValue("Date");
        cell = row.createCell(1);
        cell.setCellValue("Payments");
        for (int u = 0; u < y - 1; u++) {
            row = sheet.createRow(u + t + 12);
            for (int j = 0; j < d.getColumnCount(); j++) {

                cell = row.createCell(j);
                if (j == 0)
                    cell.setCellValue((String) d.getValueAt(u, j));
                else
                    cell.setCellValue(Integer.parseInt(d.getValueAt(u, j).toString()));
            }
        }
        row = sheet.createRow(t + y + 12);
        cell = row.createCell(0);
        cell.setCellValue("Payment Total");
        cell = row.createCell(1);
        cell.setCellValue(Long.parseLong(jTextField16.getText()));
    }

    FileOutputStream out = new FileOutputStream(
            chooser.getSelectedFile() + "\\" + jTextField1.getText() + ".xls");
    wb.write(out);
    out.close();
}

From source file:Account.java

public void writereplacement() throws FileNotFoundException, IOException {
    DefaultTableModel dtm = (DefaultTableModel) jTable2.getModel();
    Workbook wb = new HSSFWorkbook();
    //XSSFWorkbook wb = new XSSFWorkbook();
    CreationHelper createhelper = wb.getCreationHelper();
    Sheet sheet = wb.createSheet("new sheet");
    Row row = null;/* w  w w.  ja va2 s.c o  m*/
    Cell cell = null;
    row = sheet.createRow(0);
    cell = row.createCell(6);
    cell.setCellValue("Replacement");
    row = sheet.createRow(1);
    cell = row.createCell(6);
    cell.setCellValue("Date");
    cell = row.createCell(7);
    cell.setCellValue("Goods");
    cell = row.createCell(8);
    cell.setCellValue("Quantity");
    cell = row.createCell(9);
    cell.setCellValue("Rate");
    cell = row.createCell(10);
    cell.setCellValue("Total");
    int t = dtm.getRowCount();
    if (dtm.getValueAt(t - 1, 1) != null)
        t++;
    for (int i = 0; i < t - 1; i++) {
        row = sheet.createRow(i + 2);
        for (int j = 0; j < dtm.getColumnCount(); j++) {

            cell = row.createCell(j + 6);
            if (j == 4)
                cell.setCellValue((Long) dtm.getValueAt(i, j));
            else if (j == 0 || j == 1)
                cell.setCellValue((String) dtm.getValueAt(i, j));
            else
                cell.setCellValue((Integer) dtm.getValueAt(i, j));
        }
    }

    row = sheet.createRow(t + 2);
    cell = row.createCell(2 + 6);
    cell.setCellValue("Replacement Total");
    cell = row.createCell(4 + 6);
    cell.setCellValue(Long.parseLong(jTextField10.getText()));

    FileOutputStream out = new FileOutputStream("D:\\workbook.xls");
    wb.write(out);
    out.close();
}

From source file:HSSFReadWrite.java

License:Apache License

/**
 * given a filename this outputs a sample sheet with just a set of
 * rows/cells.//  w ww. jav a2 s  .  com
 */
private static void testCreateSampleSheet(String outputFilename) throws IOException {
    int rownum;
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet s = wb.createSheet();
    HSSFCellStyle cs = wb.createCellStyle();
    HSSFCellStyle cs2 = wb.createCellStyle();
    HSSFCellStyle cs3 = wb.createCellStyle();
    HSSFFont f = wb.createFont();
    HSSFFont f2 = wb.createFont();

    f.setFontHeightInPoints((short) 12);
    f.setColor((short) 0xA);
    f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    f2.setFontHeightInPoints((short) 10);
    f2.setColor((short) 0xf);
    f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    cs.setFont(f);
    cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
    cs2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    cs2.setFillPattern((short) 1); // fill w fg
    cs2.setFillForegroundColor((short) 0xA);
    cs2.setFont(f2);
    wb.setSheetName(0, "HSSF Test");
    for (rownum = 0; rownum < 300; rownum++) {
        HSSFRow r = s.createRow(rownum);
        if ((rownum % 2) == 0) {
            r.setHeight((short) 0x249);
        }

        for (int cellnum = 0; cellnum < 50; cellnum += 2) {
            HSSFCell c = r.createCell(cellnum);
            c.setCellValue(rownum * 10000 + cellnum + (((double) rownum / 1000) + ((double) cellnum / 10000)));
            if ((rownum % 2) == 0) {
                c.setCellStyle(cs);
            }
            c = r.createCell(cellnum + 1);
            c.setCellValue(new HSSFRichTextString("TEST"));
            // 50 characters divided by 1/20th of a point
            s.setColumnWidth(cellnum + 1, (int) (50 * 8 / 0.05));
            if ((rownum % 2) == 0) {
                c.setCellStyle(cs2);
            }
        }
    }

    // draw a thick black border on the row at the bottom using BLANKS
    rownum++;
    rownum++;
    HSSFRow r = s.createRow(rownum);
    cs3.setBorderBottom(HSSFCellStyle.BORDER_THICK);
    for (int cellnum = 0; cellnum < 50; cellnum++) {
        HSSFCell c = r.createCell(cellnum);
        c.setCellStyle(cs3);
    }
    s.addMergedRegion(new CellRangeAddress(0, 3, 0, 3));
    s.addMergedRegion(new CellRangeAddress(100, 110, 100, 110));

    // end draw thick black border
    // create a sheet, set its title then delete it
    s = wb.createSheet();
    wb.setSheetName(1, "DeletedSheet");
    wb.removeSheetAt(1);

    // end deleted sheet
    FileOutputStream out = new FileOutputStream(outputFilename);
    wb.write(out);
    out.close();
}

From source file:PlacasClientes.java

private void EnviarExcel(ResultSet rs) throws IOException {
    String rutaArchivo = System.getProperty("user.home") + "/ejemploExcelJava.xls";
    /*Se crea el objeto de tipo File con la ruta del archivo*/
    File archivoXLS = new File(rutaArchivo);
    /*Si el archivo existe se elimina*/
    if (archivoXLS.exists())
        archivoXLS.delete();/*from   w w  w . j  av  a  2 s . c  om*/
    /*Se crea el archivo*/
    archivoXLS.createNewFile();

    /*Se crea el libro de excel usando el objeto de tipo Workbook*/
    Workbook libro = new HSSFWorkbook();
    CreationHelper createhelper = libro.getCreationHelper();
    CellStyle cellStyle = libro.createCellStyle();
    cellStyle.setDataFormat(createhelper.createDataFormat().getFormat("dd/mm/yyyy"));
    CellStyle cellStyle2 = libro.createCellStyle();

    /*Se inicializa el flujo de datos con el archivo xls*/
    FileOutputStream archivo = new FileOutputStream(archivoXLS);

    /*Utilizamos la clase Sheet para crear una nueva hoja de trabajo dentro del libro que creamos anteriormente*/
    Sheet hoja = libro.createSheet("ClientesPlacas");

    Font fuente = libro.createFont();
    fuente.setFontHeightInPoints((short) 13);
    fuente.setFontName("Arial");
    fuente.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

    Font fuente2 = libro.createFont();
    fuente.setFontHeightInPoints((short) 13);
    fuente.setFontName("Arial");
    //fuente.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

    Row Enc = hoja.createRow(2);
    Cell celda = Enc.createCell(0);

    celda.setCellValue("PLACA DE VEHICULOS POR CLIENTES");

}

From source file:erp_frame.java

 private void btnExportMouseClicked(java.awt.event.MouseEvent evt) {
   String outputFile = "D:/ERPOutputFile/" + path + ".xls";
   switch (path) {
   case "":
      fields = employeeFields;//from  w  w  w  . ja va2 s .c  om
      break;
   case "":
      fields = attendanceFields;
      break;
   case "":
      fields = achivevmentFields;
      break;
   case "":
      fields = payRollFields;
      break;
   case "":
      fields = materialFields;
      break;
   case "?":
      fields = productFields;
      break;
   case "":
      fields = memberFields;
      break;
   case "":
      fields = orderListFields;
      break;
   case "":
      fields = orderItemFields;
      break;
   case "":
      fields = issueFields;
      break;
   case "":
      fields = vendorFields;
      break;
   case "?":
      fields = adminFields;
      break;
   case "":
      fields = purchaseFields;
      break;
   case "?":
      fields = payableFields;
      break;
   case "?":
      fields = assetFields;
      break;
   case "?":
      fields = billboardFields;
      break;
   case "?":
      fields = departFields;
      break;

   default:
      JOptionPane.showMessageDialog(JToolBar, "?");
      break;
   }
   try {
      // Create a excel file
      HSSFWorkbook workbook = new HSSFWorkbook();
      // Create a sheet with name
      HSSFSheet sheet = workbook.createSheet(path);
      HSSFRow row = null;
      HSSFCell cell = null;
      // set the sheet row count and set the first row data with title
      for (int i = 0; i < table_firmData.getRowCount() + 1; i++) {
         if (i == 0) {
            row = sheet.createRow((short) i);
            for (int k = 0; k < fields.length; k++) {
               cell = row.createCell((short) k);
               cell.setCellType(HSSFCell.CELL_TYPE_STRING);
               cell.setCellValue(fields[k]);
            }
         } else {
            // Insert table data in next row
            row = sheet.createRow((short) i);
            for (int k = 0; k < fields.length; k++) {
               cell = row.createCell((short) k);
               cell.setCellType(HSSFCell.CELL_TYPE_STRING);
               cell.setCellValue((String) table_firmData.getValueAt(i - 1, k));
            }

         }
      }
      FileOutputStream fOut = new FileOutputStream(outputFile);
      workbook.write(fOut);
      fOut.flush();
      fOut.close();
      JOptionPane.showMessageDialog(JToolBar, "?!\n ? :" + outputFile);
   } catch (Exception ee) {
      JOptionPane.showMessageDialog(JToolBar, " : " + ee.getMessage());
      System.out.println(ee.toString());
   }
}

From source file:Inicio.java

public void CrearExcel() {
    File archivoXLS = new File(rutaArchivo);
    if (archivoXLS.exists())
        archivoXLS.delete();//from   w w w . j a  v  a 2 s  . c o  m
    try {
        archivoXLS.createNewFile();
        Workbook libro = new HSSFWorkbook(); //se crea el objeto 
        FileOutputStream archivo = new FileOutputStream(archivoXLS);
        Sheet hoja = libro.createSheet("Datos");// crea la hoja de Trabajo
        Random rnd = new Random();
        for (int f = 0; f < 100; f++) {
            Row fila = hoja.createRow(f);
            for (int c = 0; c < 5; c++) {
                Cell celda = fila.createCell(c);
                if (f == 0) {
                    celda.setCellValue("Encabezado #" + c);
                } else {
                    celda.setCellValue(rnd.nextInt(999) + 1); //genera un numero entre 1 y 100
                }
            } //end for
        } //end For
        libro.write(archivo);
        archivo.close();
        //Para que lo abra en el programa predeterminado...
        Runtime.getRuntime().exec("cmd /c " + rutaArchivo);
        System.out.println("Archivo " + rutaArchivo + " Creado");
    } catch (IOException ex) {
        System.err.println("Error creando el archivo " + ex.getMessage());
    }
}

From source file:Inicio.java

public void Oracle_Excel() {

    String nombres, apellidos, id, tdoc, genero, edad, historia;
    int i = 1;//w ww .  java  2  s. c o  m
    int c = 0;
    ResultSet reg = Database.consultar("SELECT * FROM PACIENTES");
    File archivoXLS = new File(rutaArchivo);
    if (archivoXLS.exists())
        archivoXLS.delete();
    try {
        archivoXLS.createNewFile();
        Workbook libro = new HSSFWorkbook(); //se crea el objeto 
        FileOutputStream archivo = new FileOutputStream(archivoXLS);
        Sheet hoja = libro.createSheet("Datos");// crea la hoja de Trabajo
        Row fila = hoja.createRow(0);
        Cell celda = fila.createCell(0);
        celda.setCellValue("DNI");
        celda = fila.createCell(1);
        celda.setCellValue("TIPO");
        celda = fila.createCell(2);
        celda.setCellValue("NOMBRES");
        celda = fila.createCell(3);
        celda.setCellValue("APELLIDOS");
        celda = fila.createCell(4);
        celda.setCellValue("GENERO");
        celda = fila.createCell(5);
        celda.setCellValue("EDAD");
        celda = fila.createCell(6);
        celda.setCellValue("HISTORIACL");
        while (reg.next()) {
            fila = hoja.createRow(i);
            id = reg.getString("DNI");
            tdoc = reg.getString("TIPODOCUMENTO");
            nombres = reg.getString("NOMBRE1") + " " + reg.getString("NOMBRE2");
            apellidos = reg.getString("APELLIDO1") + " " + reg.getString("APELLIDO2");
            tdoc = reg.getString("TIPODOCUMENTO");
            genero = reg.getString("SEXO");
            edad = reg.getString("EDAD");
            historia = reg.getString("NUMERO_HISTORIA");
            celda = fila.createCell(0);
            celda.setCellValue(id);
            celda = fila.createCell(1);
            celda.setCellValue(tdoc);
            celda = fila.createCell(2);
            celda.setCellValue(nombres);
            celda = fila.createCell(3);
            celda.setCellValue(apellidos);
            celda = fila.createCell(4);
            celda.setCellValue(genero);
            celda = fila.createCell(5);
            celda.setCellValue(edad);
            celda = fila.createCell(6);
            celda.setCellValue(historia);
            i++;

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

        Runtime.getRuntime().exec("cmd /c " + rutaArchivo);
        System.out.println("Archivo " + rutaArchivo + " Creado");

    } catch (SQLException ex) {
        System.err.println("Error creando el archivo " + ex.getMessage());
    } catch (IOException ex) {
        System.err.println("Error creando el archivo " + ex.getMessage());
    }
}

From source file:Console.java

static public void exportToExcel(String sheetName, ArrayList headers, ArrayList data, File outputFile)
        throws HPSFException {

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet(sheetName);

    int rowIdx = 0;
    short cellIdx = 0;

    // Header//  ww  w .ja va 2s  . c o  m
    HSSFRow hssfHeader = sheet.createRow(rowIdx);
    HSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    for (Iterator cells = headers.iterator(); cells.hasNext();) {
        HSSFCell hssfCell = hssfHeader.createCell(cellIdx++);
        hssfCell.setCellStyle(cellStyle);
        hssfCell.setCellValue((String) cells.next());
    }
    // Data
    rowIdx = 1;
    for (Iterator rows = data.iterator(); rows.hasNext();) {
        ArrayList row = (ArrayList) rows.next();
        HSSFRow hssfRow = sheet.createRow(rowIdx++);
        cellIdx = 0;
        for (Iterator cells = row.iterator(); cells.hasNext();) {
            HSSFCell hssfCell = hssfRow.createCell(cellIdx++);
            Object o = cells.next();
            if ("class java.lang.Double".equals(o.getClass().toString())) {
                hssfCell.setCellValue((Double) o);
            } else {
                hssfCell.setCellValue((String) o);
            }

        }
    }

    wb.setSheetName(0, sheetName);
    try {
        FileOutputStream outs = new FileOutputStream(outputFile);
        wb.write(outs);
        outs.close();
        //            System.out.println("Archivo creado correctamente en " + outputFile.getAbsolutePath());
    } catch (IOException e) {
        e.printStackTrace();
        throw new HPSFException(e.getMessage());
    }
}

From source file:action.AdminAction.java

public String viewCustomerReport() throws IOException {
    LoginDao loginDao = new LoginDao();

    List<Customer> customerList = new ArrayList<Customer>();

    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();
    customerList = loginDao.findAll();/*from w ww  .  j a  v a 2 s .  com*/

    HttpSession session = request.getSession();
    HSSFWorkbook workBook = new HSSFWorkbook();
    AdminReport customerReport = new AdminReport();
    /** Date Time Format for Time Stamp */
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HH_mm");
    String dateFormat = simpleDateFormat.format(new Date());
    /**File Name for XLS File */
    String fileName = "Customer_" + dateFormat;

    // excelWriter.inventoryExport(lst, workBook, request);
    customerReport.customerExport(customerList, workBook, request);
    response.setContentType("application/vnd.ms-excel");
    /** Change attachment into inline, inorder to open the excel file in Readonly mode */
    response.setHeader("Content-disposition", "inline;filename=" + fileName + ".xls");

    workBook.write(response.getOutputStream());
    response.getOutputStream().close();

    return SUCCESS;

}

From source file:action.AdminAction.java

public String viewOrdersReport() throws IOException {
    TransactionDao transactionDao = new TransactionDao();
    List<CustomerOrder> orderList = new ArrayList<CustomerOrder>();
    CustomerOrder order;/*  w  w w.  j  a v a  2s .  c  o  m*/
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();
    orderList = transactionDao.findAll();
    HSSFWorkbook workBook = new HSSFWorkbook();
    AdminReport customerReport = new AdminReport();
    /** Date Time Format for Time Stamp */
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HH_mm");
    String dateFormat = simpleDateFormat.format(new Date());
    /**File Name for XLS File */
    String fileName = "Order_" + dateFormat;

    // excelWriter.inventoryExport(lst, workBook, request);
    customerReport.orderExport(orderList, workBook, request);
    response.setContentType("application/vnd.ms-excel");
    /** Change attachment into inline, inorder to open the excel file in Readonly mode */
    response.setHeader("Content-disposition", "inline;filename=" + fileName + ".xls");

    workBook.write(response.getOutputStream());
    response.getOutputStream().close();

    return SUCCESS;
}