使用 xssf 库在 excel 中绘制带箭头的线条
Drawing line with arrow in excel using xssf libraries
我想以编程方式在 excel sheet 中画一条带箭头的线,我可以创建线但不能创建箭头。
这是我的代码,我需要更改我想要的东西;
XSSFSimpleShape shape = drawing.createSimpleShape(a);
shape.setShapeType(ShapeTypes.LINE);
shape.setLineWidth(1.5);
shape.setLineStyle(3);
我也尝试过使用 shape.setShapeType(ShapeTypes.LEFT_ARROW);
shape.setShapeType(ShapeTypes.RIGHT_ARROW);
和 shape.setShapeType(ShapeTypes.UP_ARROW);
,但这对我没有帮助。
这是我的:
这就是我想要的:
apache POI 中的Drawing
支持似乎不是很完整。所以需要使用底层对象。
在 Excel 中,箭头是线条的头端或尾端属性。
示例:
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTLineProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTLineEndProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndLength;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndWidth;
class ShapeArrow {
public static void main(String[] args) {
try {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
CreationHelper helper = wb.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(2);
anchor.setRow1(2);
anchor.setCol2(5);
anchor.setRow2(5);
XSSFSimpleShape shape = ((XSSFDrawing)drawing).createSimpleShape((XSSFClientAnchor)anchor);
shape.setShapeType(ShapeTypes.LINE);
shape.setLineWidth(1.5);
shape.setLineStyle(3);
shape.setLineStyleColor(0,0,255);
//apache POI sets first shape Id to 1. It should be 0.
shape.getCTShape().getNvSpPr().getCNvPr().setId(shape.getCTShape().getNvSpPr().getCNvPr().getId()-1);
CTShapeProperties shapeProperties = shape.getCTShape().getSpPr();
CTLineProperties lineProperties = shapeProperties.getLn();
CTLineEndProperties lineEndProperties = org.openxmlformats.schemas.drawingml.x2006.main.CTLineEndProperties.Factory.newInstance();
lineEndProperties.setType(STLineEndType.TRIANGLE);
lineEndProperties.setLen(STLineEndLength.LG);
lineEndProperties.setW(STLineEndWidth.LG);
lineProperties.setHeadEnd(lineEndProperties);
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
wb.write(fileOut);
} catch (IOException ioex) {
}
}
}
在 POI 中有一个 XSSFShape.getCTShape()
方法来获取 CTShape
对象。但是这样我们就迷失了 POI 文档。因此,请参阅 http://grepcode.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/drawingml/x2006/spreadsheetDrawing/CTShape.java 获取文档。
我想以编程方式在 excel sheet 中画一条带箭头的线,我可以创建线但不能创建箭头。 这是我的代码,我需要更改我想要的东西;
XSSFSimpleShape shape = drawing.createSimpleShape(a);
shape.setShapeType(ShapeTypes.LINE);
shape.setLineWidth(1.5);
shape.setLineStyle(3);
我也尝试过使用 shape.setShapeType(ShapeTypes.LEFT_ARROW);
shape.setShapeType(ShapeTypes.RIGHT_ARROW);
和 shape.setShapeType(ShapeTypes.UP_ARROW);
,但这对我没有帮助。
这是我的:
这就是我想要的:
apache POI 中的Drawing
支持似乎不是很完整。所以需要使用底层对象。
在 Excel 中,箭头是线条的头端或尾端属性。
示例:
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTLineProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTLineEndProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndLength;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndWidth;
class ShapeArrow {
public static void main(String[] args) {
try {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
CreationHelper helper = wb.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(2);
anchor.setRow1(2);
anchor.setCol2(5);
anchor.setRow2(5);
XSSFSimpleShape shape = ((XSSFDrawing)drawing).createSimpleShape((XSSFClientAnchor)anchor);
shape.setShapeType(ShapeTypes.LINE);
shape.setLineWidth(1.5);
shape.setLineStyle(3);
shape.setLineStyleColor(0,0,255);
//apache POI sets first shape Id to 1. It should be 0.
shape.getCTShape().getNvSpPr().getCNvPr().setId(shape.getCTShape().getNvSpPr().getCNvPr().getId()-1);
CTShapeProperties shapeProperties = shape.getCTShape().getSpPr();
CTLineProperties lineProperties = shapeProperties.getLn();
CTLineEndProperties lineEndProperties = org.openxmlformats.schemas.drawingml.x2006.main.CTLineEndProperties.Factory.newInstance();
lineEndProperties.setType(STLineEndType.TRIANGLE);
lineEndProperties.setLen(STLineEndLength.LG);
lineEndProperties.setW(STLineEndWidth.LG);
lineProperties.setHeadEnd(lineEndProperties);
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
wb.write(fileOut);
} catch (IOException ioex) {
}
}
}
在 POI 中有一个 XSSFShape.getCTShape()
方法来获取 CTShape
对象。但是这样我们就迷失了 POI 文档。因此,请参阅 http://grepcode.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/drawingml/x2006/spreadsheetDrawing/CTShape.java 获取文档。