1

I use the JXL API to add an Image to an Excel file. Libraries used:
jcommon (1.0.14)
jfreechart (1.0.13)
jxl (2.6.10)

// chartImage is of type BufferedImage
com.KeyPoint.PngEncoder encoder = new com.KeyPoint.PngEncoder(chartImage, true, 0, 0); 
jxl.write.WritableImage image = new jxl.write.WritableImage(0, 2, (chartImage.getWidth()/100),16, 
encoder.pngEncode());
sheet.addImage(image);

The problem is that the WritableImage constructors take widths and heights in terms of rows and columns (width: column 0 to column chartImage.getWidth()/100, height: row 2 to row 16). This causes the chart image to blur.
How do I get the original image into the Excel using JXL? Kindly help. Thanks! :-)

Pravin Sonawane
  • 1,803
  • 4
  • 18
  • 32

2 Answers2

1

Don't use PngEncoder directly. Instead, use the relevant ChartUtilities method, which will find the Sun/Oracle PNG encoder if available.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Can you narrow it down `JFreeChart` versus `JXL`? – trashgod Aug 17 '11 at 05:23
  • Im sorry, but I did not understand that (please excuse my knowledge). My issue is that the WritableImage class forces me to specify the image width and height in terms of number columns and rows respectively which I think causes the image to blur. The image loses its clarity (being a chart, it should be very clear). – Pravin Sonawane Aug 17 '11 at 07:07
  • Sorry, my question was phrased poorly. It sounds like the image produced by `JFreeChart` is OK. I'm guessing that `WritableImage` is using a suboptimal interpolation type. Is there any way to pre-scale to the desired size, for [example](http://stackoverflow.com/questions/4216123/how-to-scale-a-bufferedimage/4216635#4216635) using `TYPE_BILINEAR` or `TYPE_BICUBIC`? – trashgod Aug 17 '11 at 09:37
  • i tried Jes's simple solution.. it too involves upscaling. Thankyou for your help. I'll try using interpolation in my spare time as i've to rush this time.. :P – Pravin Sonawane Aug 18 '11 at 06:27
  • As the original image is retained, you might try increasing the number of destination cells as a temporary workaround. – trashgod Aug 18 '11 at 06:52
1

The issue lies with either the compression used in Excel or the encoder you used.

A solution that works is drawing the chart larger than the intended image, and then letting Excel do the down-scaling. This ensures clarity although a chart-filled workbook will be much larger than before.

AFAIK that is the only solution to the problem. I have had similar issues with Word and PowerPoint, and the solution seems to be upscaling.

When you scale the chart you should note that the font sizes need to be changed too.

Jes
  • 2,748
  • 18
  • 22