Apache POI Excel 绘制形状

HSSFSimpleShape 类用于在电子表格中绘制形状。这个类提供了一个方法 setShapeType() ,它有助于绘制各种形状,如椭圆、直线、矩形等。

让我们看一个例子。

Apache POI 绘制形状示例

package com.yiidian;

import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSimpleShape;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileOutputStream;
import java.io.OutputStream;

public class ShapeExample {
    public static void main(String[] args) {
        Workbook wb = new HSSFWorkbook();
        try (OutputStream os = new FileOutputStream("yiidian.xls")) {
            Sheet sheet = wb.createSheet("Sheet");
            Row row = sheet.createRow(4); // Creating a row
            Cell cell = row.createCell(1); // Creating a cell
            HSSFPatriarch patriarch = (HSSFPatriarch) sheet.createDrawingPatriarch();
            HSSFClientAnchor  a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 0, (short) 1, 0 );
            HSSFSimpleShape shape = patriarch.createSimpleShape(a);
            shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
            wb.write(os);
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }  
}  

输出结果为:

热门文章

优秀文章