2017-06-27 101 views
0

我使用Apache.POI创建xlsx并在顶部插入徽标,如何冻结或锁定图像以便一旦xlsx就不能更改被下载。使用apache.poi冻结/锁定xlsx图片上的图像

 int numberOfSheets = wb.getNumberOfSheets(); 
      for(int i=0;i<numberOfSheets;i++) 
      { 
       XSSFSheet sheet = wb.getSheetAt(i); 

       try { 
        //insert a logo 
        String location = "C:/Git/cc/src/main/resources/logo/VCC_logo.jpg"; // hard coded for testing 
        InputStream is = new FileInputStream(location); 
        byte[] bytes = IOUtils.toByteArray(is); 
        int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG); 
        is.close(); 
        // Create Winner sheet 

        CreationHelper creationHelper = wb.getCreationHelper(); 
        Drawing drawing = sheet.createDrawingPatriarch(); 


        ClientAnchor anchor = 
        creationHelper.createClientAnchor(); 
        anchor.setCol1(0); 
        anchor.setRow1(0); 
        Picture pict = drawing.createPicture(anchor, pictureIdx); 
        pict.resize(2.5,2.5); 
        pict.getImageDimension().setSize(3.0,3.0); 
        sheet.createFreezePane(0,3); 
       } catch (FileNotFoundException e) { 
        throw new nException("Problem in reading VCC_logo.jpg"); 
       } catch (IOException io) { 
        throw new Exception("Problem in reading VCC_logo.jpg"); 
       } 

回答

0

如果您的标志开始于i行和列j。您可以使用API​​ createFreezePane,如:

Sheet.createFreezePane(i, j);

+0

是的,我试过,但它冻结在该窗格中的行和图像仍然是在纸移动。我在xlsx上有两张纸,当我打开xlsx时,图像被锁定不能移动,但是一旦我转到第二张纸并尝试移动图像,它也开始移动第一张纸。 – tyro

+0

您必须在要冻结的工作表上调用createFreezePane。请张贴问题中的代码以获得更好的帮助。 – SomeDude

+0

我添加了代码 – tyro