You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Bertil Sherman <be...@gmail.com> on 2007/10/08 21:08:03 UTC

Insert EMF pictures in Excel

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: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Insert EMF pictures in Excel

Posted by Yegor Kozlov <ye...@dinom.ru>.
Yes, it is a bug. Currently only PNG and JPEG images can be inserted.
For other formats you will get  "File error: data may have been
lost".
The problem is that the binary data for WMF and EMF should be
"preprocessed" before inserting in a workbook. In case of PNG and JPEG
we just copy the exact contents of a file and it works fine but does
not for EMF/WMF. We didn't yet figure out what's the difference.
Hope the problem will be fixed by POI 3.1.

Yegor

> 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: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org