Programming - Teach you step by step how to read student information from Excel files

In today's information age, computer technology has penetrated into various fields, and programming has become one of the key technologies to promote information construction. Among many fields, student information management system is undoubtedly one of the important applications. This article will start from the development of student information management system and discuss how to complete the development efficiently and ensure quality.

Suppose there is a student information management system that needs to read students' names, ages, grades and other data from Excel files, and store these data in the system for further processing and management.

Specifically, by calling this function in the data import/export system and passing in the Excel file as a parameter, you can get results containing student information. This result can then be further processed, such as storing student data into a database or performing other business logic operations.

Different customized data processing demand scenarios may have subtle differences in details, and it would be too cumbersome to write it from scratch. At this time:

1 ) Think about the general needs and ask FuncGPT

For example, input requirements:

Read EXCEL data, input parameter: File file, output parameter: List<Map<String, Object>>

Get the complete code.

// Class name: ExcelDataReader

// Function name: readExcelData

// Function: Read Excel data and return results of type List<Map<String, Object>>

// POM dependency package:

// <dependency>

//     <groupId>org.apache.poi</groupId>

//     <artifactId>poi</artifactId>

//     <version>4.1.2</version>

// </dependency>

// <dependency>

//     <groupId>org.apache.poi</groupId>

//     <artifactId>poi-ooxml</artifactId>

//     <version>4.1.2</version>

// </dependency>

 

import org.apache.poi.ss.usermodel.*;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

public class ExcelDataReader {

    /**

     * Read Excel data and return results of type List<Map<String, Object>>

     * @param file Excel file

     * @return List<Map<String, Object>> result of Excel data

     * @throws IOException throws IOException when an error occurs while reading the Excel file

     */

    public static List<Map<String, Object>> readExcelData(File file) throws IOException {

        List<Map<String, Object>> dataList = new ArrayList<>(); // Create a List to store Excel data

 

        FileInputStream fis = new FileInputStream(file); // Create a file input stream

        Workbook workbook = WorkbookFactory.create(fis); // Create Workbook object

 

        Sheet sheet = workbook.getSheetAt(0); // Get the first Sheet

        int rowCount = sheet.getLastRowNum(); // Get the number of rows

 

        Row headerRow = sheet.getRow(0); // Get the header row

        int columnCount = headerRow.getLastCellNum(); // Get the number of columns

 

        for (int i = 1; i <= rowCount; i++) { // Traverse each row (skip the header row)

            Row row = sheet.getRow(i);

            Map<String, Object> rowData = new HashMap<>();

 

            for (int j = 0; j < columnCount; j++) { // Traverse each column

                Cell cell = row.getCell(j);

                String columnName = headerRow.getCell(j).getStringCellValue(); // Get the column name

 

                Object cellValue;

                switch (cell.getCellType()) { // Get cell value based on cell type

                    case STRING:

                        cellValue = cell.getStringCellValue();

                        break;

                    case NUMERIC:

                        cellValue = cell.getNumericCellValue();

                        break;

                    case BOOLEAN:

                        cellValue = cell.getBooleanCellValue();

                        break;

                    case FORMULA:

                        cellValue = cell.getCellFormula();

                        break;

                    default:

                        cellValue = null;

                }

 

                rowData.put(columnName, cellValue); // Save column name and cell value into rowData

            }

 

            dataList.add(rowData); // Save rowData into dataList

        }

 

        workbook.close(); // Close the Workbook object

        fis.close(); // Close the file input stream

 

        return dataList; // Return the results of Excel data

    }

}

 

// function example

// Example of reading Excel data

// Input parameters: file, Excel file

// Output parameters: dataList, List<Map<String, Object>> result of Excel data

// Call example:

// File file = new File("data.xlsx");

// List<Map<String, Object>> dataList = ExcelDataReader.readExcelData(file);

// System.out.println(dataList);

// Output results: For example, the Excel data is:

// | Name  | Age | Grade |

// |-------|-----|-------|

// | Alice | 18  | A     |

// | Bob   | 20  | B     |

// The output result is: [{Name=Alice, Age=18, Grade=A}, {Name=Bob, Age=20, Grade=B}]

// Indicates that the Excel data is encapsulated in the format of List<Map<String, Object>>

2 ) Modify according to the code of FuncGPT

Since the overall format content of EXCEL will affect reading the real data of valid rows or columns, it is necessary to perform relevant valid verification on rows and columns. Based on the original code, make some modifications:

First determine the column, because the actual length of the column will affect the reading of the row;

Read the header of the first row. If a blank column is encountered, the valid column is determined to be the subscript of the previous column.

③After obtaining the valid columns, traverse each row of data. If there is an entire row of data, jump out of the loop and end the data acquisition.

With the rapid development of the software industry, software quality has become a crucial issue on the basis of improving software efficiency. A high-quality software can not only improve user satisfaction, but also reduce maintenance and upgrade costs, bringing long-term benefits to the enterprise. Free use link: https://c.suo.nz/bAm2D

Bun releases official version 1.0, a magical bug in Windows File Explorer when JavaScript is runtime written by Zig , improves performance in one second JetBrains releases Rust IDE: RustRover PHP latest statistics: market share exceeds 70%, the king of CMS transplants Python programs To Mojo, the performance is increased by 250 times and the speed is faster than C. The performance of .NET 8 is greatly improved, far ahead of .NET 7. Comparison of the three major runtimes of JS: Deno, Bun and Node.js Visual Studio Code 1.82 NetEase Fuxi responded to employees "due to BUG was threatened by HR and passed away. The Unity engine will charge based on the number of game installations (runtime fee) starting next year.
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4868096/blog/10110763