该文将介绍如何使用 Spire.XLS for Java插入图片到Excel图表
import com.spire.xls.*;
import com.spire.xls.core.IPictureShape;
import com.spire.xls.core.IShape;
import java.awt.*;
public class addPictureToChart {
public static void main(String[] args) {
//加载示例文档
Workbook workbook = new Workbook();
workbook.loadFromFile("Sample.xlsx");
//获取第一个工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//获取第一个图表
Chart chart = sheet.getCharts().get(0);
//插入图表并设置样式
IShape picture = chart.getShapes().addPicture("48.png");
((IPictureShape) picture).getLine().setDashStyle(ShapeDashLineStyleType.DashDotDot);
((IPictureShape) picture).getLine().setForeColor(Color.blue);
//保存文档
String result = "output/AddPictureToChart.xlsx";
workbook.saveToFile(result, ExcelVersion.Version2010);
}
}
效果图: