package com.my.iot.controller;
import com.my.iot.domain.Data;
import com.my.iot.domain.Gateway;
import com.my.iot.domain.Sensor;
import com.my.iot.service.GatewayService;
import com.my.iot.service.SensorService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
@RestController
@RequestMapping("/api/file")
public class FileController {
@Autowired
private GatewayService gatewayService;
@Autowired
private SensorService sensorService;
/*-------------获取所有网关的xls表格--------------*/
@GetMapping("/gateway")
public String getGatewaysInfo(HttpServletResponse response) throws IOException {
String filename = "gateway.xls";
Workbook workbook = null;
String msg;
List<Gateway> gateways = gatewayService.findAll(false);
try {
response.setContentType("application/vnd.ms-excel");//xls文件的mimeType
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
OutputStream out = response.getOutputStream();
workbook = this.createGatewaysWorkbook(gateways);
workbook.write(out);
out.flush();
out.close();
msg = "success";
} catch (Exception e) {
e.printStackTrace();
msg = "error";
} finally {
if (workbook != null) {
workbook.close();
}
}
return msg;
}
//创建所有网关信息xls文档
private Workbook createGatewaysWorkbook(List<Gateway> gateways) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
//设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度
sheet.setColumnWidth(0, 6 * 256);
sheet.setColumnWidth(1, 20 * 256);
sheet.setColumnWidth(2, 10 * 256);
sheet.setColumnWidth(3, 30 * 256);
sheet.setColumnWidth(4, 20 * 256);
//设置居中加粗
HSSFFont font = workbook.createFont();
font.setBold(true);
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setFont(font);
String[] headers = new String[]{"ID", "ip", "端口", "描述", "位置"};
HSSFRow headRow = sheet.createRow(0);//表头行
for (int j = 0; j < headers.length; j++) {//创建表头
HSSFCell cell = headRow.createCell(j);
cell.setCellValue(headers[j]);
cell.setCellStyle(style);
}
//添加信息
for (int i = 1; i <= gateways.size(); i++) {
HSSFRow row = sheet.createRow(i);
Gateway gateway = gateways.get(i - 1);
row.createCell(0).setCellValue(gateway.getId());
row.createCell(1).setCellValue(gateway.getIp());
row.createCell(2).setCellValue(gateway.getPort());
row.createCell(3).setCellValue(gateway.getDescription());
row.createCell(4).setCellValue(gateway.getLocation());
}
return workbook;
}
/*-------------获取所有传感器的xls表格--------------*/
@GetMapping("/sensor")
public String getSensorsInfo(HttpServletResponse response) throws IOException {
String filename = "sensor.xls";
Workbook workbook = null;
String msg;
List<Sensor> sensors = sensorService.findAll();
try {
response.setContentType("application/vnd.ms-excel");//xls文件的mimeType
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
OutputStream out = response.getOutputStream();
workbook = this.createSensorsWorkbook(sensors);
workbook.write(out);
out.flush();
out.close();
msg = "success";
} catch (Exception e) {
e.printStackTrace();
msg = "error";
} finally {
if (workbook != null) {
workbook.close();
}
}
return msg;
}
//创建所有传感器信息xls文档
private Workbook createSensorsWorkbook(List<Sensor> sensors) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
//设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度
sheet.setColumnWidth(0, 20 * 256);
sheet.setColumnWidth(1, 40 * 256);
sheet.setColumnWidth(2, 30 * 256);
sheet.setColumnWidth(3, 30 * 256);
sheet.setColumnWidth(4, 20 * 256);
sheet.setColumnWidth(5, 20 * 256);
sheet.setColumnWidth(6, 20 * 256);
sheet.setColumnWidth(7, 20 * 256);
sheet.setColumnWidth(8, 20 * 256);
//设置居中加粗
HSSFFont font = workbook.createFont();
font.setBold(true);
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setFont(font);
//日期格式
HSSFCellStyle dateStyle = workbook.createCellStyle();
dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
String[] headers = new String[]{"ID", "描述", "位置", "厂家", "安装日期", "生产日期", "质保日期", "所属网关ID", "所属分类ID"};
HSSFRow headRow = sheet.createRow(0);//表头行
for (int j = 0; j < headers.length; j++) {//创建表头
HSSFCell cell = headRow.createCell(j);
cell.setCellValue(headers[j]);
cell.setCellStyle(style);
}
//添加信息
for (int i = 1; i <= sensors.size(); i++) {
HSSFRow row = sheet.createRow(i);
Sensor sensor = sensors.get(i - 1);
row.createCell(0).setCellValue(sensor.getId());
row.createCell(1).setCellValue(sensor.getDescription());
row.createCell(2).setCellValue(sensor.getLocation());
row.createCell(3).setCellValue(sensor.getFactory());
HSSFCell cell = row.createCell(4);
cell.setCellValue(sensor.getInstall_time());
cell.setCellStyle(dateStyle);
cell = row.createCell(5);
cell.setCellValue(sensor.getProduce_date());
cell.setCellStyle(dateStyle);
cell = row.createCell(6);
cell.setCellValue(sensor.getMaintenance_time());
cell.setCellStyle(dateStyle);
row.createCell(7).setCellValue(sensor.getGate_id());
row.createCell(8).setCellValue(sensor.getClassify_id());
}
return workbook;
}
/*-------------获取一个网关及其传感器的xls表格--------------*/
@GetMapping("/gateway/{id}")
public String getGatewayInfoByIdWithSensors(@PathVariable("id") int id, HttpServletResponse response) throws IOException {
String filename = "gateway_id=" + id + ".xls";
Workbook workbook = null;
String msg;
Gateway gateway = gatewayService.findByIdWithSensors(id);
try {
response.setContentType("application/vnd.ms-excel");//xls文件的mimeType
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
OutputStream out = response.getOutputStream();
workbook = this.createOneGatewayWorkbookWithSensors(gateway);
workbook.write(out);
out.flush();
out.close();
msg = "success";
} catch (Exception e) {
e.printStackTrace();
msg = "error";
} finally {
if
龙年行大运
- 粉丝: 1385
- 资源: 3960
最新资源
- 【国泰君安期货-2024研报】镍:宏观与基本面共振承压,镍价短期低位震荡,不锈钢:高库存边际微降,成本有所下移.pdf
- Matlab实现CNN-LSTM-Mutilhead-Attention卷积长短期记忆神经网络融合多头注意力机制多变量时间序列预测(含完整的程序,GUI设计和代码详解)
- Android Studio Ladybug(android-studio-2024.2.1.12-linux.zip.002)
- 医疗知识图谱的构建及应用(平安医疗)
- SNIA多应用解决方案的高速六通道无屏蔽连接器标准规范
- Matlab实现CPO-LSTM 冠豪猪优化长短期记忆神经网络多变量回归预测(含完整的程序,GUI设计和代码详解)
- Matlab基于SSA-SVR麻雀算法优化支持向量机的数据多输入单输出回归预测(含完整的程序,GUI设计和代码详解)
- Android Studio Ladybug(android-studio-2024.2.1.12-linux.zip.001)
- 计算机专业学习资源大全
- MATLAB实现CEEMDAN+SE自适应经验模态分解+样本熵计算(含完整的程序,GUI设计和代码详解)
- Matlab实现PSO-CNN粒子群优化卷积神经网络多输入多输出预测(含完整的程序,GUI设计和代码详解)
- docker一键部署脚本 lilishop是docker及k8s脚本
- Matlab实现CPO-VMD基于冠豪猪优化算法(CPO)优化VMD变分模态分解时间序列信号分解(含完整的程序,GUI设计和代码详解)
- 最大公约数和最小公倍数.cpp
- MATLAB实现SSA-CNN-GRU-Attention多变量回归预测(SE注意力机制)(含完整的程序,GUI设计和代码详解)
- Linux操作系统使用教程全集-教学
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