How to flexibly read Excel content using Java?

In daily work and study, we often need to read data from Excel files. For small amounts of data, manual copying and pasting might work, but for large amounts of data, this approach becomes very inefficient. In this case, it would be a better choice to use a Java program to automatically read the data in the Excel file.

This tutorial will introduce how to use a Java program to read data in an Excel file, including the following aspects:

  1. Understanding the Excel file format
  2. Choose Java's Excel file reading library
  3. Read data from Excel file
  4. Handle different data types in Excel files
  5. Export Excel file using Java program

let's start!


1. Understand the Excel file format

Before we start writing Java programs to read Excel files, we need to understand the Excel file format. Excel files are based on Microsoft's OLE2 format, which organizes files into a series of "containers" and "objects." The main container of an Excel file is a Workbook, which contains one or more Sheets. Each Sheet contains a set of rows and columns that form a two-dimensional array of cells. Each cell can contain different data types such as text, numbers, dates, Boolean values, etc.

2. Select Java's Excel file reading library

In Java, there are several libraries that can be used to read Excel files. These libraries include Apache POI, JExcelApi, EasyXLS, JXL, etc. In this tutorial, we will use the Apache POI library.

Apache POI is a Java API for manipulating Microsoft document formats, including Word, Excel, and PowerPoint. It provides a set of Java classes that can be used to create, read, and modify Microsoft documents.

3. Read the data in the Excel file

In this section, we will demonstrate how to read data from an Excel file using the Apache POI library.

Before we start, we need to add the Apache POI library to the project's dependencies. You can add them to your project through build tools like Maven or Gradle.

The following is the sample code for adding the Apache POI library in the pom.xml file of the Maven project:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>

The following is the sample code for adding the Apache POI library in the build.gradle file of the Gradle project:

dependencies {
    implementation 'org.apache.poi:poi:4.1.2'
}

Next, we'll demonstrate how to use the Apache POI library to read data from an Excel file.

First, we need to create a Workbook object that represents the entire Excel file. In Apache POI, there are three different types of Workbook objects: HSSFWorkbook represents a .xls file, XSSFWorkbook represents a .xlsx file, and SXSSFWorkbook represents a large .xlsx file. We can use the WorkbookFactory.create() method to create a Workbook object based on the type of the file. The following is sample code to create an XSSFWorkbook object:

FileInputStream file = new FileInputStream(new File("path/to/excel/file.xlsx"));
Workbook workbook = WorkbookFactory.create(file);

Next, we can get a Sheet object, which represents a worksheet in the Excel file. In Apache POI, the Sheet object is returned by the Workbook object's getSheet() method. Here is sample code to get a Sheet object named "Sheet1":

Sheet sheet = workbook.getSheet("Sheet1");

Once we have the Sheet object, we can iterate through each row and column to read the cell data in it. The following is the sample code to traverse all the cells in the Sheet object and output the cell value:

Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
    Row row = rowIterator.next();
    Iterator<Cell> cellIterator = row.cellIterator();
    while (cellIterator.hasNext()) {
        Cell cell = cellIterator.next();
        switch (cell.getCellType()) {
            case STRING:
                System.out.print(cell.getStringCellValue() + "\t");
                break;
            case NUMERIC:
                System.out.print(cell.getNumericCellValue() + "\t");
                break;
            case BOOLEAN:
                System.out.print(cell.getBooleanCellValue() + "\t");
                break;
            default:
                System.out.print("\t");
        }
    }
    System.out.println();
}

In the above code, we use a nested iterator to traverse each row and each column. In each cell, we use switch statements to perform different operations based on different data types.

4. Handle different data types in Excel files

In an Excel file, cells can contain different data types, including strings, numbers, dates, and Boolean values. When we read data in a cell, we need to know the type of data in the cell to handle them correctly.

In Apache POI, we can use the Cell.getCellType() method to get the cell's data type. This method returns the value of the CellType enumeration type, and you can use the switch statement to perform different operations according to different enumeration values.

The following is sample code using switch statement to handle different data types:

switch (cell.getCellType()) {
    case STRING:
        System.out.print(cell.getStringCellValue() + "\t");
        break;
    case NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            System.out.print(cell.getDateCellValue() + "\t");
        } else {
            System.out.print(cell.getNumericCellValue() + "\t");
        }
        break;
    case BOOLEAN:
        System.out.print(cell.getBooleanCellValue() + "\t");
        break;
    default:
        System.out.print("\t");
}

In the above sample code, we first check if the data type of the cell is string. If it is a string, we use the getStringCellValue() method to get the string value. We also need to check if the cell contains a date if the data type of the cell is number. If it is a date, we use the getDateCellValue() method to get the date value, otherwise we use the getNumericCellValue() method to get the numeric value.

5. Use Java program to export Excel file

