Creating Shapes using Shape Groups
In this section, you will learn how to create shapes using Apache POI library.
EXAMPLE
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFChildAnchor;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFShapeGroup;
import org.apache.poi.hssf.usermodel.HSSFSimpleShape;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class XLShapeGroup {
public static void main(String args[]) throws FileNotFoundException {
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("Data Validation");
HSSFPatriarch patriarch = (HSSFPatriarch) sheet
.createDrawingPatriarch();
// Create a shape group.
HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor(0, 0,
900, 200, (short) 0, 1, (short) 1, 0));
// Create a couple of lines in the group.
HSSFSimpleShape shape1 = group.createShape(new HSSFChildAnchor(3, 3,
500, 500));
shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
((HSSFChildAnchor) shape1.getAnchor())
.setAnchor((short) 3, 3, 500, 500);
HSSFSimpleShape shape2 = group.createShape(new HSSFChildAnchor(
(short) 0, 200, 400, 600));
shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
FileOutputStream fileOut = new FileOutputStream("xls/XLShapeGroup.xls");
try {
workbook.write(fileOut);
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT
