1.需求分析
- 配置
项目文件
环境:IDEA IntelliJ
工具包 :hutool-poi-apidocs
工具包导入:
右击项目文件
open module settings
3.具体实现
(1)学生管理类StudentManager。java
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* ClassName StudentManager
* Description
*
* @author 阿木木
* @date 2020/12/26 10:26
* Version 1.0
*/
public class StudentManager {
static Scanner input = new Scanner(System.in);
public static List<Student> studentArray = new ArrayList<>();
/**
*Description 显示主菜单
**/
public void showMenu(){
do {
Menu.showOperation();
start();
System.out.println("返回操作菜单:y/n?");
} while ("y".equals(input.next()));
}
/**
*Description 系统驱动类
**/
public void start(){
switch (input.nextInt()){
//增加学员信息
case 1:{
Student student = new Student();
do {
if (addStudent(student)) {
System.out.println("增加学员成功");
//增加学员成功,显示成功加入的学员信息
System.out.println(studentArray);
} else {
System.out.println("增加学员失败");
}
System.out.println("是否继续增加:y/n?");
}while ("y".equals(input.next()));
//信息写入excel表格
Excel.excelReader();
//执行完一次增加操作,清空student对象数组
studentArray.clear();
break;
}
case 2:{
//查询学生信息
do{
System.out.print("请输入学号:");
Student student = findByNo(input.next());
if (student.getStudentNo() == null) {
System.out.println("该学号不存在");
}else {
System.out.println(student);
}
System.out.println("是否继续查询:y/n?");
}while ("y".equals(input.next()));
break;
}
case 3:{
//更新学生信息
do{
System.out.println("请输入要更新的学生信息");
updateStudent(inputInfo());
System.out.println("是否继续更新:y/n?");
}while ("y".equals(input.next()));
break;
}
case 4:{
//删除学生信息
do{
System.out.println("请输入要删除的学号:");
removeStudent(input.next());
System.out.println("是否继续删除:y/n?");
}while ("y".equals(input.next()));
break;
}
case 5:{
System.out.println("谢谢使用!");
//System.exit(int status)是中止当前虚拟机的运行,即强制性退出程序。status是状态码,0表示正常退出程序,其他值表示异常退出。
System.exit(0);
break;
}
default:{
break;
}
}
}
/**
*Description case 1
**/
void case1(){
Student student = new Student();
do {
if (addStudent(student)) {
System.out.println("增加学员成功");
//增加学员成功,显示成功加入的学员信息
System.out.println(studentArray);
} else {
System.out.println("增加学员失败");
}
System.out.println("是否继续增加:y/n?");
}while ("y".equals(input.next()));
//信息写入excel表格
Excel.excelReader();
//执行完一次增加操作,清空student对象数组
studentArray.clear();
}
/**
*Description case 2
**/
void case2(){
//查询学生信息
do{
System.out.print("请输入学号:");
Student student = findByNo(input.next());
if (student.getStudentNo() == null) {
System.out.println("该学号不存在");
}else {
System.out.println(student);
}
System.out.println("是否继续查询:y/n?");
}while ("y".equals(input.next()));
}
/**
*Description case 3
**/
void case3(){
//更新学生信息
do{
System.out.println("请输入要更新的学生信息");
updateStudent(inputInfo());
System.out.println("是否继续更新:y/n?");
}while ("y".equals(input.next()));
}
/**
*Description case 4
**/
void case4(){
System.out.println("请输入要删除的学号:");
removeStudent(input.next());
}
/**
*Description 添加学生对象到数组中
**/
boolean addStudent(Student student){
student = inputInfo();
// for (int i = 0; i < studentArray.size(); i++) {
studentArray.add(student);
return true;
// }
// return false;
}
/**
*Description 输入学生信息,并返回学生对象
**/
Student inputInfo(){
Student student = new Student();
System.out.print("请输入学号:");
student.setStudentNo(input.next());
System.out.print("请输入学生姓名:");
student.setStudentName(input.next());
System.out.print("请输入成绩:");
student.setStudentScore(input.next());
return student;
}
/**
*Description 按学号查找学员信息
**/
Student findByNo(String stuNo) {
ExcelReader excelReader = ExcelUtil.getReader("C:\\Users\\Administrator\\Desktop\\学生成绩表.xlsx");
Student student = new Student();
for (int i = 1; i < excelReader.getRowCount(); i++) {
List<Object> objects = excelReader.readRow(i);
if (objects != null) {
String studentNo = objects.get(1).toString();
if (stuNo.equals(studentNo)) {
student.setStudentName(objects.get(0).toString());
student.setStudentNo(studentNo);
student.setStudentScore(objects.get(2).toString());
}
}
}
return student;
}
/**
*Description 更新学生数据
**/
void updateStudent(Student student){
ExcelReader excelReader = ExcelUtil.getReader("C:\\Users\\Administrator\\Desktop\\学生成绩表.xlsx");
for (int i = 1; i < excelReader.getRowCount(); i++) {
List<Object> objects = excelReader.readRow(i);
String studentNo = objects.get(1).toString();
if (student.getStudentNo().equals(studentNo)) {
ExcelWriter excelWriter = ExcelUtil.getWriter("C:\\Users\\Administrator\\Desktop\\学生成绩表.xlsx");
excelWriter.writeCellValue(0, i, student.getStudentName());
excelWriter.writeCellValue(1, i, student.getStudentNo());
excelWriter.writeCellValue(2, i, student.getStudentScore());
excelWriter.flush();
excelWriter.close();
System.out.println("更新成功");
}
}
}
/**
*Description 删除学生信息
**/
void removeStudent(String stuNo){
ExcelReader excelReader = ExcelUtil.getReader("C:\\Users\\Administrator\\Desktop\\学生成绩表.xlsx");
List<Object> objects = new ArrayList<>();
for (int i = 1; i < excelReader.getRowCount(); i++) {
objects = excelReader.readRow(i);
if (objects.get(1).equals(stuNo)) {
Workbook workbook = excelReader.getWorkbook();
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(i);
sheet.removeRow(row);
try {
workbook.write(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\学生成绩表2.xlsx"));
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
(2)菜单类Menu.java
package com.studentscoresystem;
/**
* ClassName Menu
* Description
*
* @author 阿木木
* @date 2020/12/26 10:37
* Version 1.0
*/
public class Menu {
/**
*Description 输出学生信息管理系统操作
**/
static void showOperation(){
System.out.println("+---------------学生信息管理系统--------------------+");
System.out.println("|\t\t1.增加学员信息\t\t\t");
System.out.println("|\t\t2.查找学员信息\t\t\t");
System.out.println("|\t\t3.更新学员信息\t\t\t");
System.out.println("|\t\t4.删除学员信息\t\t\t");
System.out.println("|\t\t5.退出 \t\t\t");
System.out.println("+-------------------------------------------------+");
System.out.println("请选择操作项:(1)增加 (2)查找 (3)更新 (4)删除 (5) 注销");
}
}
(3)学生信息数据类StudeBeam.java
/**
* ClassName Student
* Description
*
* @author 阿木木
* @date 2020/12/26 10:49
* Version 1.0
*/
public class Student {
private String studentNo;
private String studentName;
private String studentScore;
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentScore() {
return studentScore;
}
public void setStudentScore(String studentScore) {
this.studentScore = studentScore;
}
@Override
public String toString() {
return "学生信息:" +
"学号" + studentNo + '\t' +
"姓名:" + studentName + '\t' +
"成绩:" + studentScore + "\t" ;
}
}
(4)系统测试类Main.java
import java.util.Scanner;
/**
* ClassName Main
* Description 系统测试主类
*
* @author 阿木木
* @date 2020/12/26 12:54
* Version 1.0
*/
public class Main {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
StudentManager studentManager = new StudentManager();
studentManager.showMenu();
}
}
4.Excel
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
/**
* ClassName Excel
* Description
*
* @author 阿木木
* @date 2020/12/28 19:42
* Version 1.0
*/
public class Excel {
/**
*Description 数据写入excel
**/
static void excelReader(){
for(Student student1:StudentManager.studentArray) {
ExcelReader excelReader = ExcelUtil.getReader("C:\\Users\\Administrator\\Desktop\\学生成绩表.xlsx");
ExcelWriter excelWriter = ExcelUtil.getWriter("C:\\Users\\Administrator\\Desktop\\学生成绩表.xlsx");
int rowCount = excelReader.getRowCount();
System.out.println(rowCount);
excelWriter.writeCellValue(0, rowCount, student1.getStudentName());
excelWriter.writeCellValue(1, rowCount, student1.getStudentNo());
excelWriter.writeCellValue(2, rowCount, student1.getStudentScore());
excelWriter.flush();
excelWriter.close();
}
}
/**
*Description 读取excel数据
**/
static void excelWriter(){
ExcelReader excelReader = ExcelUtil.getReader("C:\\Users\\Administrator\\Desktop\\学生成绩表.xlsx");
}
}
5.测试截图.java
(1)增加
(2)减少
(3)更新
(4)删除