0

我正在使用 Apache POI 创建 excel 导出文件(带有 XSSF 的.xlsx)。我在单元格之间的边界上有一个问题。

我需要将几个单元格合并到一行中,并且在该行中,我需要将一个文本向左对齐,另一个文本向右对齐,例如:

在此处输入图像描述

但两者之间没有边界。

为了获得您在图像中看到的内容,我使用了两个合并区域,一个将文本对齐到左侧,另一个将文本对齐到右侧,我不确定是否有更好/更方便的方法这个与否,如果你知道,请将它写在答案中,但是对于我现在的方法,问题在于那个边框,我可以删除它吗?我尝试将第一个合并区域的右边框NONE设置为,并将第二个合并区域的左边框设置NONE为,但它不起作用。

我该如何处理?

4

2 回答 2

2
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.OutputStream;

public class Test {

    public static void main(String[] args) throws Exception {
        try(Workbook wb = new XSSFWorkbook(); OutputStream fos = new FileOutputStream("test.xlsx")){
            Sheet sheet = wb.createSheet();

            Font font = wb.createFont();
            font.setBold(true);
            font.setFontHeightInPoints((short)11);

            CellRangeAddress leftCellRangeAddress = new CellRangeAddress(
                0,
                1,
                CellReference.convertColStringToIndex("A"),
                CellReference.convertColStringToIndex("E")
            );
            sheet.addMergedRegion(leftCellRangeAddress);
            CellRangeAddress rightCellRangeAddress = new CellRangeAddress(
                0,
                1,
                CellReference.convertColStringToIndex("F"),
                CellReference.convertColStringToIndex("H")
            );
            sheet.addMergedRegion(rightCellRangeAddress);
            Row row = sheet.createRow(0);

            Cell leftCell = row.createCell(CellReference.convertColStringToIndex("A"));
            leftCell.setCellValue("LEFT");
            leftCell.getCellStyle().setFont(font);
            CellUtil.setVerticalAlignment(leftCell, VerticalAlignment.CENTER);
            CellUtil.setAlignment(leftCell, HorizontalAlignment.LEFT);
            RegionUtil.setBorderRight(BorderStyle.THIN, leftCellRangeAddress, sheet);
            RegionUtil.setRightBorderColor(IndexedColors.WHITE.getIndex(), leftCellRangeAddress, sheet);

            Cell rightCell = row.createCell(CellReference.convertColStringToIndex("F"));
            rightCell.setCellValue("RIGHT");
            rightCell.getCellStyle().setFont(font);
            CellUtil.setVerticalAlignment(rightCell, VerticalAlignment.CENTER);
            CellUtil.setAlignment(rightCell, HorizontalAlignment.RIGHT);
            RegionUtil.setBorderLeft(BorderStyle.THIN, rightCellRangeAddress, sheet);
            RegionUtil.setLeftBorderColor(IndexedColors.WHITE.getIndex(), rightCellRangeAddress, sheet);

            wb.write(fos);
        }
    }
}

在此处输入图像描述

如果你想要底部的灰色边框,你可以添加

CellRangeAddress firstRowRegion = new CellRangeAddress(
    0,
    1,
    CellReference.convertColStringToIndex("A"),
    CellReference.convertColStringToIndex("H")
);
RegionUtil.setBorderBottom(BorderStyle.THICK, firstRowRegion, sheet);
RegionUtil.setBottomBorderColor(IndexedColors.GREY_40_PERCENT.getIndex(), firstRowRegion, sheet);

你会得到

在此处输入图像描述

于 2020-09-10T14:41:00.823 回答
1

您的屏幕截图显示的是网格线而不是边界线。这是电子表格的区别。网格线显示在应用程序窗口中只是为了更好地查看单元格。它们不会被打印出来。

如果您不想看到网格线,您可以切换到不显示整个工作表的网格线,我不建议这样做,或者您可以设置白色边框线,然后覆盖一些网格线。

既然你已经标记apache-poi-4了,我将展示一个完整的例子,它使用高级方法CellUtilPropertyTemplate产生你想要的东西。

代码:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

class CreateExcelLeftRight {

 public static void main(String[] args) throws Exception {

  try (Workbook workbook = new XSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {

   //create font with bigger size
   Font font = workbook.createFont();
   font.setFontHeightInPoints((short)24);

   Sheet sheet = workbook.createSheet(); 

   //merge A1:E2
   sheet.addMergedRegion(new CellRangeAddress(
    0, //first row (0-based)
    1, //last row  (0-based)
    0, //first column (0-based)
    4  //last column  (0-based)
   ));

   //merge F1:H2
   sheet.addMergedRegion(new CellRangeAddress(
    0, //first row (0-based)
    1, //last row  (0-based)
    5, //first column (0-based)
    7  //last column  (0-based)
   ));

   //create row 1
   Row row = sheet.createRow(0);
   //create cell A1
   Cell cell = row.createCell(0);
   cell.setCellValue("LEFT");
   CellUtil.setFont(cell, font);
   CellUtil.setVerticalAlignment(cell, VerticalAlignment.CENTER);
   //create cell F1
   cell = row.createCell(5);
   cell.setCellValue("RIGHT");
   CellUtil.setFont(cell, font);
   CellUtil.setVerticalAlignment(cell, VerticalAlignment.CENTER);
   CellUtil.setAlignment(cell, HorizontalAlignment.RIGHT);
 
   PropertyTemplate propertyTemplate = new PropertyTemplate();
   //paint all inside borders white on A1:H2
   propertyTemplate.drawBorders(new CellRangeAddress(0, 1, 0, 7), 
    BorderStyle.THIN, IndexedColors.WHITE.getIndex(), BorderExtent.INSIDE);
   //paint all bottom borders thick gray on A2:H2
   propertyTemplate.drawBorders(new CellRangeAddress(1, 1, 0, 7), 
    BorderStyle.THICK, IndexedColors.GREY_40_PERCENT.getIndex(), BorderExtent.BOTTOM);
   propertyTemplate.applyBorders(sheet);

   sheet.setActiveCell(new CellAddress(3, 0));

   workbook.write(fileout);
  }

 }
}

结果:

在此处输入图像描述

于 2020-09-10T12:46:00.973 回答