是否可以在 java poi 中以单元格内的中角画一条线?

Is it possible to draw a line in java poi at mid-angle within a cell?

我想通过在单元格中画一条线来画下划线和顶线,但我不知道怎么做。我需要画两条直线来实现它。

Sheet sheet = workbook.getSheetAt(2);
        XSSFDrawing patriarch = (XSSFDrawing) sheet.createDrawingPatriarch();
        XSSFClientAnchor regionr = patriarch.createAnchor(0, 0, 1, 1, 5, 5, 6, 6);
        XSSFSimpleShape region1Shapevr = patriarch.createSimpleShape(regionr);
        region1Shapevr.setLineStyleColor(10, 10, 10);
        region1Shapevr.setFillColor(90, 10, 200);
        region1Shapevr.setLineWidth(1);
        region1Shapevr.setLineStyle(0);
        region1Shapevr.setShapeType(ShapeTypes.LINE);

当前代码给出以下结果

这是想要的结果

如前所述,给您的主要建议是使用单元格边框线而不是线条形状。单元格边框线是电子表格的主要特征之一。形状不是。这就是形状比单元格边框线更复杂的原因。

但当然可以绘制线条形状。

形状的位置和大小由锚点决定。可以考虑在单元格上的绘图层中浮动的形状。锚点将它们锚定在单元格边缘。根据它们所锚定的单元格的位置,形状也会被拉伸或压缩。所以大小也是由锚决定的。

A ClientAnchor 具有以下属性:

Col1 = 形状的左上边缘锚定在该列的左边缘

第 1 行 = 形状的左上边缘被锚定在该行的上边缘

Col2 = 形状的右下边缘被锚定在该列的左边缘

第 2 行 = 形状的右下边缘被锚定在该行的上边缘

Dx1 = delta x 将形状的左上边缘从 Col1 的左边缘移开

Dy1 = delta y 将形状的左上边缘从 Row1 的上边缘移开

Dx2 = delta x 将形状的右下边缘从 Col2 的左边缘移开

Dy2 = delta y 将形状的右下边缘从 Row2 的上边缘移开

注意,dx和dy的计量单位是EMU(英制单位)。 Units 可以正确处理那些奇怪的测量单位。

完整示例:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.util.Units;

import java.io.FileOutputStream;

class CreateExcelLineShapesParallel {
    
 static void drawLine(XSSFDrawing xssfdrawing, XSSFClientAnchor xssfanchor) {
  XSSFSimpleShape xssfshape = xssfdrawing.createSimpleShape(xssfanchor);
  xssfshape.setShapeType(ShapeTypes.LINE);
  xssfshape.setLineWidth(1);
  xssfshape.setLineStyle(0);
  xssfshape.setLineStyleColor(0, 0, 0);
 }
 
 static ClientAnchor getAnchorHorizontalFromCell(CreationHelper helper, Cell cell) {
  //anchor determines the size of the line shape to be from 
  //upper left edge of cell to upper left edge of next cell in row
  ClientAnchor anchor = helper.createClientAnchor();
  anchor.setCol1(cell.getColumnIndex());
  anchor.setRow1(cell.getRowIndex()); 
  anchor.setCol2(cell.getColumnIndex()+1);
  anchor.setRow2(cell.getRowIndex());
  //dx and dy in anchor to shift it away from the edges of the cell
  //all initialized to 0
  anchor.setDx1(0);
  anchor.setDx2(0);
  anchor.setDy1(0);
  anchor.setDy2(0);
  return anchor;
 }

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

  Workbook workbook = new XSSFWorkbook(); String filePath = "./CreateExcelLineShapesParallel.xlsx";
  
  CellStyle style = workbook.createCellStyle();
  style.setAlignment(HorizontalAlignment.CENTER);
  style.setVerticalAlignment(VerticalAlignment.CENTER);
    
  Sheet sheet = workbook.createSheet("Sheet1");
  
  int rowHeightInPt = 30;
  int lineMarginTopAndBottomInPt = 5;
  
  Row row = sheet.createRow(5);
  row.setHeightInPoints(rowHeightInPt);
  Cell cell = row.createCell(5);
  cell.setCellValue("Hello");
  cell.setCellStyle(style);

  CreationHelper helper = workbook.getCreationHelper();
  Drawing drawing = sheet.createDrawingPatriarch();

  ClientAnchor anchor = getAnchorHorizontalFromCell(helper, cell);
  //dx and dy in anchor to shift it away from the edges of the cell
  //measurement unit for dx and dy is EMU (English Metric Unit)
  anchor.setDy1(Units.toEMU(lineMarginTopAndBottomInPt));
  anchor.setDy2(Units.toEMU(lineMarginTopAndBottomInPt));
  //draw a line positioned by the anchor.
  drawLine((XSSFDrawing)drawing, (XSSFClientAnchor)anchor);
  
  anchor = getAnchorHorizontalFromCell(helper, cell);
  anchor.setDy1(Units.toEMU(rowHeightInPt-lineMarginTopAndBottomInPt));
  anchor.setDy2(Units.toEMU(rowHeightInPt-lineMarginTopAndBottomInPt));
  drawLine((XSSFDrawing)drawing, (XSSFClientAnchor)anchor);
  
  FileOutputStream out = new FileOutputStream(filePath);
  workbook.write(out);
  out.close();
  workbook.close();

 }
}