package cn.report.template.sam;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.report.template.poi.XWPFTemplate;
import cn.report.template.poi.config.Configure;
import cn.report.template.poi.plugin.table.LoopRowTableRenderPolicy;


/** 动态合并单元格与添加颜色
	https://blog.csdn.net/weixin_43872218/article/details/114885806
	https://www.csdn.net/tags/OtDaYgzsODM1OTYtYmxvZwO0O0OO0O0O.html

	https://blog.csdn.net/weixin_59158648/article/details/118415467
*/

public class WordPoiTest {
	public static void main(String[]args) throws IOException {
        WordPoiTest wordPoiTest=new WordPoiTest();
        wordPoiTest.test();
 
    }
    public void test() throws IOException 
    {
       // LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        Configure config = Configure.builder()
                	.bind("tables_2", new CustomTableRenderPolicy())
                	.build();
        XWPFTemplate template = XWPFTemplate.compile("C:/bjtu/temp/output.docx",config);
        Map<String, Object> map = new HashMap<>();
        map.put("title", "Hi, poi-tl Word模板引擎");
        
        /**表中每行数据*/
        //第一行
        List<TableData> listList = new ArrayList<>();
        TableData tables1=new TableData("type1",1,"占山占山占山占山占山占山占山占占山占山占山占山占山山占山占山","123456");
        TableData tables2=new TableData("type1",2,"占山","123456");
        TableData tables3=new TableData("type1",3,"占山","123456");
        listList.add(tables1);
        listList.add(tables2);
        listList.add(tables3);
        
        //第二行
        TableData tables4=new TableData("type2",4,"占山","123456");
        TableData tables5=new TableData("type2",5,"占山占山占山占山占山占山占山占占山占山占山占山占山山占山占山","123456");
        TableData tables6=new TableData("type2",6,"占山","123456");
        listList.add(tables4);
        listList.add(tables5);
        listList.add(tables6);
        
        //第三行
        TableData tables7=new TableData("type3",3,"占山","123456");
        listList.add(tables7);
        
        Map<String, List<?>> data_array=new HashMap<String, List<?>>();
        data_array.put("data_list",listList);
        
        /**同列中需要合并行*/
        List< Map<String, Integer>> row_map_list = new ArrayList<>();
        Map<String, Integer> row_map = new HashMap<String, Integer>();
        row_map.put("fromrow",1);//起始行
        row_map.put("torow",3);//结束行
        row_map_list.add(row_map);
        row_map = new HashMap<String, Integer>();
        row_map.put("fromrow",4);
        row_map.put("torow",6);
        row_map_list.add(row_map);
        data_array.put("row_map_list",row_map_list);
        
        map.put("tables_2",data_array);
 
 
        template.render(map);
        template.writeAndClose(new FileOutputStream("C:/bjtu/temp/new_output.docx"));
    }

}
