SoFunction
Updated on 2025-04-14

Use Java implementation to get excel attachments and parse

Handling OLE objects in Excel using Apache POI

Core code analysis

import .Ole10Native;
import .Ole10NativeException;
import ;

try (XSSFWorkbook workbook = new XSSFWorkbook(())) {
    // Get all embedded files    List<PackagePart> partList = ();
    for (PackagePart part : partList) {
        InputStream emb = ();
        if (emb != null) {
            // parse ole file            POIFSFileSystem fs = new POIFSFileSystem(emb);
            Ole10Native ole = (());
            // Get and process the original file name            String originalName = ();
            // Get the file content            byte[] fileContent = ();
            // You can add business processing logic here        }
    }
} catch (RuntimeException e) {
    throw new RuntimeException(e);
}

1. Get the file stream

  • From the file system:new FileInputStream("path/to/")
  • From the uploaded MultipartFile:()

2. Load Excel workbook

useXSSFWorkbookClass loads Excel files in XLSX format:

try (XSSFWorkbook workbook = new XSSFWorkbook(())) {
}

try-with-resourcesStatements ensure that the workbook is automatically closed after use.

3. Get all embedded objects

getAllEmbeddedParts()Method returns a list of all embedded objects in the workbook:

List<PackagePart> partList = ();

4. Process each embedded object

Iterate through all embedded objects:

for (PackagePart part : partList) {
    InputStream emb = ();
    if (emb != null) {
        // Process input stream    }
}

5. Parsing OLE objects

usePOIFSFileSystemParsing OLE objects:

POIFSFileSystem fs = new POIFSFileSystem(emb);
Ole10Native ole = (());

6. Obtain OLE object information

Extract useful information from OLE objects:

String originalName = ();  // Original file namebyte[] fileContent = ();  // File content

Practical application scenarios

File Extraction: Extract and save embedded OLE objects to the file system

Content analysis: Check whether the content of the embedded file meets security requirements

Data migration: Convert embedded objects to other formats

Document audit: Record information about all embedded objects in the document

Method supplement

Java to read attachments in excel

Preparation

Before we start, we need to prepare the following tools and libraries:

  • Apache POI: A Java library for processing Microsoft Office format files.
  • Excel file: Excel file containing attachments.

You can add dependencies to the Apache POI library in Maven:

<dependency>
    <groupId></groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>

Read attachments in Excel

First, we need to create a Java class to read attachments in Excel files. Here is a sample code:

import .*;

import ;
import ;

public class ExcelAttachmentReader {
    
    public void readAttachments(String filePath) {
        try {
            FileInputStream file = new FileInputStream(filePath);
            Workbook workbook = (file);
            Sheet sheet = (0);

            for (Row row : sheet) {
                for (Cell cell : row) {
                    if (() == ) {
                        Attachment attachment = ();
                        // Handle attachments                        ("Attachment found: " + attachment);
                    }
                }
            }

            ();
        } catch (IOException e) {
            ();
        }
    }
    
    public static void main(String[] args) {
        ExcelAttachmentReader reader = new ExcelAttachmentReader();
        ("");
    }
}

In this code, we first open the Excel file through FileInputStream and create the Workbook object using WorkbookFactory. Then we iterate through each cell, if the cell is of type ATTACHMENT, then we get the attachment object and process it.

This is the article about using Java to obtain and parse excel attachments and parsing. For more related Java Excel attachments to obtain and parse content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!