import com.twf.springcloud.ExportExcel.po.ServiceVo;
import com.twf.springcloud.ExportExcel.po.StationRespDto;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class Export {
public void exportExcel(HttpServletResponse response) {
XSSFWorkbook book = new XSSFWorkbook();
try {
//造两条数据
List<StationRespDto> list = new ArrayList<StationRespDto>();
StationRespDto stationRespDto = new StationRespDto();
stationRespDto.setOwner("运营商XXXX");
stationRespDto.setStationName("站名称");
stationRespDto.setStationID("站ID");
stationRespDto.setAddress("地址");
stationRespDto.setIsShow(true);
stationRespDto.setRecommendation(false);
List<ServiceVo> aaaa = new ArrayList<ServiceVo>();
ServiceVo serviceVo = new ServiceVo();
serviceVo.setName("服务A");
aaaa.add(serviceVo);
ServiceVo serviceVo2 = new ServiceVo();
serviceVo2.setName("服务B");
aaaa.add(serviceVo2);
ServiceVo serviceVo3 = new ServiceVo();
serviceVo3.setName("服务C");
aaaa.add(serviceVo3);
ServiceVo serviceVo4 = new ServiceVo();
serviceVo4.setName("服务D");
aaaa.add(serviceVo4);
stationRespDto.setServices(aaaa);
List<String> tags = new ArrayList<String>();
tags.add("标签A");
tags.add("标签B");
tags.add("标签C");
tags.add("标签D");
stationRespDto.setTags(tags);
stationRespDto.setCreationTime(new Date());
list.add(stationRespDto);
StationRespDto stationRespDto2 = new StationRespDto();
stationRespDto2.setOwner("运营商XXXX22222222222");
stationRespDto2.setStationName("站名称2222222222222");
stationRespDto2.setStationID("站ID2222222222222222");
stationRespDto2.setAddress("地址222222222222222");
stationRespDto2.setIsShow(false);
stationRespDto2.setRecommendation(true);
List<ServiceVo> ssss = new ArrayList<ServiceVo>();
ServiceVo serviceVo5 = new ServiceVo();
serviceVo5.setName("服务A222222222222");
ssss.add(serviceVo5);
ServiceVo serviceVo6 = new ServiceVo();
serviceVo6.setName("服务B222222222222");
ssss.add(serviceVo6);
ServiceVo serviceVo7 = new ServiceVo();
serviceVo7.setName("服务C222222222222");
ssss.add(serviceVo7);
ServiceVo serviceVo8 = new ServiceVo();
serviceVo8.setName("服务D222222222222");
ssss.add(serviceVo8);
stationRespDto2.setServices(ssss);
List<String> tags2 = new ArrayList<String>();
tags2.add("标签A2222222222222");
tags2.add("标签B22222222222222");
tags2.add("标签C2222222222222222");
tags2.add("标签D222222222222");
stationRespDto2.setTags(tags2);
stationRespDto2.setCreationTime(new Date());
list.add(stationRespDto2);
if (!CollectionUtils.isEmpty(list)) {
XSSFSheet sheet = book.createSheet("mySheent");
String[] title = {"运营商", "名称", "编号", "地址", "是否C端可见" ,"是否推荐","服务项目","标签","创建时间"};
createExcel(sheet, 0, title);
SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
for (int i = 0; i < list.size(); i++) {
StationRespDto station = list.get(i);
String service = "";
if (!CollectionUtils.isEmpty(station.getServices())){
List<ServiceVo> services = station.getServices();
List<String> serviceName = new ArrayList<>();
for (ServiceVo s : services){
serviceName.add(s.getName());
}
service = String.join(",", serviceName);
}
String tag = "";
if (!CollectionUtils.isEmpty(station.getTags())){
tag = String.join(",", station.getTags());
}
String[] data = new String[]{station.getOwner(), station.getStationName(), station.getStationID(), station.getAddress(),
station.getIsShow() ? "是" : "否",station.getRecommendation() ? "是" : "否",service,tag,format.format(station.getCreationTime())};
createExcel(sheet, i + 1, data);
}
book.write(generateResponseExcel("充电站列表",response));
}
book.close();
}catch(Exception e){
e.printStackTrace();
}
}
private void createExcel(XSSFSheet sheet, int rowNo, String[] vals) {
if (vals.length > 0) {
XSSFRow row = sheet.createRow(rowNo);
for (int i = 0; i < vals.length; i++) {
String val = vals[i];
XSSFCell cell = row.createCell(i);
cell.setCellValue(val);
}
}
}
private ServletOutputStream generateResponseExcel(String excelName, HttpServletResponse response) throws IOException {
excelName = excelName == null || "".equals(excelName) ? "excel" : URLEncoder.encode(excelName, "UTF-8");
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename=" + excelName + ".xlsx");
return response.getOutputStream();
}
}