一
com.opencsv
opencsv
4.1
package com.in.out.mine.controller;
import com.in.out.mine.ServiceImpl.FileServiceImpl;
import com.in.out.mine.entity.Student;
import com.in.out.mine.utils.ResObject;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Controller
public class FileController {
@Autowired
private FileServiceImpl fileService;
@RequestMapping(value = "/export", method = RequestMethod.GET)
public String toExport() {
return "export";
}
@ResponseBody
@RequestMapping(value = "/import", method = RequestMethod.POST)
public ResObject importFunction(HttpServletResponse response, MultipartFile file) throws IOException {
InputStreamReader isr = new InputStreamReader(file.getInputStream(),"gbk");
CSVReader csvReader = new CSVReader(isr);
List ret = csvReader.readAll();
System.out.println(ret.get(0)[1]);
System.out.println(ret.get(0)[2]);
System.out.println(ret.get(0)[3]);
String[] a = csvReader.readNext();
long bb = csvReader.getLinesRead();
long cc = csvReader.getRecordsRead();
List studentList = fileService.export();
return new ResObject();
}
@ResponseBody
@RequestMapping(value = "/export/download", method = RequestMethod.GET)
public void export(HttpServletResponse response) throws IOException {
List studentList = fileService.export();
response.setHeader("Content-Type","text/csv; charset=gbk");
response.setHeader("Content-Disposition","attachment; filename=export_"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".csv");
try(
OutputStream outputStream = response.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream);
) {
printWriter.println("id,姓名,地址,描述");
studentList.forEach(student -> {
printWriter.println(String.format("%s,%s,%s,%s",
String.valueOf(student.id),
student.name,
student.address,
student.desc)
);
});
}
}
@ResponseBody
@RequestMapping(value = "/export/download2", method = RequestMethod.GET)
public void export2(HttpServletResponse response) throws IOException {
List studentList = fileService.export();
response.setHeader("Content-Type","text/csv; charset=gbk");
response.setHeader("Content-Disposition","attachment; filename=export_"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".csv");
CSVWriter csvWriter = new CSVWriter(response.getWriter());
// try(
// OutputStream outputStream = response.getOutputStream();
// PrintWriter printWriter = new PrintWriter(outputStream);
{
String [] title = {"id","姓名","地址","描述"};
csvWriter.writeNext(title);
studentList.forEach(student -> {
String[] content = new String[]{
String.valueOf(student.id),
student.name,
student.address,
student.desc
};
csvWriter.writeNext(content);
});
}
}
}
二
Title