poi 合并单元格 无边框问题

 public void merge(int startrow,int endstartrow,int startColumn,int endColumn){
  sht.addMergedRegion(new CellRangeAddress(startrow, endstartrow, startColumn, endColumn));
 }

合并单元格时候  合并后的单元格部分边框会看不见。从而进行处理。

下面是处理样式的 方法。 单元格设置边框

public void setStyle(int size, int color, boolean isBold, boolean isItalic, int backgroudColor, int align, int valign)
    {
        style = wb.createCellStyle();
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setRightBorderColor(HSSFColor.BLACK.index);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setTopBorderColor(HSSFColor.BLACK.index);
        HSSFFont font = wb.createFont();
        font.setColor((short)color);
        font.setFontHeightInPoints((short)size);
        if (isBold)
        {
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        }
        else
        {
            font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        }
        font.setItalic(isItalic);
        style.setFont(font);
        style.setFillForegroundColor((short)backgroudColor);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setAlignment((short)align);
        style.setVerticalAlignment((short)valign);
    }

然而并无卵用,一直出现 单元格无边框。几个单元格都出现相同效果。总结是合并单元格的 最后一个单元格 的边框总是有一边不显示。

最后解决办法是挺简单的,只要给最后那个单元格里面放一个空字符串。问题就解决了。

ew.putData(1, 1, 1, "");

ew是放数据的。

public void putData(int shtidx, int rowidx, int colidx, Object data)
    {
        if (rowidx > bounder)
        {
            try
            {
                wb.getSheetAt(shtidx);
            }
            catch (IndexOutOfBoundsException e)
            {
                wb.createSheet();
                wb.setSheetName(shtidx, "Sheet " + (shtidx + 1));
            }
            sht = wb.getSheetAt(shtidx);
        }
        if (sht.getRow(rowidx) == null)
        {
            sht.createRow(rowidx);
        }
        row = sht.getRow(rowidx);

        if (row.getCell(colidx) == null)
        {
            row.createCell(colidx);
        }
        cell = row.getCell(colidx);
        
        if (data instanceof Double)
        {
            Double d = (Double)data;
            cell.setCellValue(d);
        }
        else if (data instanceof Integer)
        {
            Integer i = (Integer)data;
            cell.setCellValue(i);
        }
        else if (data instanceof Date)
        {
            Date d = (Date)data;
            cell.setCellValue(d);
        }
        else if (data instanceof String)
        {
            String s = (String)data;
            cell.setCellValue(s);
        }
        else
        {
            String s;
            if (data == null)
            {
                data = new String("");
            }
            s = data.toString();
            cell.setCellValue(s);
        }

        cell.setCellStyle(style);
    }
原文地址:https://www.cnblogs.com/wupeng88/p/4632487.html