edu.aplic.escuchadores.EjemploExcel.java Source code

Java tutorial

Introduction

Here is the source code for edu.aplic.escuchadores.EjemploExcel.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.aplic.escuchadores;

/**
 *
 * @author rogelioesl
 */
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * Ejemplo sencillo de cmo crear una hoja Excel con POI
 * 
 * @author chuidiang
 * 
 */
public class EjemploExcel {

    /**
     * Crea una hoja Excel y la guarda.
     * 
     * @param args
     */

    public static void crearExel(File file) {
        // Se crea el libro
        HSSFWorkbook libro = new HSSFWorkbook();

        // Se crea una hoja dentro del libro
        HSSFSheet hoja = libro.createSheet();
        HSSFRow fila = null;
        HSSFCell celda = null;

        // Se crea una fila dentro de la hoja
        fila = hoja.createRow(0);

        // Se crea una celda dentro de la fila
        celda = fila.createCell(0);
        // Se crea el contenido de la celda y se mete en ella.
        //HSSFRichTextString texto = new HSSFRichTextString("hola mundo");
        celda.setCellValue(new HSSFRichTextString("hola mundo"));

        // Se crea una celda dentro de la fila
        celda = fila.createCell(1);
        // Se crea el contenido de la celda y se mete en ella.
        //HSSFRichTextString texto = new HSSFRichTextString("hola mundo");
        celda.setCellValue(new HSSFRichTextString("Celada dos"));

        fila = hoja.createRow(1);

        // Se crea una celda dentro de la fila
        celda = fila.createCell(0);

        // Se crea el contenido de la celda y se mete en ella.
        //HSSFRichTextString texton = new HSSFRichTextString("value");
        celda.setCellValue(new HSSFRichTextString("value"));

        // Se crea una celda dentro de la fila
        celda = fila.createCell(1);
        // Se crea el contenido de la celda y se mete en ella.
        //HSSFRichTextString texton = new HSSFRichTextString("value");
        //celda.setCellValue(new HSSFRichTextString("20040044.822"));
        celda.setCellValue(20040044.82);

        // Se salva el libro.
        try {
            //FileOutputStream elFichero = new FileOutputStream("ventas"+new Date().toString()+".xls");
            FileOutputStream elFichero = new FileOutputStream(file);

            libro.write(elFichero);
            elFichero.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

    }
}