使用apache.poi冻结/锁定xlsx工作表上的图像

时间:2017-06-27 13:24:20

标签: java xssf xsl-stylesheet

我正在使用Apache.POI创建xlsx并在顶部插入徽标,如何冻结或锁定图像,以便在下载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");
                }

1 个答案:

答案 0 :(得分:0)

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

Sheet.createFreezePane( i, j );