锚点决定了形状的大小 . 对于线形,默认情况下,线从第一个锚单元的左上边缘到左上边缘加上最后一个锚单元的 dx 和 dy . 第一锚单元是锚的左上方的单元,而最后的锚单元是锚的右下方的单元 . 因此,默认情况下,线条形状将从左上角到右下角 .
如果我们希望它从左下角到右上角,那么我们必须翻转形状 .
如果我们在 Excel 中从左下角到右上角画一条线,然后查看存储的 /xl/drawings/drawing1.xml ,我们将看到这个手动绘制的线条形状也被翻转 .
两行示例:
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import java.io.FileOutputStream;
class CreateExcelLineShapes {
public static void main(String[] args) throws Exception{
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
CreationHelper helper = workbook.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
//Anchor B2:C4
//This determines the size of the line shape to be from
//upper left edge of B2 to upper left edge of C4 plus dx and dy.
//Since dx and dy are both 0, this is the bottom right edge of B3.
anchor.setCol1(1);
anchor.setRow1(1);
anchor.setCol2(2);
anchor.setRow2(3);
//From here on XSSF only.
XSSFDrawing xssfdrawing = (XSSFDrawing)drawing;
XSSFClientAnchor xssfanchor = (XSSFClientAnchor)anchor;
//Draw a line from upper left edge of B2 to upper left edge of C4 = bottom right edge of B3.
//This is the default.
XSSFSimpleShape xssfshape = xssfdrawing.createSimpleShape(xssfanchor);
xssfshape.setShapeType(ShapeTypes.LINE);
xssfshape.setLineWidth(4);
xssfshape.setLineStyle(0);
xssfshape.setLineStyleColor(0, 0, 0);
//This sets the arrow line end type:
xssfshape.getCTShape().getSpPr().getLn().addNewTailEnd().setType(
org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType.TRIANGLE);
//Again draw a line from upper left edge of B2 to upper left edge of C4 = bottom right edge of B3.
xssfshape = xssfdrawing.createSimpleShape(xssfanchor);
xssfshape.setShapeType(ShapeTypes.LINE);
xssfshape.setLineWidth(4);
xssfshape.setLineStyle(0);
xssfshape.setLineStyleColor(0, 0, 0);
//Now flip this vertically.
//So it now will to be from bottom left edge of B3 to upper left edge of B2.
xssfshape.getCTShape().getSpPr().getXfrm().setFlipV(true);
xssfshape.getCTShape().getSpPr().getLn().addNewTailEnd().setType(
org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType.TRIANGLE);
workbook.write(new FileOutputStream("CreateExcelLineShapes.xlsx"));
workbook.close();
}
}
结果: