Hi,

I'm using poi-bin-3.0.1-FINAL-20070705 and I'm trying to insert an EMF
picture into an Excel sheet without much luck. I've used the example
in the "Busy Developers's Guide" and it works great with a png but
when I change it to an emf Excel tells me: "File error: data may have
been lost" when I open the xls document.

Is this a bug, or am I doing something silly?



Thanks in advance,

Bertil



public class TestEMF {

    public static void main(String[] args) throws Exception {
        new TestEMF().test();
    }

    public void test() throws Exception {
        createWorkbook("png", "test_png.png",
                HSSFWorkbook.PICTURE_TYPE_PNG);
        createWorkbook("emf", "test_emf.emf",
                HSSFWorkbook.PICTURE_TYPE_EMF);
    }

    public void createWorkbook(String name, String image, int imageType)
    throws Exception {
        HSSFWorkbook workbook = new HSSFWorkbook();
        createSheet(workbook, name, image, imageType);
        FileOutputStream out = new FileOutputStream(name + ".xls");
        workbook.write(out);
        out.close();
    }

    public void createSheet(HSSFWorkbook workbook, String sheetName,
            String image, int imageType) throws Exception {
        HSSFSheet sheet = workbook.createSheet(sheetName);
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

        HSSFClientAnchor anchor;
        anchor = new HSSFClientAnchor(0,0,0,255,(short)2,2,(short)4,7);
        anchor.setAnchorType( 2 );
        patriarch.createPicture(anchor,
                loadPicture(image, workbook, imageType));
    }

    private int loadPicture(String image, HSSFWorkbook workbook, int type)
    throws IOException {
        File file = new File(image);
        byte[] bytes = new byte[(int)file.length()];
        InputStream input = new BufferedInputStream(new FileInputStream(file));

        try {
            int offset = 0;
            int read = -1;
            while((read = input.read()) != -1)
                bytes[offset++] = (byte)read;
        } finally {
            input.close();
        }

        return workbook.addPicture(bytes, type);
    }
}

The example pictures can be found here:
http://bluefire.dnsalias.com/~richard/poi/test_png.png
http://bluefire.dnsalias.com/~richard/poi/test_emf.emf

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to