Crear Excel HSSF

 
PAGINA PRINCIPAL:   DOCUMENTACION POI APACHE  
 
 
 
package com.fywservices.poi.hssf;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import org.apache.poi.hssf.usermodel.*;

public class ExcelWriterDemo
{
   private static final     String[] HEADINGS =
      new String[]{"Symbol", "Price", "PEG", "Yield", "Change"};

   public HSSFWorkbook
      createExcelWorkbook(ExcelWriterDemoData[] data)
   {
      HSSFWorkbook     excelWorkbook  = new HSSFWorkbook();
      HSSFSheet        excelSheet     = null;
      HSSFRow          sheetRow       = null;
      HSSFCell         rowCell        = null;

      ExcelWriterDemoStyles    =
         new ExcelWriterDemoStyles(excelWorkbook);
      ExcelWriterDemoStock     stock  = null;

      HSSFCellStyle     headerStyle   = styles.getHeaderStyle();
      HSSFDataFormat    dataFormat    = styles.getDataFormat();
      HSSFFont          headerFont    = styles.getHeaderFont();
      HSSFPalette       palette       = styles.getPalette();

      int               rowIndex      = 1;

      excelSheet   =
         excelWorkbook.createSheet("Today Stock Analysis");
      sheetRow = excelSheet.createRow(0);
      excelSheet.setColumnWidth(0, 6000);
      excelSheet.setColumnWidth(1, 6000);
      excelSheet.setColumnWidth(2, 2500);
      excelSheet.setColumnWidth(3, 2500);
      excelSheet.setColumnWidth(4, 3000);
      sheetRow.setHeight((short)600);
      for(int cellIndex = 0; cellIndex < HEADINGS.length;
      {
         rowCell = sheetRow.createCell(cellIndex);
         rowCell.setCellType(HSSFCell.CELL_TYPE_STRING);
         rowCell.setCellValue(new
            HSSFRichTextString(HEADINGS[cellIndex]));
         rowCell.setCellStyle(headerStyle);
      }

      for(Iterator<ExcelWriterDemoStock>   stockIt =
         data[0].getDemoDataCollection().iterator();
         stockIt.hasNext(); rowIndex++)
     {
         sheetRow  = excelSheet.createRow(rowIndex);
         stock     = stockIt.next();

         rowCell   = sheetRow.createCell(0);
         rowCell.setCellType(HSSFCell.CELL_TYPE_STRING);
         rowCell.setCellStyle(styles.getDataStringStyle());
         rowCell.setCellValue(new
            HSSFRichTextString(stock.getSymbol()));

         rowCell   = sheetRow.createCell(1);
         rowCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
         rowCell.setCellStyle(styles.getDataDollarStyle());
         rowCell.setCellValue(stock.getPrice());

         rowCell   = sheetRow.createCell(2);
         rowCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
         rowCell.setCellValue(stock.getPeg());

         rowCell   = sheetRow.createCell(3);
         rowCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
         rowCell.setCellValue(stock.getYield());

         rowCell   = sheetRow.createCell(4);
         rowCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
         rowCell.setCellValue(stock.getChange());

      }
      excelSheet   =
         excelWorkbook.createSheet("Yesterday Stock Analysis");
      sheetRow     = excelSheet.createRow(0);
      excelSheet.setColumnWidth(0, 6000);
      excelSheet.setColumnWidth(1, 6000);
      excelSheet.setColumnWidth(2, 2500);
      excelSheet.setColumnWidth(3, 2500);
      excelSheet.setColumnWidth(4, 3000);
      sheetRow.setHeight((short)600);
      for(int cellIndex =
         0; cellIndex < HEADINGS.length; cellIndex++)
      {
         rowCell = sheetRow.createCell(cellIndex);
         rowCell.setCellType(HSSFCell.CELL_TYPE_STRING);
         rowCell.setCellValue(new
            HSSFRichTextString(HEADINGS[cellIndex]));
         //TODO: SHOW RICH TEXT STRING APPROACH TOO
         rowCell.setCellStyle(headerStyle);
      }
      rowIndex      = 1;
      for(Iterator<ExcelWriterDemoStock> stockIt =
         data[1].getDemoDataCollection().iterator();
         stockIt.hasNext(); rowIndex++)
      {
         sheetRow   = excelSheet.createRow(rowIndex);
         stock      = stockIt.next();

         rowCell    = sheetRow.createCell(0);
         rowCell.setCellType(HSSFCell.CELL_TYPE_STRING);
         rowCell.setCellStyle(styles.getDataStringStyle());
         rowCell.setCellValue(new
            HSSFRichTextString(stock.getSymbol()));

         rowCell    = sheetRow.createCell(1);
         rowCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
         rowCell.setCellStyle(styles.getDataDollarStyle());
         rowCell.setCellValue(stock.getPrice());

         rowCell    = sheetRow.createCell(2);
         rowCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
         rowCell.setCellValue(stock.getPeg());

         rowCell    = sheetRow.createCell(3);
         rowCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
         rowCell.setCellValue(stock.getYield());

         rowCell    = sheetRow.createCell(4);
         rowCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
         rowCell.setCellValue(stock.getChange());
      }
      return excelWorkbook;
   }
}

Comentarios

Entradas populares de este blog

Rootear Motorola Defy

Liferay DXP -IP Geocoder

Pasos para Publicar un sitio Web