{
List<T> list = new ArrayList<>();
list.add(导入的对象放到list 里 就好了)
ExportParams exportParams = new ExportParams();
exportParams.setSheetName("遥测参数");
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, T.class, list);
String fileName = "文件名称.xls";
ExcelUtil.downLoadExcel(fileName, response, workbook);
return CheckResult.success(”OK“);
}
导入
{
ImportParams importParams = new ImportParams();
importParams.setHeadRows(1);
importParams.setNeedVerify(true);
ExcelImportResult<T> data = new ExcelImportResult<>();
try {
data = ExcelImportUtil.importExcelMore(file.getInputStream(), T.class, importParams);
} catch (Exception e) {
log.error(e.getMessage(), e);
return CheckResult.fail("文件解析失败,可能原因文件格式不正确");
}
List<T> successList = data.getList();
if (successList.size() == 0) {
log.info(MsgPrefixKit.genMgt("错误信息,不继续执行"));
return CheckResult.fail("错误信息,不继续执行,请检查表格信息是否完整");
}
List<T> failList = data.getFailList();
if (failList.size() > 0) {
log.info(MsgPrefixKit.genMgt("有" + data.getFailList().size() + "条数据不合格,无法导入"));
}
List<T> listmsg = new ArrayList<>(successList);
listmsg 就是导入的对象集合, 然后遍历进行C R U D
}
spring:
servlet:
multipart:
max-request-size: 10MB
max-file-size: 10MB
public class ExcelUtil {
public static boolean isExcel2003(String filePath)
{
return filePath.matches("^.+\\.(?i)(xls)$");
}
public static boolean isExcel2007(String filePath)
{
return filePath.matches("^.+\\.(?i)(xlsx)$");
}
public static Workbook getWorkBook(MultipartFile file) throws IOException {
InputStream is = file.getInputStream();
Workbook hssfWorkbook = null;
try {
hssfWorkbook = new HSSFWorkbook(is);
} catch (Exception ex) {
is =file.getInputStream();
hssfWorkbook = new XSSFWorkbook(is);
}
return hssfWorkbook;
}
public static void getWrongInfo(StringBuilder sb, List list, int i, Object obj, String name, String msg) throws Exception{
Class clazz=obj.getClass();
Object str=null;
Field[] fields = clazz.getDeclaredFields();
for(Field f : fields){
if(f.getName().equals(name)){
PropertyDescriptor pd = new PropertyDescriptor(f.getName(), clazz);
Method getMethod=pd.getReadMethod();
str = getMethod.invoke(obj);
}
}
if(i==0) {
sb.append(msg + str + ";");
}
else if(i==(list.size()-1)) {
sb.append(str + "</br>");
}
else {
sb.append(str + ";");
}
}
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);
}
}
public static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
try {
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.setCharacterEncoding("UTF-8");
workbook.write(response.getOutputStream());
} catch (IOException e) {
log.error(e.getMessage());
}finally {
try {
workbook.close();
} catch (IOException e) {
log.error(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){
log.error(e.getMessage());
} catch (Exception e) {
log.error(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){
log.error(e.getMessage());
} catch (Exception e) {
log.error(e.getMessage());
}
return list;
}
public static Workbook exportExcel(List<Map<String, Object>> list) {
Workbook workbook = new HSSFWorkbook();
for (Map<String, Object> map : list) {
createSheetWithList(workbook, (ExportParams) map.get("title"), ExportParams.class,
(List<ExcelExportEntity>) map.get("entityList"),
(Collection<?>) map.get("data"));
}
return workbook;
}
public static void createSheetWithList(Workbook workbook, ExportParams entity, Class<?> pojoClass, List<ExcelExportEntity> entityList, Collection<?> dataSet) {
if (workbook == null || entity == null || pojoClass == null || dataSet == null) {
throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR);
}
try {
List<ExcelExportEntity> excelParams = entityList;
Field[] fileds = PoiPublicUtil.getClassFields(pojoClass);
ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
String targetId = etarget == null ? null : etarget.value();
new ExcelExportService().getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass,null, null);
new ExcelExportService().createSheetForMap(workbook, entity, excelParams, dataSet);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-6007.html