lunes, 12 de mayo de 2014

Estoy agregando una imagen a un archivo de Excel, cuando lo ejecuto desde eclipse, la imagen se agrega correctamente. Pero al generar el jar lanza una excepción indicando que no puede encontrar el recurso. Aquí el código para hacer la inserción. Agregar una imagen desde un jar.
 public void agregarImagenEncabezado(Sheet hoja, double x, double y,
   double width, double heigh, String rutaImagen)
   throws WriteException, IOException {

  InputStream is = this.getClass().getResourceAsStream(rutaImagen);

  // add picture data to this workbook.
  byte[] bytes = IOUtils.toByteArray(is);
  int pictureIdx = libro.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
  is.close();

  CreationHelper helper = libro.getCreationHelper();

  // Create the drawing patriarch. This is the top level container for all
  // shapes.
  Drawing drawing = hoja.createDrawingPatriarch();

  // add a picture shape
  ClientAnchor anchor = helper.createClientAnchor();
  // set top-left corner of the picture,
  // subsequent call of Picture#resize() will operate relative to it
  anchor.setCol1((int)x);
  anchor.setRow1((int)y);
  Picture pict = drawing.createPicture(anchor, pictureIdx);

  // auto-size picture relative to its top-left corner
  pict.resize();
 }