This paper introduces the method of operating shape (Figure) in excel by java program, including:
1. Add shape (such as setting shape type / position / size, shape color fill (monochrome / gradient / texture / picture fill), shape display or hide, shape tilt angle, adding text to shape, shape shadow, etc.)
2. Read the text and pictures in the shape
3. Delete shape (delete specified or all shapes)
Tools: fire.xls for Java
Jar file acquisition and import: through Download official website or maven download and import . The import effect is as follows:
Java code example
[example 1] add shape
import com.spire.xls.*; import com.spire.xls.core.IPrstGeomShape; import java.awt.*; public class AddShape { public static void main(String[] args) { //Load test document Workbook wb = new Workbook(); wb.loadFromFile("test.xlsx"); //Get worksheet Worksheet sheet = wb.getWorksheets().get(0); //Add ellipse IPrstGeomShape ellipse = sheet.getPrstGeomShapes().addPrstGeomShape(3,4,125,200,PrstGeomShapeType.Ellipse); ellipse.getFill().setFillType(ShapeFillType.SolidColor);//Single color filling ellipse.getFill().setForeColor(new Color(180,125,208)); ellipse.setRotation(30);//Shape rotation angle ellipse.setText("Ellipse add text");//Add text to shape //Star added IPrstGeomShape star = sheet.getPrstGeomShapes().addPrstGeomShape(3,8,180,200,PrstGeomShapeType.Star5); star.getFill().setFillType(ShapeFillType.Gradient);//Fountain Fill star.getFill().setGradientColorType(GradientColorType.Preset); star.getFill().setForeColor(Color.orange); //Add cloud shape IPrstGeomShape cloud = sheet.getPrstGeomShapes().addPrstGeomShape(12,4,175,200,PrstGeomShapeType.Cloud); cloud.getFill().setFillType(ShapeFillType.Texture);//Texture filling cloud.getFill().setTexture(GradientTextureType.WhiteMarble); cloud.setVisible(true);//Set whether the shape is visible //Set cloud shadow effect cloud.getShadow().setAngle(90); cloud.getShadow().setDistance(10); cloud.getShadow().setSize(100); cloud.getShadow().setColor(Color.GRAY); cloud.getShadow().setBlur(30); cloud.getShadow().setTransparency(1); cloud.getShadow().hasCustomStyle(); //Add rectangle shape IPrstGeomShape rect = sheet.getPrstGeomShapes().addPrstGeomShape(15,8,125,200,PrstGeomShapeType.Rect); rect.getFill().customPicture("tp.png");//Load picture fill rect.setName("Shape4");//Named shape //Save document wb.saveToFile("AddShape.xlsx"); wb.dispose(); } }
Shape add effect:
[example 2] read the text and pictures in the shape
import com.spire.xls.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class Extract { public static void main(String[] args) throws IOException { //Loading documents Workbook wb = new Workbook(); wb.loadFromFile("AddShape.xlsx"); //Get worksheet Worksheet sheet = wb.getWorksheets().get(0); //Gets the text in the specified shape String text = sheet.getPrstGeomShapes().get(0).getText(); System.out.println(text); //Get pictures in the specified shape BufferedImage image = sheet.getPrstGeomShapes().get(3).getFill().getPicture(); ImageIO.write(image,"png",new File("ExtractedImage.png")); } }
Reading results of text and picture:
[example 3] delete shape
import com.spire.xls.*; public class RemoveShape { public static void main(String[] args) { //Loading documents Workbook wb = new Workbook(); wb.loadFromFile("AddShape.xlsx"); //Get worksheet Worksheet sheet = wb.getWorksheets().get(0); //Get the specified shape,delete sheet.getPrstGeomShapes().get(1).remove();//Get and delete by index value // sheet.getPrstGeomShapes().get("Shape4").remove();//Get and get by shape name //Delete all drawings for (int i = sheet.getPrstGeomShapes().getCount()-1; i >= 0; i--) { sheet.getPrstGeomShapes().get(i).remove(); } //Save document wb.saveToFile("RemoveShape.xlsx"); wb.dispose(); } }
After running the program, you can see the shape removal effect.
(end of this paper)