POI 3.17导出Excel、导入

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值