EsayPoi模板导出

news/2024/7/21 5:29:44 标签: EsayPoi, 导出, poi, Excel

1.首先导入jar包

  <!--EasyPoi导入导出-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.0.3</version> </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.0.3</version>
        </dependency>
        <!-- 文件上传 -->

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>

 

2.导入工具类

package com.zzf.finals.utiles;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

public class ExcelUtiles {
    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,
                                   String fileName, boolean isCreateHeader, HttpServletResponse response){
        ExportParams exportParams = new ExportParams(title, sheetName);
        exportParams.setCreateHeadRows(isCreateHeader);
        defaultExport(list, pojoClass, fileName, response, exportParams);
    }

    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,
                                   HttpServletResponse response){
        defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
    }

    public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){
        defaultExport(list, fileName, response);
    }

    private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName,
                                      HttpServletResponse response, ExportParams exportParams) {
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
        if (workbook != null); downLoadExcel(fileName, response, workbook);
    }

    private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            //throw new NormalException(e.getMessage());
        }
    }

    private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
        Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
        if (workbook != null);
        downLoadExcel(fileName, response, workbook);
    }

    public static <T> List<T> importExcel(String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass){
        if (StringUtils.isBlank(filePath)){
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
        }catch (NoSuchElementException e){
            //throw new NormalException("模板不能为空");
        } catch (Exception e) {
            e.printStackTrace();
            //throw new NormalException(e.getMessage());
        } return list;
    }

        public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){
        if (file == null){ return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        }catch (NoSuchElementException e){
           // throw new NormalException("excel文件不能为空");
        } catch (Exception e) {
            //throw new NormalException(e.getMessage());
            System.out.println(e.getMessage());
        }
        return list;
    }

}

3.demo代码

	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public void test(HttpServletResponse response) throws IOException {
		TemplateExportParams params = new TemplateExportParams("excel/test.xlsx");
		List<Map<String, String>> List = new ArrayList<Map<String,String>>();

		for(int i=1;i<=9;i++)
		{
			Map<String, String> map = new HashMap<String, String>();
			for(int n=1;n<=9;n++)
			{
				map.put("n"+n, (i*n)+"");
			}
			List.add(map);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("lists", List);
		Workbook workbook = ExcelExportUtil.exportExcel(params, map);

		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-Type", "application/vnd.ms-excel");
		response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("9*9乘法表", "UTF-8"));
		workbook.write(response.getOutputStream());
	}

4.excel的导出模板

这个模板的路径是在resources目录下的excel文件夹中。

Ok,接下来进行测试一下

Ok这样就不需要POI的样式了~

文档地址:http://easypoi.mydoc.io/

参考:https://www.jianshu.com/p/5d67fb720ece


http://www.niftyadmin.cn/n/1457146.html

相关文章

多线程与高并发基本概念

1.同步&#xff08;Synchronous&#xff09;与异步&#xff08;Asynchronous&#xff09; 同步和异步通常形容一次方法的调用。同步方法调用开始后调用者必须等到方法调用返回才能进行后续行为。异步方法则像一个消息的传递&#xff0c;调用方法后立即返回而方法体则在后台继续…

多线程的基本操作(1)

线程和进程一样分为五个阶段&#xff1a;创建、就绪、运行、阻塞、终止。 1、新建状态&#xff08;New&#xff09;&#xff1a;新创建了一个线程对象。2、就绪状态&#xff08;Runnable&#xff09;&#xff1a;线程对象创建后&#xff0c;其他线程调用了该对象的start()方法…

多线程的基本操作 (2)

1.volatile与Java内存模型 在Java中Java的内存模型是围绕着原子性&#xff0c;有序性和可见性展开的。 当在Java中用volatile去声明一个变量时&#xff0c;相当于告诉虚拟机这个变量时容易被程序和线程修改&#xff0c;所以为了确保这个变量被修改后应用程序范围内所有的线程…

JDK并发包(1)

1.重入锁 重入锁可以替代synchronized关键字。在JDK5.0中重入锁性能好于synchronized但是从JDK6.0开始&#xff0c;JDK对synchronized做了大量优化是得两者性能差距不多。 重入锁使用java.util.concurrent.locks.ReentrantLock类实现&#xff1a; public class ReetrantLock…

JDK并发包(2)

1.允许多个线程同时访问&#xff1a;信号量&#xff08;Semaphore&#xff09; 从广义的来说&#xff0c;信号量是对锁的扩展。无论是内部锁synchronized还是重入锁ReentrantLock一次都只允许一个线程访问一个资源&#xff0c;而信号量可以指定多个线程&#xff0c;同时访问某…

JDK并发包(3)

1.循环栅栏&#xff1a;CyclicBarrier CyclicBarrier是另一种多线程并发控制实用工具。它和CountDownLatch非常类似&#xff0c;它也可以实现线程间的计数等待&#xff0c;而且他的功能比CountDownLatch更加复杂强大。假如将计数器设为10&#xff0c;那么在凑足10个线程后&…

JDK并发包(线程池)(1)

1.什么是线程池 为了避免系统频繁地创建和销毁线程&#xff0c;我们可以让创建的线程进行复用。如同数据库连接池一样当系统使用数据库时不是创建一个新的连接&#xff0c;而是从连接池中获取一个可用的连接&#xff0c;反之当需要关闭连接是&#xff0c;并不是真的进行关闭连…

JDK并发包(线程池)(2)

在线程池中寻找堆栈 首先来看下这段代码 package thread.pool;import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit;public class Demo {public static class DivTask implements Runnabl…