feat: 增加导出excel
parent
5375dc5629
commit
6c36d6f4da
@ -0,0 +1,10 @@
|
|||||||
|
package com.xydl.cac.service;
|
||||||
|
|
||||||
|
import com.xydl.cac.model.SensorDetail;
|
||||||
|
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
|
public interface ReportService {
|
||||||
|
|
||||||
|
void exportSensor(SensorDetail detail, OutputStream output) throws Exception;
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
package com.xydl.cac.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.excel.EasyExcel;
|
||||||
|
import com.alibaba.excel.ExcelWriter;
|
||||||
|
import com.alibaba.excel.write.metadata.WriteSheet;
|
||||||
|
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
|
||||||
|
import com.xydl.cac.entity.NiecPoint;
|
||||||
|
import com.xydl.cac.model.SensorDetail;
|
||||||
|
import com.xydl.cac.service.ReportService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public class ReportServiceImpl implements ReportService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void exportSensor(SensorDetail detail, OutputStream output) throws Exception {
|
||||||
|
List<List<String>> heads = new ArrayList<>();
|
||||||
|
List<String> head = new ArrayList<>();
|
||||||
|
head.add("时间");
|
||||||
|
heads.add(head);
|
||||||
|
for (NiecPoint point : detail.getPoints()) {
|
||||||
|
head = new ArrayList<>();
|
||||||
|
head.add(point.getName());
|
||||||
|
heads.add(head);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<List<String>> list = new ArrayList<>();
|
||||||
|
for (Map<String, Object> map : detail.getContent()) {
|
||||||
|
List<String> line = new ArrayList<>();
|
||||||
|
Object data = map.get("acquisitionTime");
|
||||||
|
line.add(String.valueOf(data));
|
||||||
|
for (NiecPoint point : detail.getPoints()) {
|
||||||
|
data = map.get(point.getField());
|
||||||
|
if (data == null) {
|
||||||
|
data = "";
|
||||||
|
}
|
||||||
|
line.add(String.valueOf(data));
|
||||||
|
}
|
||||||
|
list.add(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 导出Excel
|
||||||
|
ExcelWriter excelWriter = EasyExcel.write(output).inMemory(true)
|
||||||
|
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
|
||||||
|
|
||||||
|
WriteSheet writeSheet0 = EasyExcel.writerSheet(0, detail.getSensor().getName())
|
||||||
|
.head(heads)
|
||||||
|
.build();
|
||||||
|
excelWriter.write(list, writeSheet0);
|
||||||
|
|
||||||
|
excelWriter.finish();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue