You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-spms-java/src/main/java/com/glxp/api/util/Excel/ExcelHandler.java

289 lines
13 KiB
Java

package com.glxp.api.util.Excel;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.glxp.api.util.Excel.Exception.RenException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@Slf4j
@Component
public class ExcelHandler {
/**
* excel
*
* @param file
* @param clazz
* @param sheetNamesheet [sheet]
* @throws Exception
*/
public <T> List<T> importExcel(MultipartFile file, Class<T> clazz, String sheetName) throws Exception {
this.checkFile(file);
UploadDataListener<T> uploadDataListener = new UploadDataListener<>();
ExcelReaderBuilder builder = EasyExcelFactory.read(file.getInputStream(), clazz, uploadDataListener);
if (StringUtils.isEmpty(sheetName)) {
builder.sheet().doRead();
} else {
builder.sheet(sheetName).doRead();
}
return uploadDataListener.getList();
}
/**
* sheet
*
* @param multipartFile
* @param objList sheet
* @param index sheet
* @param indexList sheet
* @param <T>
* @return <T> List<List<T>>
* @throws Exception
*/
public <T> List<List<T>> importExcelsByIndex(MultipartFile multipartFile, List<T> objList, int index, List<Integer> indexList) throws Exception {
if (multipartFile == null) {
throw new RenException("文件为空");
}
List<List<T>> resultList = new LinkedList<>();
//初始化导入sheet页实体类型下标
int objListClass = 0;
for (int i = 0; i < index; i++) {
if (indexList.contains(i)) {
UploadDataListener<T> uploadDataListener = new UploadDataListener<>();
List<T> excels;
EasyExcelFactory.read(multipartFile.getInputStream(), objList.get(objListClass).getClass(), uploadDataListener).sheet(i).doRead();
excels = uploadDataListener.getList();
resultList.add(excels);
objListClass++;
}
}
return resultList;
}
/**
* sheet
*
* @param file
* @param indexsheet [030 1 2]
* @param paramssheet[index33]
* @param <T>
* @return
*/
public <T> List<List<T>> importExcels(MultipartFile file, int index, List<Object> params) throws Exception {
this.checkFile(file);
List<List<T>> resultList = new LinkedList<>();
for (int i = 0; i < index; i++) {
UploadDataListener<T> uploadDataListener = new UploadDataListener<>();
ExcelReaderBuilder builder = EasyExcelFactory.read(file.getInputStream(), params.get(i).getClass(), uploadDataListener);
builder.sheet(i).doRead();
List<T> list = uploadDataListener.getList();
resultList.add(list);
}
return resultList;
}
/**
* excel
*
* @param response
* @param dataList
* @param clazz
* @param fileName
* @param sheetNamesheet
* @throws Exception
*/
public <T> void exportExcel(HttpServletResponse response, List<T> dataList, Class<T> clazz, String fileName, String sheetName) throws Exception {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name());
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ExcelTemplateEnum.TEMPLATE_SUFFIX.getDesc());
ServletOutputStream outputStream = response.getOutputStream();
EasyExcelFactory.write(outputStream, clazz).sheet(sheetName).doWrite(dataList);
outputStream.flush();
outputStream.close();
}
/**
* sheet
*
* @param response
* @param dataList
* @param clazzMapsheet
* @param fileName
* @param <T>
* @throws Exception
*/
public <T> void exportExcels(HttpServletResponse response, List<List<?>> dataList, Map<Integer, String> clazzMap, String fileName) throws Exception {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name());
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ExcelTemplateEnum.TEMPLATE_SUFFIX.getDesc());
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
int len = dataList.get(0).size();
for (int i = 0; i < len; i++) {
List<?> objects = (List<?>) dataList.get(0).get(i);
Class<?> aClass = objects.get(0).getClass();
WriteSheet writeSheet0 = EasyExcel.writerSheet(i, clazzMap.get(i)).head(aClass).build();
excelWriter.write(objects, writeSheet0);
}
excelWriter.finish();
}
/**
* -sheet
*
* @param list
* @param object
* @param fileName
* @param templateName
* @throws Exception
*/
public <T> void exportTemplateExcels(HttpServletResponse response, List<T> list, Object object, String fileName, String templateName) throws Exception {
String template = ExcelTemplateEnum.TEMPLATE_PATH.getDesc() + File.separator + templateName + ExcelTemplateEnum.TEMPLATE_SUFFIX.getDesc();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(template);
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
ExcelWriter excelWriter = EasyExcelFactory.write(getOutputStream(fileName, response)).withTemplate(inputStream).build();
WriteSheet writeSheet0 = EasyExcelFactory.writerSheet(0).build();
excelWriter.fill(object, fillConfig, writeSheet0);
excelWriter.fill(list, fillConfig, writeSheet0);
excelWriter.finish();
}
/**
* -sheet
*
* @param list1
* @param list2
* @param object1
* @param object2
* @param fileName
* @param templateName
* @throws Exception
*/
public <T> void exportSheetTemplateExcels(HttpServletResponse response, List<T> list1, List<T> list2, Object object1, Object object2, String fileName, String templateName) throws Exception {
String template = ExcelTemplateEnum.TEMPLATE_PATH.getDesc() + File.separator + templateName + ExcelTemplateEnum.TEMPLATE_SUFFIX.getDesc();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(template);
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
ExcelWriter excelWriter = EasyExcelFactory.write(getOutputStream(fileName, response)).withTemplate(inputStream).build();
WriteSheet writeSheet0 = EasyExcelFactory.writerSheet(0).build();
WriteSheet writeSheet1 = EasyExcelFactory.writerSheet(1).build();
excelWriter.fill(object1, fillConfig, writeSheet0);
excelWriter.fill(list1, fillConfig, writeSheet0);
excelWriter.fill(object2, fillConfig, writeSheet1);
excelWriter.fill(list2, fillConfig, writeSheet1);
excelWriter.finish();
}
/**
*
*
* @param object
* @param templateName
* @param fileName
* @param sheetName sheet [sheet]
* @throws Exception
*/
public void exportTemplateExcel(HttpServletResponse response, Object object, String templateName, String fileName, String sheetName) throws Exception {
String template = ExcelTemplateEnum.TEMPLATE_PATH.getDesc() + File.separator + templateName + ExcelTemplateEnum.TEMPLATE_SUFFIX.getDesc();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(template);
if (StringUtils.isEmpty(sheetName)) {
EasyExcelFactory.write(getOutputStream(fileName, response)).withTemplate(inputStream).sheet().doFill(object);
} else {
EasyExcelFactory.write(getOutputStream(fileName, response)).withTemplate(inputStream).sheet(sheetName).doFill(object);
}
}
/**
*
*
* @param list
* @param fileName
* @param templateName
* @param sheetNamesheet [sheet]
* @throws Exception
*/
public <T> void exportTemplateExcelList(HttpServletResponse response, List<T> list, String fileName, String templateName, String sheetName) throws Exception {
log.info("模板名称:{}", templateName);
String template = ExcelTemplateEnum.TEMPLATE_PATH.getDesc() + File.separator + templateName + ExcelTemplateEnum.TEMPLATE_SUFFIX.getDesc();
log.info("模板路径:{}", template);
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(template);
// 全部填充:全部加载到内存中一次填充
if (StringUtils.isEmpty(sheetName)) {
EasyExcelFactory.write(getOutputStream(fileName, response)).withTemplate(inputStream).sheet().doFill(list);
} else {
EasyExcelFactory.write(getOutputStream(fileName, response)).withTemplate(inputStream).sheet(sheetName).doFill(list);
}
}
/**
*
*
* @param list
* @param fileName
* @param templateName
* @throws Exception
*/
public <T> void exportTemplateExcel2(HttpServletResponse response, List<T> list, String fileName, String templateName) throws Exception {
String template = ExcelTemplateEnum.TEMPLATE_PATH.getDesc() + File.separator + templateName + ExcelTemplateEnum.TEMPLATE_SUFFIX.getDesc();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(template);
ExcelWriter excelWriter = EasyExcelFactory.write(getOutputStream(fileName, response)).withTemplate(inputStream).build();
WriteSheet writeSheet = EasyExcelFactory.writerSheet().build();
excelWriter.fill(list, writeSheet);
excelWriter.finish();
}
/**
*
*
* @param fileName
* @param response
* @return
* @throws Exception
*/
private OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {
fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name());
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ExcelTemplateEnum.TEMPLATE_SUFFIX.getDesc());
return response.getOutputStream();
}
/**
*
*
* @param file
*/
private void checkFile(MultipartFile file) {
if (file == null) {
throw new RenException("文件不能为空");
}
String fileName = file.getOriginalFilename();
if (StringUtils.isEmpty(fileName)) {
throw new RenException("文件不能为空");
}
if (!fileName.endsWith(ExcelTemplateEnum.TEMPLATE_SUFFIX.getDesc())
&& !fileName.endsWith(ExcelTemplateEnum.TEMPLATE_SUFFIX_XLS.getDesc())) {
throw new RenException("请上传.xlsx或.xls文件");
}
}
}