com.eworkcloud.web.util.ExcelUtils Maven / Gradle / Ivy
package com.eworkcloud.web.util;
import com.eworkcloud.excel.enmus.ExcelType;
import com.eworkcloud.excel.model.ExportParams;
import com.eworkcloud.excel.model.ImportParams;
import com.eworkcloud.excel.util.ExportUtils;
import com.eworkcloud.excel.util.ImportUtils;
import com.eworkcloud.excel.util.PoiUtils;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.List;
/**
* Excel 导入|导出|下载
*
* @author 马恩全
*/
public abstract class ExcelUtils {
/**
* 创建EXCEL对象
*
* @param inputStream InputStream
* @return EXCEL对象
*/
@SneakyThrows
public static Workbook createWorkbook(InputStream inputStream) {
return WorkbookFactory.create(inputStream);
}
/**
* 创建EXCEL对象
*
* @param multipart MultipartFile
* @return EXCEL对象
*/
@SneakyThrows
public static Workbook createWorkbook(MultipartFile multipart) {
InputStream inputStream = multipart.getInputStream();
return WorkbookFactory.create(inputStream);
}
/**
* 创建EXCEL对象
*
* @param type 类型
* @return EXCEL对象
*/
public static Workbook createWorkbook(ExcelType type) {
return PoiUtils.createWorkbook(type);
}
/**
* 导入Excel
*
* @param workbook Workbook
* @param clazz 类
* @param params 参数
* @param 类型
* @return 指定类型列表
*/
public static List importExcel(Workbook workbook, Class clazz, ImportParams params) {
return ImportUtils.importExcel(workbook, clazz, params);
}
/**
* 导入Excel
*
* @param inputStream InputStream
* @param clazz 类
* @param params 参数
* @param 类型
* @return 指定类型列表
*/
public static List importExcel(InputStream inputStream, Class clazz, ImportParams params) {
return importExcel(createWorkbook(inputStream), clazz, params);
}
/**
* 导入Excel
*
* @param multipart MultipartFile
* @param clazz 类
* @param params 参数
* @param 类型
* @return 指定类型列表
*/
public static List importExcel(MultipartFile multipart, Class clazz, ImportParams params) {
return importExcel(createWorkbook(multipart), clazz, params);
}
/**
* 导入Excel
*
* @param inputStream InputStream
* @param clazz 类
* @param 类型
* @return 指定类型列表
*/
public static List importExcel(InputStream inputStream, Class clazz) {
return importExcel(inputStream, clazz, new ImportParams());
}
/**
* 导入Excel
*
* @param multipart MultipartFile
* @param clazz 类
* @param 类型
* @return 指定类型列表
*/
public static List importExcel(MultipartFile multipart, Class clazz) {
return importExcel(multipart, clazz, new ImportParams());
}
/**
* 导出Excel
*
* @param workbook Workbook
* @param dataSet 数据
* @param clazz 类
* @param params 参数
* @param 类型
*/
public static void attachSheet(Workbook workbook, Collection dataSet, Class clazz, ExportParams params) {
ExportUtils.exportExcel(workbook, dataSet, clazz, params);
}
/**
* 导出Excel
*
* @param workbook Workbook
* @param dataSet 数据
* @param clazz 类
* @param 类型
*/
public static void attachSheet(Workbook workbook, Collection dataSet, Class clazz) {
attachSheet(workbook, dataSet, clazz, new ExportParams());
}
/**
* 导出Excel
*
* @param dataSet 数据
* @param clazz 类
* @param params 参数
* @param 类型
* @return Workbook
*/
public static Workbook exportExcel(Collection dataSet, Class clazz, ExportParams params) {
Workbook workbook = createWorkbook(params.getExcelType());
ExportUtils.exportExcel(workbook, dataSet, clazz, params);
return workbook;
}
/**
* 导出Excel
*
* @param dataSet 数据
* @param clazz 类
* @param 类型
* @return Workbook
*/
public static Workbook exportExcel(Collection dataSet, Class clazz) {
return exportExcel(dataSet, clazz, new ExportParams());
}
/**
* 下载Excel
*
* @param response 响应对象
* @param workbook Excel
* @param filename 文件名
*/
@SneakyThrows
public static void downLoad(HttpServletResponse response, Workbook workbook, String filename) {
filename = URLEncoder.encode(filename, "UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
workbook.write(response.getOutputStream());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy