编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

excel/csv/txt导入导出util类(导入csv格式数据到列表)

wxchong 2024-08-09 11:36:53 开源技术 17 ℃ 0 评论

1.首先,pom文件需要导入依赖:

<dependency>
 <groupId>cn.afterturn</groupId>
 <artifactId>easypoi-base</artifactId>
 <version>3.1.0</version>
</dependency>
<dependency>
 <groupId>cn.afterturn</groupId>
 <artifactId>easypoi-web</artifactId>
 <version>3.1.0</version>
</dependency>
<dependency>
 <groupId>cn.afterturn</groupId>
 <artifactId>easypoi-annotation</artifactId>
 <version>3.1.0</version>
</dependency>
<dependency>
 <groupId>commons-io</groupId>
 <artifactId>commons-io</artifactId>
 <version>2.5</version>
</dependency>

2.其次,新建一个注解类,在实体类字段上加上这个注解就可以在导出时将该字段忽略:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ExportIgnonre {
 boolean exportIgnore() default true;
}

3.最后,工具类代码:

package com.util;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.util.annotation.ExportIgnonre;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
public class UploadAndExportDataUtil {
public static final String TXT_SPLIT = "\t";
public static final String CSV_SPLIT = ",";
public static final String LINE_FEED = "\n";
public static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static<T> void outputToClient(HttpServletResponse response,String fileName,String fileType,List<T> list,Class<T> cla) throws IOException, ParseException, IllegalAccessException {
response.setContentType("application/force-download");
if("xlsx".equals(fileType)){
fileType = "xls";
}
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName+"."+fileType, "UTF-8"));
if("xls".equals(fileType)||"xlsx".equals(fileType)){
Workbook workbook = null;
workbook = getWorkbook(workbook,cla,list,1,1);
workbook.write(response.getOutputStream());
}else {
response.getWriter().write(exportForBean(fileType,list,cla));
}
}
public static<T> void outputToClient(HttpServletResponse response,String fileName,String fileType,List<T> list,Class<T> cla,int currentPage,int totalPage) throws IOException, IllegalAccessException, ParseException {
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName+"."+fileType, "UTF-8"));
response.getWriter().write(exportForBean(fileType,list,cla,currentPage,totalPage));
}
public static<T> Workbook outputToClient(HttpServletResponse response, Workbook workbook, String fileName,List<T> list,Class<T> cla,int currentPage,int totalPage) throws IOException {
workbook = getWorkbook(workbook,cla,list,currentPage,totalPage);
if(currentPage ==1 && currentPage==totalPage){
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName+".xls", "UTF-8"));
workbook.write(response.getOutputStream());
return workbook;
}
if(currentPage==totalPage){
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName+".xlsx", "UTF-8"));
ExcelExportUtil.closeExportBigExcel();
workbook.write(response.getOutputStream());
}
return workbook;
}
public static void outputToClient(HttpServletResponse response, String fileName, String fileType, String fileContext, int currentPage) throws IOException {
if(currentPage == 1){
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName+"."+fileType, "UTF-8"));
}
response.getWriter().write(fileContext);
}
public static void outputToDisk(FileOutputStream outputStream,String fileContext){
PrintWriter pw = new PrintWriter(outputStream);
pw.write(fileContext);
pw.flush();
pw.close();
}
public static<T> Workbook getWorkbook(Workbook workbook, Class<T> cla, List<T> list,int currentPage,int totalPage){
ExportParams exportParams = new ExportParams();
if(currentPage==1 && currentPage == totalPage){
workbook = ExcelExportUtil.exportExcel(exportParams,cla,list);
}else {
workbook = ExcelExportUtil.exportBigExcel(exportParams,cla,list);
}
return workbook;
}
//--少量数据的导出
public static<T> String exportForBean(String fileType, List<T> list, Class<T> cla) throws IllegalAccessException, ParseException {
return exportForBean(fileType,list,cla,1);
}
public static<T> String exportForBean(String fileType, List<T> list, Class<T> cla,int currentPage) throws IllegalAccessException, ParseException {
StringBuffer buffer = new StringBuffer();
if(list!=null&&list.size()>0){
if("txt".equals(fileType) || "csv".equals(fileType)){
if(Map.class.equals(cla)){
List<Map> list1 = (List<Map>) list;
if(currentPage==1){
Set set = list1.get(0).keySet();
int ss = set.size();
int count = 0;
for (Object o : set) {
count++;
buffer.append(o.toString());
if(count!=ss){
if("csv".equals(fileType)){
buffer.append(CSV_SPLIT);
}else if("txt".equals(fileType)){
buffer.append(TXT_SPLIT);
}
}
}
buffer.append("\r\n");
}
for (Map map : list1) {
Set set = map.keySet();
int ss = set.size();
int count = 0;
for (Object value : map.values()) {
if(value==null||"".equals(value)){
value="null";
}
if("txt".equals(fileType)){
buffer.append("\"");
buffer.append(value.toString());
buffer.append("\"");
buffer.append(TXT_SPLIT);
}else if("csv".equals(fileType)){
count++;
buffer.append(value.toString());
if(count!=ss){
buffer.append(CSV_SPLIT);
}
}
}
buffer.append(LINE_FEED);
}
}else {
Class clz = cla;
Field[] fields = clz.getDeclaredFields();
int ss = fields.length;
if(currentPage==1){
int count = 0;
for (Field field : fields) {
field.setAccessible(true);
boolean isAnno = field.isAnnotationPresent(ExportIgnonre.class);
if(isAnno){
count++;
continue;
}
count++;
buffer.append(field.getName());
if(count!=ss){
if("csv".equals(fileType)){
buffer.append(CSV_SPLIT);
}else if("txt".equals(fileType)){
buffer.append(TXT_SPLIT);
}
}
}
buffer.append("\r\n");
}
for (T t : list) {
for (Field field : fields) {
int count = 0;
field.setAccessible(true);
boolean isAnno = field.isAnnotationPresent(ExportIgnonre.class);
if(isAnno){
count++;
continue;
}
Object o = field.get(t);
if(o==null||"".equals(o)){
o="null";
}
String value = o.toString();
boolean isDate = field.isAnnotationPresent(Excel.class);
if(isDate){
String format = field.getAnnotation(Excel.class).format();
if(!"".equals(format)){
SimpleDateFormat sm = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", Locale.US);
Date date = sm.parse(value);
sm = new SimpleDateFormat(format);
value = sm.format(date);
}
}
if("txt".equals(fileType)){
buffer.append("\"");
buffer.append(value);
buffer.append("\"");
buffer.append(TXT_SPLIT);
}else if("csv".equals(fileType)){
count++;
buffer.append(value);
if(count!=ss){
buffer.append(CSV_SPLIT);
}
}
}
buffer.append(LINE_FEED);
}
}
}else if("cfg".equals(fileType) || "json".equals(fileType)){
String str = JSON.toJSONString(list, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
return str;
}
}
return buffer.toString();
}
//--全量数据的导出
public static<T> String exportForBean(String fileType, List<T> list,Class<T> cla,int currentPage,int totalPage) throws IllegalAccessException, ParseException {
StringBuffer buffer = new StringBuffer();
if(currentPage==1){
buffer.append("[");
}
int countList = list.size();
int count = 0;
if("txt".equals(fileType) || "csv".equals(fileType)){
return exportForBean(fileType,list,cla,currentPage);
}else if("json".equals(fileType) || "cfg".equals(fileType)){
for (T t : list) {
count++;
buffer.append(LINE_FEED);
buffer.append(JSON.toJSONString(t,true));
if (count==countList && currentPage==totalPage){
buffer.append(LINE_FEED);
buffer.append("]");
break;
}
buffer.append(CSV_SPLIT);
}
}
return buffer.toString();
}
public static<T> List<T> upload(MultipartFile multipartFile, Class<T> cla) throws Exception {
List<T> list = new ArrayList<>();
String originalFilename = multipartFile.getOriginalFilename();
String extension = FilenameUtils.getExtension(originalFilename);
InputStream inputStream = multipartFile.getInputStream();
InputStreamReader reader =new InputStreamReader(inputStream);
BufferedReader br = new BufferedReader(reader);
if("txt".equals(extension)){
while(true){
String line = br.readLine();
if (line==null){
break;
}
String[] split = line.replace("\"", "").trim().split(TXT_SPLIT);
list.add((T) getBeanAttributeName(cla,split));
}
}
if("csv".equals(extension)){
br.readLine();
while(true){
String line = br.readLine();
if (line==null){
break;
}
String[] split = line.trim().split(CSV_SPLIT);
list.add((T) getBeanAttributeName(cla,split));
}
}
if("xls".equals(extension)||"xlsx".equals(extension)){
ImportParams importParams = new ImportParams();
importParams.setHeadRows(1);
List objects = ExcelImportUtil.importExcel(inputStream, cla, importParams);
return objects;
}
if("json".equals(extension)||"cfg".equals(extension)){
StringBuffer bf = new StringBuffer();
while(true){
String line = br.readLine();
if (line==null){
break;
}
bf.append(line);
}
br.close();
reader.close();
List<T> list1 = JSON.parseArray(bf.toString(), cla);
return list1;
}
br.close();
reader.close();
return list;
}
public static<T> Object getBeanAttributeName(Class<T> cla,String[] strings) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, ParseException {
if(isEight(cla)){
String s = cla.getTypeName();
if("".equals(strings[0].trim())){
return null;
}
if(s.contains("String")){
return (T)strings[0].trim();
}else if(s.contains("int")||s.contains("Integer")){
return Integer.valueOf(strings[0].trim());
}else if(s.contains("double")||s.contains("Double")){
return Double.valueOf(strings[0].trim());
}else if(s.contains("boolean")||s.contains("Boolean")){
return Boolean.valueOf(strings[0].trim());
}else if(s.contains("long")||s.contains("Long")){
return Long.valueOf(strings[0].trim());
}else if(s.contains("Date")||s.contains("date")||s.contains("time")){
Date date = df.parse(strings[0].trim());
return date;
}
}
Class cl = cla;
Object o = cl.newInstance();
Field[] fields = cl.getDeclaredFields();
int count = 0;
for (Field field : fields) {
field.setAccessible(true);
boolean anno = field.isAnnotationPresent(ExportIgnonre.class);
if(anno){
continue;
}
String name = field.getName();
name = name.substring(0,1).toUpperCase()+name.substring(1);
Method method = cl.getDeclaredMethod("set" + name, field.getType());
String s = field.getType().toString();
if("".equals(strings[count])||"null".equals(strings[count])){
count++;
continue;
}
if(s.contains("String")){
method.invoke(o,strings[count].trim());
}else if(s.contains("int")||s.contains("Integer")){
method.invoke(o,Integer.valueOf(strings[count].trim()));
}else if(s.contains("double")||s.contains("Double")){
method.invoke(o,Double.valueOf(strings[count].trim()));
}else if(s.contains("boolean")||s.contains("Boolean")){
method.invoke(o,Boolean.valueOf(strings[count].trim()));
}else if(s.contains("long")||s.contains("Long")){
method.invoke(o,Long.valueOf(strings[count].trim()));
}else if(s.contains("Date")||s.contains("date")||s.contains("time")){
Date date = df.parse(strings[count].trim());
method.invoke(o,date);
}
count++;
}
T t = (T) o;
return t;
}
public static boolean isEight(Class cla){
if (String.class.equals(cla)){
return true;
}else if (Integer.class.equals(cla)){
return true;
}else if(Long.class.equals(cla)){
return true;
}else if(Double.class.equals(cla)){
return true;
}else if(Float.class.equals(cla)){
return true;
}
return false;
}
}

4.具体实现:

@ApiOperation(value = "export")
@RequestMapping(value = "export", method = RequestMethod.GET)
@ResponseBody
public Object export(String idList, HttpServletResponse response) {
 if (CommonUtils.isNotEmpty(idList)) {
 try {
 List<Integer> ids = JSON.parseArray(idList, Integer.class);
 logger.info(ids.toString());
 List<LinkedHashMap> list = nodesManagerService.nodeInfoExport(ids);
 UploadAndExportDataUtil.outputToClient(response, "node", "cfg", list, LinkedHashMap.class);
 return null;
 } catch (Exception e) {
 e.printStackTrace();
 return new Result(false, 500, languageMsg.errorMsg);
 }
 } else {
 return new Result(false, 400, languageMsg.parameterMsg);
 }
}
@ApiOperation(value = "import NodeInfo")
@RequestMapping(value = "importNodeInfo", method = RequestMethod.POST)
@ResponseBody
public Result importNodeInfo(@RequestParam(value = "file") MultipartFile multipartFile) {
 try {
 List<ImportParam> importParams = UploadAndExportDataUtil.upload(multipartFile, ImportParam.class);
 logger.info(importParams.toString());
 return nodesManagerService.importData(importParams);
 } catch (Exception e) {
 e.printStackTrace();
 return new Result(false, 500, e.getMessage());
 }
}

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表