установка значения ячейки не работает, когда я объединяю ячейки - PullRequest
0 голосов
/ 23 апреля 2020

Я хочу создать документ Excel с помощью Apache Poi. Я добавил картинку в первый ряд и столбец. Я хочу добавить строку, которая изображает правую сторону, и я хочу сделать слияние. Я написал эти коды для него

 try (OutputStream fileOut = new FileOutputStream("C:\\Users\\ftk1187\\Desktop\\poi-generated-file.xls")) {  
            Workbook wb = new HSSFWorkbook();  
            Sheet sheet = wb.createSheet("Sheet");  
            Font font = wb.createFont();
            font.setColor(IndexedColors.BLUE.getIndex());
            font.setFontHeightInPoints((short) 24);
            font.setFontName("ARIAL");
            font.setBold(true);            
            Row row = sheet.createRow(0);
            row.setHeight((short)120);
            Row row1 = sheet.createRow(1);
            row1.setHeight((short)285);
            Row row2 = sheet.createRow(2);
            row2.setHeight((short)285);
            Row row3 = sheet.createRow(3);
            row3.setHeight((short)285);
            Row row4 = sheet.createRow(4);
            row4.setHeight((short)285);
            sheet.setColumnWidth(0, 1*256);
            sheet.setColumnWidth(1, 3*256);
            sheet.setColumnWidth(2, 42*256);
            sheet.setColumnWidth(3, 1*256);
            sheet.setColumnWidth(4, 25*256);
            sheet.setColumnWidth(5, 2*256);
            sheet.setColumnWidth(6, 39*256);
            sheet.setColumnWidth(7, 1*256);
            sheet.setColumnWidth(8, 25*256);
            sheet.setColumnWidth(9, 4*256);
            sheet.addMergedRegionUnsafe(new CellRangeAddress(1,3,1,10));
            CellStyle cellStyle = wb.createCellStyle();
            cellStyle.setFont(font);
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            Cell cell = row1.createCell(3);
            cell.setCellValue("Request to IAES Istanbul Template"); 
            cell.setCellStyle(cellStyle);
            //IAE
            InputStream inputStream=new FileInputStream("C:\\Users\\ftk1187\\Desktop\\iae.png");
            byte[] imageBytes = IOUtils.toByteArray(inputStream);
            int pictureureIdx = wb.addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG);
            inputStream.close();
            CreationHelper helper = wb.getCreationHelper();
            Drawing drawing = sheet.createDrawingPatriarch();
            ClientAnchor anchor = helper.createClientAnchor();
            anchor.setDx1(0);
            anchor.setDy1(0);
            anchor.setDx2(125);
            anchor.setDy2(55);
            anchor.setCol1(1);
            anchor.setRow1(1);
            Picture pict = drawing.createPicture(anchor, pictureureIdx);
            pict.resize(2.5);
            Cell cell0 = row1.createCell(1);
            cell0.setCellStyle(cellStyle);
            //sheet.addMergedRegionUnsafe(new CellRangeAddress(1,3,1,3));
            //TEC
            /*InputStream inputStream2=new FileInputStream("C:\\Users\\ftk1187\\Desktop\\tec.png");
            byte[] imageBytes2 = IOUtils.toByteArray(inputStream2);
            int pictureureIdx2 = wb.addPicture(imageBytes2, Workbook.PICTURE_TYPE_PNG);
            inputStream2.close();
            CreationHelper helper2 = wb.getCreationHelper();
            Drawing drawing2 = sheet.createDrawingPatriarch();
            ClientAnchor anchor2 = helper2.createClientAnchor();
            anchor2.setCol1(11);
            anchor2.setRow1(1);
            Picture pict2 = drawing2.createPicture(anchor2, pictureureIdx2);
            pict2.resize(2);
            Cell cell1 = sheet.createRow(2).createCell(11);
            sheet.addMergedRegionUnsafe(new CellRangeAddress(0,3,10,13));
            cell1.setCellStyle(cellStyle);*/
            wb.write(fileOut);  
            fileOut.close();
        }catch(Exception e) {  
            System.out.println(e.getMessage());  
        } 

Изображение идет, но строка не приходит. Я не понимаю, почему это не придет. Поэтому я добавил снимок моего листа Excel и написал на нем то, что хочу.

capture

1 Ответ

2 голосов
/ 23 апреля 2020

Диапазоны объединенных ячеек показывают значение ячейки в верхней левой ячейке. Как в вашем случае CellRangeAddress(1, 3, 1, 10), то есть B2:K4, объединяются, значение ячейки должно быть в B2. Но вы устанавливаете его на Cell cell = row1.createCell(3);, что составляет D2.

Так что вместо

...
Cell cell = row1.createCell(3);
cell.setCellValue("Request to IAES Istanbul Template"); 
cell.setCellStyle(cellStyle);
...

сделайте

...
Cell cell = row1.createCell(1);
cell.setCellValue("Request to IAES Istanbul Template"); 
cell.setCellStyle(cellStyle);
...

И затем не создавайте row1, ячейку 1, снова позже. Потому что тогда снова будет создана пустая ячейка. Так что удалите:

...
Cell cell0 = row1.createCell(1);
cell0.setCellStyle(cellStyle);
...

после pict.resize(2.5); в вашем коде.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...