Java Excel的模板导出

原创
2016/09/13 10:39
阅读数 1.8K

Excel的模板导出框架

jxls 1.6 pom

                <!-- https://mvnrepository.com/artifact/ .jxls/jxls-jdk1.6 -->
		<dependency>
			<groupId>org.jxls</groupId>
			<artifactId>jxls-jdk1.6</artifactId>
			<version>2.2.9</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.jxls/jxls-poi-jdk1.6 -->
		<dependency>
			<groupId>org.jxls</groupId>
			<artifactId>jxls-poi-jdk1.6</artifactId>
			<version>1.0.8</version>
		</dependency>

                <!--2.3.0版本 -->
                <dependency>
			<groupId>org.jxls</groupId>
			<artifactId>jxls</artifactId>
			<version>2.3.0</version>
		</dependency>

                <dependency>
			<groupId>org.jxls</groupId>
			<artifactId>jxls-jexcel</artifactId>
			<version>1.0.6</version>
		</dependency>

import org.jxls.area.Area;
import org.jxls.builder.AreaBuilder;
import org.jxls.builder.xls.XlsCommentAreaBuilder;
import org.jxls.common.CellRef;
import org.jxls.common.Context;
import org.jxls.expression.ExpressionEvaluator;
import org.jxls.expression.JexlExpressionEvaluator;
import org.jxls.transform.Transformer;
import org.jxls.transform.poi.PoiTransformer;
import org.jxls.util.JxlsHelper;
import org.jxls.util.TransformerFactory;

import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 public static void main(String[] args) throws  Exception {

        InputStream is = new FileInputStream(new File("jexl_custom_function_template.xlsx"));
        OutputStream os = new FileOutputStream(new File("out.xlsx"));


        //Transformer transformer = TransformerFactory.createTransformer(is, os);
        PoiTransformer transformer = PoiTransformer.createTransformer(is, os);

        ExpressionEvaluator expressionEvaluator = new VelocityExpressionEvaluator();

        transformer.getTransformationConfig().setExpressionEvaluator(expressionEvaluator);

        AreaBuilder areaBuilder = new XlsCommentAreaBuilder(transformer);
        List<Area> xlsAreaList = areaBuilder.build();
        Area xlsArea = xlsAreaList.get(0);

        List<String> strings = Arrays.asList(new String[]{"a", "b", "c","d"});

        Context context = new Context();

        context.putVar("username", "a");
        context.putVar("list", strings);

        xlsArea.applyAt(new CellRef("Sheet1!A1"), context);

        transformer.write();
    }
展开阅读全文
打赏
0
1 收藏
分享
加载中
有无遇到,EasyPOI模板导出图片存在合并单元格只填充一个单元格问题?
2019/06/26 13:22
回复
举报
更多评论
打赏
1 评论
1 收藏
0
分享
返回顶部
顶部