poi-tl的区块对的循环功能

这个例子是官网上的 10.4野生动物现状
但是跟官网写的不太一样 他用的自己写的工厂类
我是用的他推荐的数据模型
Word模板不改变 用数据模型可以更好的理解虽然很麻烦
Poi-tl官网地址

package com.poi.template;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.ChartMultiSeriesRenderData;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.SeriesRenderData;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类说明: 
* 〈〉 * * @Author: wlt * @Date: 2021/1/14 11:23 */
@Slf4j public class AnimalTemplate { public static byte[] getLocalByteArray(File res) { Path path = res.toPath(); try { return Files.readAllBytes(path); } catch (IOException e) { log.error("readAllBytes error", e); } return null; } @Test public void animalTemplate() throws IOException { Map<String, Object> map = new HashMap<>(); map.put("name","大象"); ChartMultiSeriesRenderData chart = new ChartMultiSeriesRenderData(); chart.setChartTitle("大象生存现状"); chart.setCategories(new String[] { "2018年", "2019年", "2020年"}); List<SeriesRenderData> seriesRenderData = new ArrayList<>(); //柱状图 SeriesRenderData series1 = new SeriesRenderData("成年象", new Integer[] { 500, 600, 700 }); SeriesRenderData series2 = new SeriesRenderData("幼象", new Integer[] { 200, 300, 400 }); SeriesRenderData series3 = new SeriesRenderData("全部", new Integer[] { 700, 900, 1100 }); seriesRenderData.add(series1); seriesRenderData.add(series2); seriesRenderData.add(series3); chart.setSeriesDatas(seriesRenderData); map.put("chart", chart); Map<String, Object> map2 = new HashMap<>(); map2.put("name","长颈鹿"); //TODO 这个地方如果用PictureRenderData(int width, int height, String path) Word模板必须得写{{@picture}} 否则就报错 // map2.put("picture", new PictureRenderData(100, 120,"src/test/resources/lu.png")); File file=new File("src/main/resources/lu.png"); byte[] localByteArray = AnimalTemplate.getLocalByteArray(file); map2.put("picture", new PictureRenderData(80, 100, "src/main/resources/lu.png", localByteArray)); ChartMultiSeriesRenderData chart2 = new ChartMultiSeriesRenderData(); chart2.setChartTitle("长颈鹿生存现状"); chart2.setCategories(new String[] { "2018年", "2019年", "2020年"}); //柱状图 List<SeriesRenderData> seriesRenderData2 = new ArrayList<>(); SeriesRenderData series4 = new SeriesRenderData("成年鹿", new Integer[] { 500, 600, 700 }); SeriesRenderData series5 = new SeriesRenderData("幼鹿", new Integer[] { 200, 300, 400 }); seriesRenderData2.add(series4); seriesRenderData2.add(series5); chart2.setSeriesDatas(seriesRenderData2); map2.put("chart", chart2); List<Map<String, Object>> animals = new ArrayList<Map<String, Object>>(); animals.add(map); animals.add(map2); Map<String, Object> stringObjectHashMap = new HashMap<>(); stringObjectHashMap.put("animals", animals); XWPFTemplate.compile("src/main/resources/animal.docx").render(stringObjectHashMap).writeToFile("src/test/resources/outTemplate/outTemplate.docx"); } }

你可能感兴趣的:(Java基础编程,笔记,java)