In addition to reading data in Excel files, we can also use Java programs to create and export Excel files. Apache POI provides two classes, HSSFWorkbook and XSSFWorkbook, for creating and manipulating Excel files.

The HSSFWorkbook class is used to manipulate the legacy .xls format Excel files, while the XSSFWorkbook class is used to manipulate the newer .xlsx format Excel files. We can choose the appropriate class to create and export Excel files according to our needs.

Here are the general steps to create and export an Excel file:

  1. Create a Workbook object that represents the entire Excel file.
  2. Creates a Sheet object that represents a worksheet in an Excel file.
  3. Create Row and Cell objects representing rows and columns in the Excel file.
  4. Set data and styles for each cell.
  5. Writes a Workbook object to a file or output stream.

Next, let's look at some sample code that demonstrates how to create and export an Excel file using a Java program.

  1. Create an XSSFWorkbook object

The following is sample code to create an XSSFWorkbook object:

Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
  1. Create rows and cells

Here is sample code to create rows and cells:

Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Hello, World!");
  1. Set cell style

In an Excel file, we can set different styles for cells, including font, color, alignment, etc. Here is sample code to style a cell:

CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBold(true);
style.setFont(font);
cell.setCellStyle(style);

In the above sample code, we first use the createCellStyle() method to create a cell style object. Then, we use the createFont() method to create a font object and set its properties. Finally, we apply the style to the cell.

  1. Export Excel file

When exporting an Excel file, we can write the Workbook object to a file or to an output stream. Here is sample code that writes a Workbook object to a file:

FileOutputStream fileOut = new FileOutputStream("path/to/excel/file.xlsx");
workbook.write(fileOut);
fileOut.close();

In the above sample code, we first use the FileOutputStream class to create a file output stream object. Then, we write the Workbook object to the file using the write() method. Finally, we close the file output stream.

In addition to writing to a file, we can also output the Workbook object to a network stream to download the Excel file in the browser. Here is sample code that outputs a Workbook object to a network stream:

response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=\"excel_file.xlsx\"");
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.close();

In the sample code above, we first set the content type and filename of the HTTP response. Then, we use the getOutputStream() method to get the network output stream object. Finally, we write the Workbook object to the network stream using the write() method and close the output stream.

Complete sample code

Here is a complete sample code that demonstrates how to read and export an Excel file using a Java program:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtils {

    public static void readExcel(String fileName) throws IOException {
        // 创建文件输入流
        FileInputStream fileInputStream = new FileInputStream(new File(fileName));

        // 创建工作簿对象
        Workbook workbook = WorkbookFactory.create(fileInputStream);

        // 获取第一个工作表
        Sheet sheet = workbook.getSheetAt(0);

        // 遍历工作表中的所有行和单元格
        for (Row row : sheet) {
            for (Cell cell : row) {
                // 根据单元格的类型读取数据
                switch (cell.getCellType()) {
                    case STRING:
                        System.out.print(cell.getStringCellValue() + "\t");
                        break;
                    case NUMERIC:
                        System.out.print(cell.getNumericCellValue() + "\t");
                        break;
                    case BOOLEAN:
                        System.out.print(cell.getBooleanCellValue() + "\t");
                        break;
                    case FORMULA:
                        System.out.print(cell.getCellFormula() + "\t");
                        break;
                    case BLANK:
                        System.out.print("\t");
                        break;
                    default:
                        System.out.print("\t");
                }
            }
            System.out.println();
        }

        // 关闭文件输入流
        fileInputStream.close();
    }

    public static void writeExcel(String fileName) throws IOException {
        // 创建工作簿对象
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Sheet1");

        // 创建行和单元格
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("Hello, World!");

        // 设置单元格样式
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        cell.setCellStyle(style);

        // 导出 Excel 文件
        FileOutputStream fileOut = new FileOutputStream(fileName);
        workbook.write(fileOut);
        fileOut.close();
    }
}

Using this class, we can easily read and export Excel files as follows:

public static void main(String[] args) throws IOException {
    String fileName = "path/to/excel/file.xlsx";

    // 读取 Excel 文件
    ExcelUtils.readExcel(fileName);

    // 导出 Excel 文件
    ExcelUtils.writeExcel(fileName);
}

6 Conclusion

This article introduces how to flexibly read and export Excel files using a Java program. We started by introducing the Apache POI library, a powerful Java tool for manipulating Office documents. We then demonstrated how to read an Excel file using the POI library and explained how to style cells on a cell-by-cell basis. Finally, we provide a complete sample code that demonstrates how to use the ExcelUtils class to read and export Excel files.

Reading and exporting Excel files from a Java program is a very useful skill, especially in projects that need to process large amounts of data. Using the Apache POI library, we can easily read and write Excel files, which can greatly improve our work efficiency.

In practical applications, we need to choose the method of reading and writing Excel files according to specific needs in order to achieve the best results.

references

Guess you like

Origin blog.csdn.net/bairo007/article/details/131818662