Maven依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
一、导入控制层代码
@RequestMapping(value = "/fileUpload",method = RequestMethod.POST,
consumes = "application/json",produces = "application/json")
public Map<String, Object> fileUpload(HttpServletRequest request,@RequestParam("file") MultipartFile file) {
Map<String, Object> map = new HashMap<String, Object>();
// 判断文件是否为空
if (!StringUtils.isEmpty(file)) {
try {
List<RefPartExcel> excelBeans = ExcelUtil .readExcel(request,RefPartExcel.class);
System.out.println(excelBeans.size());
for(RefPartExcel ep : excelBeans){
System.out.println(ep.toString());
}
//........逻辑
} catch (Exception e) {
e.printStackTrace();
}
}
return map;
}
二、导出
package com.phil.download;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.phil.excel.model.ExportExcel;
import com.phil.service.QuestionService;
@Controller
@RequestMapping("api/download")
public class DownloadController {
private static String EXPORT_XLSX_FILE_SUFFIX = ".xlsx";
// private static String EXPORT_XLS_FILE_SUFFIX = ".xls";
@Autowired
private QuestionService questionService;
@GetMapping("export")
public void export(Map<String, Object> map ,HttpServletResponse response) {
List<Map<String, Object>> list = questionService.findByPage(new HashMap<>());
for(int i = 0; i < 100000; i++) { //数据库为空,遍历了100000个
Map<String, Object> temp_ = new HashMap<>();
temp_.put("id", i + 1);
temp_.put("number", i + 1);
temp_.put("description", (i + 1) + "描述");
list.add(temp_);
}
ExportExcel<List<Map<String, Object>>> exportExcel = new ExportExcel<>();
StringBuffer filename = new StringBuffer();
filename.append("导出");
filename.append(System.currentTimeMillis());
if(StringUtils.isEmpty(map.get("excel_type"))) {
filename.append(EXPORT_XLSX_FILE_SUFFIX);
} else {
filename.append(map.get("excel_type"));
}
OutputStream out = null;
try {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + new String(filename.toString().getBytes("UTF-8"), "ISO8859-1"));
out = response.getOutputStream();
exportExcel.exportXSExcelByColumn("Title", new String[] {"id", "number", "description"}, new String[] {"id", "number", "description"},
list, out ,null);
} catch (IOException e) {
}
}
}
具体代码可参考:https://blog.csdn.net/phil_jing/article/details/78307819