java如何在后台生成echarts图表并将图片插入到word文档中,完整demo
- echarts
- 时间:2023-05-23 12:42
- 3140人已阅读
简介
下面是一个完整的Java示例代码,可以将生成echarts图表并将图片插入到Word文档中并保存到本地磁盘。import com.github.abel533.echarts.Options;import com.github.abel533.echarts.axis.CategoryAxis;import com.github.abel533.echarts.axi
🔔🔔好消息!好消息!🔔🔔
有需要的朋友👉:微信号
下面是一个完整的Java示例代码,可以将生成echarts图表并将图片插入到Word文档中并保存到本地磁盘。
1:添加相关依赖:
<dependency> <groupId>com.github.abel533</groupId> <artifactId>echarts-java</artifactId> <version>2.3.3</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency>
2:代码
import com.github.abel533.echarts.Options; import com.github.abel533.echarts.axis.CategoryAxis; import com.github.abel533.echarts.axis.ValueAxis; import com.github.abel533.echarts.code.Trigger; import com.github.abel533.echarts.json.GsonOption; import com.github.abel533.echarts.series.Bar; import com.github.abel533.echarts.util.EnhancedOption; import com.github.abel533.echarts.util.GsonUtil; import com.github.abel533.echarts.util.MapDataUtil; import com.github.abel533.echarts.util.Prepare; import com.github.abel533.echarts.util.TestUtil; import com.github.abel533.echarts.util.Theme; import org.apache.poi.util.Units; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class EchartsToWord { public static void main(String[] args) throws Exception { // 生成echarts图表 Options options = generateOptions(); // 将echarts图表转换为本地图片文件 BufferedImage bufferedImage = EnhancedOption.toBufferedImage(options); File chartFile = new File("chart.png"); TestUtil.saveAsFile(bufferedImage, "png", chartFile); // 插入图片到Word文档中 XWPFDocument document = new XWPFDocument(); XWPFTable table = document.createTable(); XWPFTableRow row = table.getRow(0); row.getCell(0).setText("学生分数统计图表"); row.addNewTableCell().setText(""); XWPFParagraph p = row.getCell(1).addParagraph(); XWPFRun r = p.createRun(); String imgFile = "chart.png"; int imgFormat = XWPFDocument.PICTURE_TYPE_PNG; int id = document.getNextPicNameNumber(imgFormat); r.addPicture(new FileInputStream(imgFile), imgFormat, imgFile, Units.toEMU(450), Units.toEMU(300)); // 保存Word文件到本地 FileOutputStream out = new FileOutputStream(new File("output.docx")); document.write(out); out.close(); document.close(); System.out.println("输出文件成功!"); } private static Options generateOptions() { final MapDataUtil.ValueFormatter<Integer> integerFormatter = new MapDataUtil.ValueFormatter<Integer>() { @Override public String format(Integer value) { return "100%"; } @Override public boolean isFormatter(Integer value) { return true; } }; final MapDataUtil.ValueFormatter<Bar> barFormatter = new MapDataUtil.ValueFormatter<Bar>() { @Override public String format(Bar value) { return value.getName() + ": " + value.getValue(); } }; final MapDataUtil.ValueFormatter<Map<String, Object>> formatter = new MapDataUtil.ValueFormatter<Map<String, Object>>() { @Override public String format(Map<String, Object> value) { return "学生:" + value.get("xAxis") + ", 分数:" + value.get("yAxis"); } }; MapDataUtil.registerFormatter(Integer.class, integerFormatter); MapDataUtil.registerFormatter(Map.class, formatter); MapDataUtil.registerFormatter(Bar.class, barFormatter); //使用下面方式创建Option可以快速测试 Map<String, Object> map = new HashMap<String, Object>(); map.put("tooltip.trigger", Trigger.axis); map.put("tooltip.formatter", "{b}分数:{c}"); map.put("legend.data", new String[]{"学生分数"}); map.put("calculable", true); Map<String, Object> xAxis = new HashMap<String, Object>(); xAxis.put("type", "category"); List<String> categoryData = new ArrayList<String>(); categoryData.add("小明"); categoryData.add("小红"); categoryData.add("小张"); categoryData.add("小李"); xAxis.put("data", categoryData); map.put("xAxis", xAxis); Map<String, Object> yAxis = new HashMap<String, Object>(); yAxis.put("type", "value"); map.put("yAxis", yAxis); List<Map<String, Object>> series = new ArrayList<Map<String, Object>>(); Map<String, Object> bar = new HashMap<String, Object>(); bar.put("name", "学生分数"); bar.put("type", "bar"); List<Integer> data = new ArrayList<Integer>(); data.add(85); data.add(72); data.add(92); data.add(78); bar.put("data", data); series.add(bar); map.put("series", series); GsonOption option = GsonUtil.mapToObject(map, GsonOption.class); option.color(Theme.Chalk.get().getAllColor().toArray(new String[0])); Prepare.prepare(option); return option; } }
这个示例代码使用echarts-java库生成一个柱形图,并将其转换为一个BufferedImage对象,然后使用POI将该图片插入到Word文档中,并保存到本地磁盘中。你可以根据自己的需要修改这个示例代码以适应您的业务需求。