Home  >  Article  >  Java  >  Java development skills revealed: Implementing Excel file reading and writing functions

Java development skills revealed: Implementing Excel file reading and writing functions

王林
王林Original
2023-11-20 09:09:261094browse

Java development skills revealed: Implementing Excel file reading and writing functions

Java is a programming language widely used to develop various software. Its powerful functions make it the first choice for many developers. In Java development, realizing the reading and writing functions of Excel files is also one of the very common requirements. This article will reveal some development techniques for reading and writing Excel files to help readers better deal with related issues.

First, let’s discuss how to implement the reading function of Excel files. Java provides many libraries for operating Excel files, the most commonly used of which is Apache POI. Through the POI library, we can easily read data in Excel files. The following is a simple example:

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

import java.io.FileInputStream;
import java.io.IOException;

public class ExcelReader {
    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("path/to/excel.xlsx");
            Workbook workbook = new XSSFWorkbook(fis);
            Sheet sheet = workbook.getSheetAt(0);

            for (Row row : sheet) {
                for (Cell cell : row) {
                    CellType cellType = cell.getCellType();
                    if (cellType == CellType.STRING) {
                        String value = cell.getStringCellValue();
                        System.out.print(value + "    ");
                    } else if (cellType == CellType.NUMERIC) {
                        double value = cell.getNumericCellValue();
                        System.out.print(value + "    ");
                    }
                }
                System.out.println();
            }

            workbook.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In the above code, we first obtain the input stream of the Excel file through FileInputStream. Then, use the Workbook object to represent the entire Excel file, and use the Sheet object to represent a worksheet in the Excel file. By traversing the rows and cells of the Sheet object, we can obtain the data of each cell. According to the type of cell, we use the getCellType method to obtain the corresponding data and process it accordingly.

Next, let’s discuss how to implement the writing function of Excel files. Likewise, we can use Apache POI to achieve this functionality. The following is a simple example:

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

import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelWriter {
    public static void main(String[] args) {
        try {
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("Sheet1");

            Row headerRow = sheet.createRow(0);
            Cell headerCell1 = headerRow.createCell(0);
            headerCell1.setCellValue("Name");
            Cell headerCell2 = headerRow.createCell(1);
            headerCell2.setCellValue("Age");

            Row dataRow = sheet.createRow(1);
            Cell dataCell1 = dataRow.createCell(0);
            dataCell1.setCellValue("John");
            Cell dataCell2 = dataRow.createCell(1);
            dataCell2.setCellValue(25);

            FileOutputStream fos = new FileOutputStream("path/to/excel.xlsx");
            workbook.write(fos);

            workbook.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In the above code, we first create a Workbook object that represents the entire Excel file. Then, use the createSheet method to create a worksheet and set the name of the worksheet. Next, we can create rows and cells using the createRow and createCell methods, and set the value of the cell through the setCellValue method.

Finally, we use FileOutputStream to write the Workbook object to the file and close the corresponding stream.

Through the above code examples, we can see that it is not complicated to implement the reading and writing functions of Excel files in Java. Using the methods provided by the Apache POI library, we can easily read and write Excel files. However, it should be noted that the use of the Apache POI library also includes other functions for operating Excel files, such as style setting, merging cells, etc. Readers can further explore according to actual needs.

In short, it is very important for developers to master the skills of implementing Excel file reading and writing functions in Java development. We hope that the simple examples provided in this article can help readers understand and apply relevant knowledge and improve work efficiency in actual development.

The above is the detailed content of Java development skills revealed: Implementing Excel file reading and writing functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn