forked from github/easyexcel
zhuangjiaju
5 years ago
7 changed files with 182 additions and 28 deletions
@ -0,0 +1,22 @@
|
||||
package com.alibaba.easyexcel.test.demo.web; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* 基础数据类 |
||||
* |
||||
* @author zhuangjiaju |
||||
**/ |
||||
@Data |
||||
public class DownloadData { |
||||
@ExcelProperty("字符串标题") |
||||
private String string; |
||||
@ExcelProperty("日期标题") |
||||
private Date date; |
||||
@ExcelProperty("数字标题") |
||||
private Double doubleData; |
||||
} |
@ -0,0 +1,17 @@
|
||||
package com.alibaba.easyexcel.test.demo.web; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* 基础数据类 |
||||
* |
||||
* @author zhuangjiaju |
||||
**/ |
||||
@Data |
||||
public class UploadData { |
||||
private String string; |
||||
private Date date; |
||||
private Double doubleData; |
||||
} |
@ -0,0 +1,49 @@
|
||||
package com.alibaba.easyexcel.test.demo.web; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* 模板的读取类 |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class UploadDataListener extends AnalysisEventListener<UploadData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(UploadDataListener.class); |
||||
/** |
||||
* 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 |
||||
*/ |
||||
private static final int BATCH_COUNT = 5; |
||||
List<UploadData> list = new ArrayList<UploadData>(); |
||||
|
||||
@Override |
||||
public void invoke(UploadData data, AnalysisContext context) { |
||||
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data)); |
||||
list.add(data); |
||||
if (list.size() >= BATCH_COUNT) { |
||||
saveData(); |
||||
list.clear(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
saveData(); |
||||
LOGGER.info("所有数据解析完成!"); |
||||
} |
||||
|
||||
/** |
||||
* 加上存储数据库 |
||||
*/ |
||||
private void saveData() { |
||||
LOGGER.info("{}条数据,开始存储数据库!", list.size()); |
||||
LOGGER.info("存储数据库成功!"); |
||||
} |
||||
} |
@ -0,0 +1,64 @@
|
||||
package com.alibaba.easyexcel.test.demo.web; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.ArrayList; |
||||
import java.util.Date; |
||||
import java.util.List; |
||||
|
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
import org.springframework.stereotype.Controller; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.PostMapping; |
||||
import org.springframework.web.bind.annotation.ResponseBody; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
|
||||
import com.alibaba.excel.EasyExcelFactory; |
||||
|
||||
/** |
||||
* web读写案例 |
||||
* |
||||
* @author zhuangjiaju |
||||
**/ |
||||
@Controller |
||||
public class WebTest { |
||||
/** |
||||
* 文件下载 |
||||
* <li>1. 创建excel对应的实体对象 参照{@link DownloadData} |
||||
* <li>2. 设置返回的 参数 |
||||
* <li>3. 直接写,这里注意,finish的时候会自动关闭OutputStream,当然你外面再关闭异常问题不大 |
||||
*/ |
||||
@GetMapping("download") |
||||
public void download(HttpServletResponse response) throws IOException { |
||||
response.setContentType("application/vnd.ms-excel"); |
||||
response.setCharacterEncoding("utf-8"); |
||||
response.setHeader("Content-disposition", "attachment;filename=demo.xlsx"); |
||||
EasyExcelFactory.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data()).finish(); |
||||
} |
||||
|
||||
/** |
||||
* 文件上传 |
||||
* <li>1. 创建excel对应的实体对象 参照{@link UploadData} |
||||
* <li>2. 由于默认异步读取excel,所以需要创建excel一行一行的回调监听器,参照{@link UploadDataListener} |
||||
* <li>3. 直接读即可 |
||||
*/ |
||||
@PostMapping("upload") |
||||
@ResponseBody |
||||
public String upload(MultipartFile file) throws IOException { |
||||
EasyExcelFactory.read(file.getInputStream(), UploadData.class, new UploadDataListener()).sheet().doRead() |
||||
.finish(); |
||||
return "success"; |
||||
} |
||||
|
||||
private List<DownloadData> data() { |
||||
List<DownloadData> list = new ArrayList<DownloadData>(); |
||||
for (int i = 0; i < 10; i++) { |
||||
DownloadData data = new DownloadData(); |
||||
data.setString("字符串" + 0); |
||||
data.setDate(new Date()); |
||||
data.setDoubleData(0.56); |
||||
list.add(data); |
||||
} |
||||
return list; |
||||
} |
||||
} |
Loading…
Reference in new issue