该文将介绍如何使用 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);
}
}
效果图: