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
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 1、该资源内项目代码都是经过测试运行成功,功能正常的情况下才上传的,请放心下载使用。 2、适用人群:主要针对计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、数学、电子信息等)的同学或企业员工下载使用,具有较高的学习借鉴价值。 3、不仅适合小白学习实战练习,也可作为大作业、课程设计、毕设项目、初期项目立项演示等,欢迎下载,互相学习,共同进步!
资源推荐
资源详情
资源评论
收起资源包目录
基于SpringBoot框架搭建的物联网数据采集系统服务器端(源码+项目说明).zip (94个子文件)
code_30312
pom.xml 3KB
src
test
java
com
my
iot
BaseTest.java 290B
log
LoggerTest.java 621B
main
resources
iot.sql 11KB
static-old
js
dateformat.js 1KB
echarts.min.js 753KB
jquery-3.4.1.min.js 86KB
html
regist.html 507B
gateway.html 5KB
changePassword.html 430B
exception.html 3KB
changeBasicInfomation.html 412B
download.html 1KB
login.html 355B
sensor.html 6KB
sensor_classify.html 3KB
data_sensor.html 3KB
network.html 4KB
data_post.html 3KB
index.html 2KB
static
js
dateformat.js 1KB
echarts.min.js 753KB
html
regist.html 10KB
basicInformation.html 9KB
gateway.html 15KB
changePassword.html 10KB
exception.html 13KB
download.html 11KB
login.html 8KB
sensor.html 22KB
data-post.html 11KB
data-sensor.html 14KB
network.html 10KB
index.html 10KB
application.yml 381B
java
com
my
iot
schedule
AsyncTaskService.java 2KB
mapper
GatewayExceptionMapper.java 1KB
UserMapper.java 902B
SensorClassifyMapper.java 1KB
GatewayMapper.java 1KB
SensorExceptionMapper.java 1KB
SensorMapper.java 2KB
DataMapper.java 987B
controller
FileController.java 18KB
DataController.java 6KB
UserController.java 4KB
GatewayExceptionController.java 2KB
SensorExceptionController.java 2KB
GatewayController.java 3KB
SensorClassifyController.java 2KB
HomeController.java 1KB
SensorController.java 6KB
service
SensorService.java 568B
GatewayExceptionService.java 434B
GatewayService.java 384B
DataService.java 401B
UserService.java 203B
SensorExceptionService.java 428B
SensorClassifyService.java 359B
impl
SensorServiceImpl.java 2KB
SensorExceptionServiceImpl.java 2KB
UserServiceImpl.java 941B
SensorClassifyServiceImpl.java 2KB
GatewayServiceImpl.java 3KB
DataServiceImpl.java 2KB
GatewayExceptionServiceImpl.java 2KB
IoTSpringBootApplication.java 322B
util
Md5Util.java 2KB
domain
PageBean.java 2KB
Sensor.java 4KB
Gateway.java 2KB
Result.java 934B
SensorException.java 2KB
SensorClassify.java 1KB
GatewayException.java 2KB
User.java 2KB
Data.java 2KB
config
WebAppConfig.java 1KB
ServerConfig.java 1004B
StringToDateConverter.java 1KB
interceptor
LoginInterceptor.java 2KB
IoT-SpringBoot.iml 80B
.idea
jarRepositories.xml 1KB
uiDesigner.xml 9KB
sqldialects.xml 209B
$CACHE_FILE$ 1KB
vcs.xml 180B
IoT-SpringBoot.iml 80B
misc.xml 616B
DAQ-IoT-SpringBoot.iml 80B
compiler.xml 818B
.gitignore 176B
encodings.xml 308B
README1111.md 7KB
共 94 条
- 1
资源评论
龙年行大运
- 粉丝: 1265
- 资源: 3865
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 联想7400打印机更换定影组件.jpg
- 基于servlet+jsp+mysql实现的影视管理系统课程设计
- GUIdemo.zip
- 正点原子RK3568卡片电脑ATOMPI-CA1的ubuntu-24.04.1最小安装包,特别适合运行板级ROS2环境jazzy
- U盘量产工具SM3280&3281&3282-AvidiaV0209整合版
- 可直接运行 MATLAB数学建模学习资料 模拟算法MATLAB代码实现.rar
- 计算机数学建模中模拟退火算法详解及其TSP问题求解应用
- 基于 Java+SQLServer 实现的医药售卖系统课程设计
- HCNP(HCDP)华为认证资深网络工程师-路由交换方向培训 -IESN中文理论书-内文.pdf
- 新版FPGA课程大纲,芯片硬件开发用的大纲
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功