合并单元格时,设置单元格值不起作用。

我想通过Apache Poi创建一个excel文档。我在第一行和第二列添加了一张图片。我想添加一个字符串,图片右侧,我想做合并。我为它写了这样的代码

 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

解决方案:

合并后的单元格范围显示左上角的单元格值。就像你的例子一样 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); 在你的代码中。

给TA打赏
共{{data.count}}人
人已打赏
解决方案

如何从Cypress的beforeEach中删除所有cy.route?

2022-5-13 13:00:17

解决方案

Azure Pipeline YAML文件的语法检查器

2022-5-13 13:00:24

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索