假设学生成绩均保存在Excel当中。实现学生成绩的增、删、改、查,相应的统计分析功能java
时间: 2023-10-19 20:07:31 浏览: 60
在Java中,你可以使用Apache POI库来操作Excel文件,并实现学生成绩的增、删、改、查功能。以下是一个示例代码,演示了如何使用Apache POI库实现这些功能:
```java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelManager {
private static final String FILE_NAME = "学生成绩.xlsx";
private static final String SHEET_NAME = "成绩";
public static void main(String[] args) {
try {
// 加载Excel文件
FileInputStream file = new FileInputStream(FILE_NAME);
Workbook workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheet(SHEET_NAME);
// 添加学生成绩
addScore(sheet, "张三", 80);
addScore(sheet, "李四", 90);
// 删除学生成绩
deleteScore(sheet, "张三");
// 修改学生成绩
updateScore(sheet, "李四", 95);
// 查询学生成绩
double score = getScore(sheet, "李四");
System.out.println("李四的成绩为:" + score);
// 统计分析
double averageScore = calculateAverageScore(sheet);
System.out.println("平均成绩为:" + averageScore);
// 保存Excel文件
FileOutputStream outFile = new FileOutputStream(FILE_NAME);
workbook.write(outFile);
outFile.close();
// 关闭资源
file.close();
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void addScore(Sheet sheet, String name, double score) {
int lastRowNum = sheet.getLastRowNum();
Row row = sheet.createRow(lastRowNum + 1);
row.createCell(0).setCellValue(name);
row.createCell(1).setCellValue(score);
}
private static void deleteScore(Sheet sheet, String name) {
for (Row row : sheet) {
if (row.getCell(0).getStringCellValue().equals(name)) {
sheet.removeRow(row);
break;
}
}
}
private static void updateScore(Sheet sheet, String name, double newScore) {
for (Row row : sheet) {
if (row.getCell(0).getStringCellValue().equals(name)) {
row.getCell(1).setCellValue(newScore);
break;
}
}
}
private static double getScore(Sheet sheet, String name) {
for (Row row : sheet) {
if (row.getCell(0).getStringCellValue().equals(name)) {
return row.getCell(1).getNumericCellValue();
}
}
return 0;
}
private static double calculateAverageScore(Sheet sheet) {
int count = 0;
double sum = 0;
for (Row row : sheet) {
sum += row.getCell(1).getNumericCellValue();
count++;
}
return sum / count;
}
}
```
请确保已经添加了Apache POI库的依赖。此示例将学生成绩保存在名为"学生成绩.xlsx"的Excel文件中的"成绩"工作表中。你可以根据自己的需求修改文件名和工作表名称。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)