学生成绩管理系统

//需要导入poi库
//依赖 导入pom.xml
    //<!--使用POI读取文件-->
    //        <dependencies>
    //        <dependency>
    //        <groupId>org.apache.poi</groupId>
    //        <artifactId>poi</artifactId>
    //        <version>5.1.0</version>
    //        </dependency>
    //        <dependency>
    //        <groupId>org.apache.poi</groupId>
    //        <artifactId>poi-ooxml</artifactId>
    //        <version>3.8</version>
    //        </dependency>
    //        <dependency>
    //        <groupId>org.sakaiproject.deploy</groupId>
    //        <artifactId>deploy</artifactId>
    //        <version>22.2</version>
    //        <scope>compile</scope>
    //        </dependency>
    //        </dependencies>
    

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.*;
import java.util.*;

class Student {
    private static int k;
    private static int z;
    //登陆

    //菜单页面
    public interface StudentService {
        static void menu() {
            System.out.println("\t\t\t\t\t\t********************************************************");
            System.out.println("\t\t\t\t\t\t*                                                      *");
            System.out.println("\t\t\t\t\t\t*              欢迎使用学生成绩管理系统                     *");
            System.out.println("\t\t\t\t\t\t*                                                      *");
            System.out.println("\t\t\t\t\t\t********************************************************");
            System.out.println("\t\t\t\t\t\t*              请选择以下功能                             *");
            System.out.println("\t\t\t\t\t\t*              ①添加学生成绩                             *");
            System.out.println("\t\t\t\t\t\t*              ②修改学生成绩                             *");
            System.out.println("\t\t\t\t\t\t*              ③删除学生成绩                             *");
            System.out.println("\t\t\t\t\t\t*              ④查看全部学生成绩                          *");
            System.out.println("\t\t\t\t\t\t*              ⑤按学号查看学生成绩                        *");
            System.out.println("\t\t\t\t\t\t*              ⑥退出                                   *");
            System.out.println("\t\t\t\t\t\t********************************************************");
            System.out.println("请输入你的选择:");
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入你要选择的功能序号:");
            int i = sc.nextInt();
            while (true) {
                switch (i) {

                    case 1:
                        Student.StudentService.createConten(getContent());;
                        break;
                    case 2:
                        Student.StudentService.ChangeExcel();
                        break;
                    case 3:

                        Student.StudentService.deleteExcel();
                        break;
                    case 4:
                        Student.StudentService.excelRead();
                        break;
                    case 5:
                        Student.StudentService.findStudent();
                        break;
                    case 6:
                        System.out.println("谢谢使用,再见!");
                        System.exit(0);

                    default:
                        System.out.println("你输入的序号不正确,请重新输入");
                        break;
                }
                Student.StudentService.menu();
                System.out.print("请输入你要选择的功能序号:");
                i = sc.nextInt();
            }
        }
        static void findStudent(){
            File readFile = new File("D://学生成绩表.xls");
            FileInputStream fileInputStream = null;
            try {

                fileInputStream = new FileInputStream(readFile);
                //
                HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
                //获取sheet
                //workbook.getSheetAt(index);读取索引index上的工作表
                HSSFSheet sheet0 = workbook.getSheet("学生信息成绩表");
                //起始行,与最后一行
                int True = 0;
                while (True != 2) {
                    int lastRow = sheet0.getLastRowNum();
                    Scanner l = new Scanner(System.in);
                    System.out.print("请输入查找的学号学生信息:");
                    String L = l.nextLine();
                    for (int i = 1; i <= lastRow; i++) {
                        //获取数据行
                        HSSFRow row = sheet0.getRow(i);
                        //获取最后单元格列号
                        HSSFCell cell = row.getCell(0);
                        String stringCellValue = cell.getStringCellValue();
                        if (Objects.equals(L, stringCellValue)) {
                            z++;
                            System.out.println("搜索成功");
                            Scanner u = new Scanner(System.in);
                            for (int j=0;j<row.getLastCellNum();j++){
                                HSSFCell cell1 = row.getCell(j);
                                if (i != 0 && (j == 4 || j == 5 || j == 6 || j == 7 || j == 8)) {
                                    double num = cell1.getNumericCellValue();
                                    System.out.print(num + "        ");
                                } else {
                                    String StringCellValue = cell1.getStringCellValue();
                                    System.out.print(StringCellValue + "        ");
                                }
                            }
                            fileInputStream.close();//关闭文件输入流
                            FileOutputStream fos = new FileOutputStream(readFile);
                            workbook.write(fos);
                            fos.close();//关闭文件输出流
                            System.out.println();
                            System.out.println("1.继续查找 2.结束查找");
                            System.out.print("请输入选项:");
                            if (u.nextInt() == 2) True = 2;
                        }
                    }
                    if (z == 0) {
                        System.out.println("没有搜索到信息或学号输入错误");
                        System.out.println("1.重新输入学号 2.返回菜单");
                        Scanner n = new Scanner(System.in);
                        if (n.nextInt() == 1) {
                            deleteExcel();
                        } else
                            menu();

                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }




        }

        //删除学生成绩
        static void deleteExcel() {
            File readFile = new File("D://学生成绩表.xls");
            FileInputStream fileInputStream = null;
            try {

                fileInputStream = new FileInputStream(readFile);
                //
                HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
                //获取sheet
                //workbook.getSheetAt(index);读取索引index上的工作表
                HSSFSheet sheet0 = workbook.getSheet("学生信息成绩表");
                //起始行,与最后一行
                int True = 0;
                while (True != 2) {
                    int lastRow = sheet0.getLastRowNum();
                    Scanner l = new Scanner(System.in);
                    System.out.print("请输入删除学生的学号:");
                    String L = l.nextLine();
                    for (int i = 1; i <= lastRow; i++) {
                        //获取数据行
                        HSSFRow row = sheet0.getRow(i);
                        //获取最后单元格列号
                        HSSFCell cell = row.getCell(0);
                        String stringCellValue = cell.getStringCellValue();
                        if (Objects.equals(L, stringCellValue)) {
                            z++;
                            System.out.println("搜索成功");
                            if (i == lastRow) {
                                sheet0.removeRow(row);
                            }
                            System.out.println("1.删除 2.取消 ");
                            Scanner u = new Scanner(System.in);
                            if (1 == u.nextInt() && i != lastRow) {
                                sheet0.shiftRows(i + 1, lastRow, -1);
                                sheet0.removeRow(sheet0.getRow(lastRow));
                            } else
                                True = 2;
                            fileInputStream.close();//关闭文件输入流
                            FileOutputStream fos = new FileOutputStream(readFile);
                            workbook.write(fos);
                            System.out.println("保存成功");
                            fos.close();//关闭文件输出流
                            System.out.println("1.继续删除 2.结束删除");
                            System.out.print("请输入选项:");
                            if (u.nextInt() == 2) True = 2;
                        }
                    }
                    if (z == 0) {
                        System.out.println("没有搜索到信息或学号输入错误");
                        System.out.println("1.重新输入学号 2.返回菜单");
                        Scanner n = new Scanner(System.in);
                        if (n.nextInt() == 1) {
                            deleteExcel();
                        } else
                            ;

                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        //通过学号查找修改
        static void ChangeExcel1(String L, String stringCellValue, int i) {

            File readFile = new File("D://学生成绩表.xls");
            FileInputStream fileInputStream = null;
            try {
                fileInputStream = new FileInputStream(readFile);
                //
                HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
                //获取sheet
                //workbook.getSheetAt(index);读取索引index上的工作表
                HSSFSheet sheet0 = workbook.getSheet("学生信息成绩表");
                //起始行,与最后一行
                //获取数据行
                HSSFRow row = sheet0.getRow(i);
                //获取最后单元格列号
                if (Objects.equals(L, stringCellValue)) {
                    k++;
                    System.out.println("搜索成功");
                    while (true) {
                        System.out.print("请输入要修改的选项:");
                        System.out.println("1.学号 2.姓名 3.性别 4.专业班级 5.英语 6.高等数学 7.政治 8.Java 9.python 10.总成绩");
                        Scanner u = new Scanner(System.in);
                        Scanner U = new Scanner(System.in);
                        int v = u.nextInt();
                        HSSFCell cell = row.getCell(v - 1);
                        System.out.println("请输人要修改的内容:");
                        if (v == 5 || v == 6 || v == 7 || v == 8 || v == 9)
                            cell.setCellValue(Double.parseDouble(U.nextLine()));
                        else
                            cell.setCellValue(U.nextLine());
                        fileInputStream.close();//关闭文件输入流
                        FileOutputStream fos = new FileOutputStream(readFile);
                        workbook.write(fos);
                        System.out.println("保存成功");
                        fos.close();//关闭文件输出流
                        System.out.println("1.继续修改 2.结束修改");
                        System.out.print("请输入选项:");
                        if (u.nextInt() == 2) break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


        }

        static void ChangeExcel() {
            File readFile = new File("D://学生成绩表.xls");
            FileInputStream fileInputStream = null;
            try {

                fileInputStream = new FileInputStream(readFile);
                //
                HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
                //获取sheet
                //workbook.getSheetAt(index);读取索引index上的工作表
                HSSFSheet sheet0 = workbook.getSheet("学生信息成绩表");
                //起始行,与最后一行

                int lastRow = sheet0.getLastRowNum();
                Scanner l = new Scanner(System.in);
                System.out.print("请输入学号:");
                String L = l.nextLine();
                for (int i = 1; i <=lastRow; i++) {
                    //获取数据行
                    HSSFRow row = sheet0.getRow(i);
                    //获取最后单元格列号
                    HSSFCell cell = row.getCell(0);
                    String stringCellValue = cell.getStringCellValue();
                    ChangeExcel1(L, stringCellValue, i);
                }
                if (k == 0) {
                    System.out.println("没有搜索到信息或学号输入错误");
                    System.out.println("1.重新输入学号 2.返回菜单");
                    Scanner n = new Scanner(System.in);
                    if (n.nextInt() == 1) {
                        ChangeExcel();
                    } else
                        menu();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        //读取全部数据
        static void excelRead() {
            File readFile = new File("D://学生成绩表.xls");
            FileInputStream fileInputStream = null;
            try {
                fileInputStream = new FileInputStream(readFile);
                //
                HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
                //获取sheet
                //workbook.getSheetAt(index);读取索引index上的工作表
                HSSFSheet sheet0 = workbook.getSheet("学生信息成绩表");
                //起始行,与最后一行

                int lastRow = sheet0.getLastRowNum();

                for (int i = 0; i <= lastRow; i++) {
                    //获取数据行
                    HSSFRow row = sheet0.getRow(i);
                    //获取最后单元格列号
                    int lastCellNum = row.getLastCellNum();

                    for (int j = 0; j < lastCellNum; j++) {
                        HSSFCell cell = row.getCell(j);
                        if (i != 0 && (j == 4 || j == 5 || j == 6 || j == 7 || j == 8)) {
                            double num = cell.getNumericCellValue();
                            System.out.print(num + "        ");
                        } else {
                            String stringCellValue = cell.getStringCellValue();
                            System.out.print(stringCellValue + "        ");
                        }
                    }
                    System.out.println();
                }

            } catch (Exception e) {
                e.printStackTrace();

            }
        }

        //增加学生成绩信息
        static List<List<String>> getContent() {
            List<List<String>> contentList = new ArrayList<>();
            while (true) {
                List<String> content1 = new ArrayList<>();
                Scanner A = new Scanner(System.in);
                List<String> headList = new ArrayList<>();
                headList.add("学号:");
                headList.add("姓名:");
                headList.add("性别:");
                headList.add("专业班级:");
                headList.add("英语:");
                headList.add("高等数学:");
                headList.add("政治:");
                headList.add("Java:");
                headList.add("python:");
                headList.add("总成绩:");
                for (int i = 0; i < 9; i++) {
                    System.out.print(headList.get(i));
                    content1.add(A.nextLine());
                }
                contentList.add(content1);
                System.out.println("是否继续增加,1.增加  2.结束");
                Scanner B = new Scanner(System.in);
                System.out.print("请输入:");
                int i = B.nextInt();
                if (i == 2)
                    break;
            }
            return contentList;
        }

        static void createConten(List<List<String>> contentList1) {
            File readFile = new File("D://学生成绩表.xls");
            FileInputStream fileInputStream;

            try {
                fileInputStream = new FileInputStream(readFile);
                //
                HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
                //获取sheet
                //workbook.getSheetAt(index);读取索引index上的工作表
                HSSFSheet sheet0 = workbook.getSheet("学生信息成绩表");
                //起始行,与最后一行
                int lastRow = sheet0.getLastRowNum() + 1;
                for (int i = 0; i < contentList1.size(); i++) {
                    HSSFRow Row = sheet0.createRow(lastRow++);
                    for (int j = 0; j < contentList1.get(i).size(); j++) {
                        HSSFCell cell = Row.createCell(j);
                        if (j == 4 || j == 5 || j == 6 || j == 8 || j == 7)
                            cell.setCellValue(Double.parseDouble(contentList1.get(i).get(j)));
                        else
                            cell.setCellValue(contentList1.get(i).get(j));
                    }
                }
                try {
                    workbook.write(new FileOutputStream(readFile));
                    System.out.println("保存成功");
                    workbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        class StudentMain {
            public static void main(String[] args){
                Student.StudentService.menu();
            }
        }

    }

}









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浪仙545

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值