本项目是一个简单的spring项目,使用了spring+mybatis,实现数据的增,删,查,和poi的基本使用,导出数据库中的请假信息到excel中等一些基本的功能。有很多不足之处欢迎大家指出,后面慢慢学习会慢慢优化一些地方……
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 实例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1 align="center">个人信息统计系统</h1>
<p align="center">在这里提交你的信息</p>
</div>
<div class="row" align="center">
<input type="text" class="form-control" style="height:40px;width:450px" placeholder="请假人姓名">
<div class="panel panel-info" style="width: 450px">
<div class="panel-heading">
<h3 class="panel-title" align="center">请假原因</h3>
</div>
<textarea class="form-control" rows="5" style="resize: none;height: 100%;width: 100%"></textarea>
</div>
<div>
<button type="button" class="btn btn-success">提交</button>
</div>
</div>
</div>
</body>
</html>
package com.leave.controller;
import com.leave.po.Leave;
import com.leave.service.LeaveService;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* @ClassName LeaveController
* @Description
* @Author martind
* @Date 2018/8/25 16:05
**/
@Controller
@RequestMapping(value = "leave")
public class LeaveController {
@Autowired
private LeaveService leaveService;
@RequestMapping(value = "/leavePage")
public String leaveSystem(){
return "leave";
}
/**
* 提交表单到该页面,进行数据库的插入操作
*
* @m_temp 是一个临时的功能,提交表单后会跳转到一个查询结果的界面,列出所有查询结果
*
* @param leave
* @param model
* @return
* @throws IOException
*/
@RequestMapping(value = "leave")
public String addLeave(Leave leave, Model model) throws IOException {
leaveService.addLeave(leave);
/*List<Leave> leaveList = leaveService.queryAll();
model.addAttribute("queryAll",leaveList);
leaveService.leaveOutPut(leaveList,"请假人名单.xlsx");*/
return "success";
}
/**
* 删除数据库操作
* 在该操作中先保存数据到*.xlsx中
*
* @return 删除成功界面
* @throws IOException
*/
@RequestMapping(value = "delete")
public String deleteLeave() throws IOException {
List<Leave> leavesList = leaveService.queryAll();
leaveService.leaveOutPut(leavesList,"deleteBefore.xlsx");
leaveService.deleteLeave();
return "deleteSuccess";
}
//下载服务器中生成的excel文件
@RequestMapping("/downLeave")
public ResponseEntity<byte[]> download() throws IOException {
leaveService.leaveOutPut(leaveService.queryAll(),"LeaveName.xlsx");
File file = new File("LeaveName.xlsx");
byte[] body = null;
InputStream is = new FileInputStream(file);
body = new byte[is.available()];
is.read(body);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attchement;filename=" + file.getName());
HttpStatus statusCode = HttpStatus.OK;
ResponseEntity<byte[]> entity = new ResponseEntity<>(body, headers, statusCode);
return entity;
}
//用户管理界面,用于导出数据或者清空数据库
@RequestMapping("/admin")
public String adminPage(Model model) throws IOException {
List<Leave> leaveList = leaveService.queryAll();
model.addAttribute("leaveList",leaveList);
return "adminPage";
}
}
-
插入数据界面
-
插入成功界面
-
管理员管理界面,要手动地址,地址为下图地址输入框中地址,在这里管理员可以下载数据为excel和清空数据库,清空数据库之前会先导出数据备份。
- 导出的数据
最后附上我的代码地址