POI HSSFComment: Una manera simple de añadir un comentario en una celda de excel usando el metodo cell.setCellComment().
public class ExcelTools
{
public static void main(String[] args){
//escribirExcel();
//leerExcel();
validarValoresDuplicadosLista();
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);
HSSFPatriarch patr = sheet.createDrawingPatriarch();
//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);
//Añadir comentario a una celda en excel
HSSFComment comment1 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short) 6, 5));
comment1.setString(new HSSFRichTextString("Comentario para la celda"));
comment1.setAuthor("dmottab.blogspot.com");
cell.setCellComment(comment1);
//Escribimos los resultados a un fichero Excel
FileOutputStream fileOut = new FileOutputStream("C:\\ejemplo.xls");
wb.write(fileOut);
fileOut.close();
}catch(IOException e){
System.out.println("Error al escribir el fichero.");
}
}
}
Excelente muchas gracias!!!
ResponderEliminarSiempre es bueno tener el código fuente a la mano hee.
saludos!