En esta página se muestra un programa java completo como ejemplo de uso de la librería Jakarta POI. Puedes encontrar mucha más información sobre esta librería en el artículo original que contiene este ejemplo.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class DemoPOI
{
public static void main(String[] args)
{
escribirExcel();
leerExcel();
System.out.println(«Ejemplo Finalizado.»);
}
public static void escribirExcel()
{
try
{
//Se crea el libro Excel
HSSFWorkbook wb = new HSSFWorkbook();
//Se crea una nueva hoja dentro del libro
HSSFSheet sheet = wb.createSheet(«HojaEjemplo»);
//Se crea una fila dentro de la hoja
HSSFRow row = sheet.createRow((short)0);
//Creamos celdas de varios tipos
row.createCell((short)0).setCellValue(1);
row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue(«ejemplo»);
row.createCell((short)3).setCellValue(true);
//Creamos una celda de tipo fecha y la mostramos
//indicando un patrón de formato
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(
HSSFDataFormat.getBuiltinFormat(«d/m/yy h:mm»));
HSSFCell cell = row.createCell((short)4);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
//Escribimos los resultados a un fichero Excel
FileOutputStream fileOut =
new FileOutputStream(«ejemplo.xls»);
wb.write(fileOut);
fileOut.close();
}
catch(IOException e)
{
System.out.println(«Error al escribir el fichero.»);
}
}
public static void leerExcel()
{
try
{
//Se abre el fichero Excel
POIFSFileSystem fs =
new POIFSFileSystem(
new FileInputStream(«c:ejemplo.xls»));
//Se obtiene el libro Excel
HSSFWorkbook wb = new HSSFWorkbook(fs);
//Se obtiene la primera hoja
HSSFSheet sheet = wb.getSheetAt(0);
//Se obtiene la primera fila de la hoja
HSSFRow row = sheet.getRow(0);
//Se leen las primeras 5 celdas
for(int i=0; i<5; i++)
{
//Se obtiene la celda i-esima
HSSFCell cell = row.getCell((short)i);
//Si la celda leida no está vacía
if (cell != null)
{
//Se imprime en pantalla la celda según su tipo
switch(cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC:
System.out.println("Número: " + cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
System.out.println("String: " + cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
System.out.println("Boolean: " + cell.getBooleanCellValue());
break;
default:
System.out.println("Default: " + cell.getDateCellValue());
break;
}
}
}
}
catch(IOException ex)
{
System.out.println("Error al leer el fichero.");
}
}
}
[/sourcecode]