【问题标题】:Align text center in the merged region in excel using apache poi in java使用java中的apache poi在excel中的合并区域中对齐文本中心
【发布时间】:2017-01-22 13:36:35
【问题描述】:

我创建了一个 Excel 表,我在其中将值插入单元格并同时垂直合并单元格,我能够做到这一点,但问题是,我无法将文本垂直居中对齐合并单元格。目前,文本显示在垂直合并单元格的底部。

这是我的代码,

    CellStyle cs = null;
    cs = wb.createCellStyle();
    cs.setWrapText(true);
    //cs.setAlignment(CellStyle.ALIGN_CENTER);
    cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    cs.setFont(f);



            Row row[]=new Row[pageListAcrToDept.size()]; 
            for (int i = 0; i < pageListAcrToDept.size(); i++) {
                rowIndex = 8 + i ;
                row[i]=sheet.createRow(rowIndex);
            }

            List<List<String>> datas=new ArrayList<>();
            datas.add(pageListAcrToDept);
            datas.add(notesListAcrToDept);
            datas.add(treatmentListAcrToDept);
            datas.add(upcListAcrToDept);
            datas.add(itemCodeListAcrToDept);
            datas.add(descListAcrToDept);
            datas.add(subDeptListAcrToDept);
            datas.add(limitListAcrToDept);
            datas.add(XforListAcrToDept);
            datas.add(priceListAcrToDept);
            datas.add(couponListAcrToDept);
            datas.add(adzoneListAcrToDept);
            datas.add(promoDescListAcrToDept);

            for (int column = 0; column < 13; column++) {
                List <String> list=datas.get(column);
            int index=0;
            for (int i = 0, prev = -1; i < list.size(); i++) {
                if (i == list.size() - 1 || ! list.get(i).equals(list.get(i + 1))) {
                    //System.out.printf("number: %d, count: %d%n", list.get(i), i - prev);


                    for(int pos=0;pos<i - prev;pos++){
                        int posi=index+pos;
                    Cell cell=  row[posi].createCell(column);
                    cell.setCellStyle((CellStyle) cs);
                    cell.setCellValue(list.get(i));
                    }
                    int startrowpos=index+8;
                    int endrowpos=index+8+(i - prev)-1;
                    if(startrowpos==endrowpos){
                        LOG.info("don't merge");
                    }else{
                    CellRangeAddress cellRangeAddress = new CellRangeAddress(startrowpos, endrowpos,
                            column, column);
                    sheet.addMergedRegion(cellRangeAddress);
                    }
                    index=index+(i - prev);
                    prev = i;
                }
            }
        }

【问题讨论】:

  • 这个问题举例说明了为什么Minimal, Complete, and Verifiable example 是必要的。在实际的apache poi 版本(3.15)中,垂直对齐通常也适用于合并区域。那么为什么它在您的特殊情况下不起作用?由于您的示例代码不完整且无法验证,因此除了您之外没有其他人可以知道。

标签: java excel apache-poi


【解决方案1】:

最后,我解决了这个问题。我在这里发布更改,因为它可能对其他人有所帮助。

     for (int column = 0; column < 13; column++) {
                List <String> list=datas.get(column);
            int index=0;
            for (int i = 0, prev = -1; i < list.size(); i++) {
                if (i == list.size() - 1 || ! list.get(i).equals(list.get(i + 1))) {
                    //System.out.printf("number: %d, count: %d%n", list.get(i), i - prev);

                    int posi=0;
                    for(int pos=0;pos<i - prev;pos++){
                        if(pos==0){
                            posi=index+pos;
                        }
                    }
                    int startrowpos=index+8;
                    int endrowpos=index+8+(i - prev)-1;
                    if(startrowpos==endrowpos){
                        LOG.info("don't merge");
                        Cell cell=  row[posi].createCell(column);
                        cell.setCellStyle((CellStyle) cs);
                        cell.setCellValue(list.get(i));
                    }else{
                    CellRangeAddress cellRangeAddress = new CellRangeAddress(startrowpos, endrowpos,
                            column, column);
                    sheet.addMergedRegion(cellRangeAddress);
                        Cell cell=  row[posi].createCell(column);
                        cell.setCellStyle((CellStyle) cs);
                        cell.setCellValue(list.get(i));
                    }
                    index=index+(i - prev);
                    prev = i;
                }
            }
        }

【讨论】:

    猜你喜欢
    • 2016-11-16
    • 2014-12-05
    • 2015-06-22
    • 2021-01-27
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    相关资源
    最近更新 更多