杰瑞科技汇

Java CSV如何快速转Excel?

Of course! Converting a CSV (Comma-Separated Values) file to an Excel (XLSX) file is a common task in Java. There are several ways to do this, ranging from using powerful, dedicated libraries to leveraging built-in Java functionality.

Java CSV如何快速转Excel?-图1
(图片来源网络,侵删)

Here’s a comprehensive guide covering the most popular and effective methods.

Summary of Methods

Method Pros Cons Best For
Apache POI Industry standard, powerful, full control over Excel features. Steeper learning curve, large JAR size. Most professional applications, complex Excel formatting.
OpenCSV + EasyExcel Excellent performance, memory-efficient, good for large files. Requires two libraries, slightly more setup. Processing very large CSV/Excel files (>10,000 rows).
Java 8 Streams + Apache POI Modern, functional, concise code. Still requires POI, can be less readable for some. Developers comfortable with Java 8 Streams API.
Simple Manual Parsing No external dependencies. Brittle (doesn't handle quotes, newlines well), no Excel features. Quick and dirty scripts for simple, perfectly formatted CSVs.

Method 1: Using Apache POI (The Standard Approach)

Apache POI is the most popular Java library for manipulating Microsoft Office formats, including Excel. It's robust and feature-rich.

Step 1: Add Apache POI Dependency

You need to add the POI dependencies to your project. If you're using Maven, add this to your pom.xml:

<dependencies>
    <!-- For XLSX format -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.5</version>
    </dependency>
</dependencies>

Step 2: Write the Java Code

This code reads a CSV file line by line, splits it by commas, and writes the data into an Excel sheet.

Java CSV如何快速转Excel?-图2
(图片来源网络,侵删)
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class CsvToExcel {
    public static void main(String[] args) {
        String csvFile = "path/to/your/input.csv";
        String excelFile = "path/to/your/output.xlsx";
        try (BufferedReader br = new BufferedReader(new FileReader(csvFile));
             Workbook workbook = new XSSFWorkbook();
             java.io.FileOutputStream fileOut = new java.io.FileOutputStream(excelFile)) {
            Sheet sheet = workbook.createSheet("Sheet1");
            String line;
            int rowIdx = 0;
            // Read CSV file line by line
            while ((line = br.readLine()) != null) {
                String[] values = line.split(","); // Simple split, may need improvement for complex CSVs
                Row row = sheet.createRow(rowIdx++);
                for (int i = 0; i < values.length; i++) {
                    Cell cell = row.createCell(i);
                    // Try to parse as number, otherwise set as string
                    try {
                        double num = Double.parseDouble(values[i]);
                        cell.setCellValue(num);
                    } catch (NumberFormatException e) {
                        cell.setCellValue(values[i]);
                    }
                }
            }
            // Auto-size columns for better readability
            for (int i = 0; i < rowIdx; i++) {
                sheet.autoSizeColumn(i);
            }
            System.out.println("Excel file created successfully!");
            workbook.write(fileOut);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Limitation of the simple split(","): This basic approach fails if your CSV data contains commas within quoted fields (e.g., "Doe, John", "New York"). For robust CSV parsing, use a library like OpenCSV or Apache Commons CSV.


Method 2: High Performance with OpenCSV and EasyExcel

For very large files, Apache POI can consume a lot of memory because it loads the entire workbook into RAM. A better approach is to use a streaming library for writing.

  • OpenCSV: Excellent for reading CSVs robustly.
  • EasyExcel (from Alibaba): An extremely popular and fast library for writing Excel files in a streaming fashion, which is very memory-efficient.

Step 1: Add Dependencies

<dependencies>
    <!-- For robust CSV reading -->
    <dependency>
        <groupId>com.opencsv</groupId>
        <artifactId>opencsv</artifactId>
        <version>5.8</version>
    </dependency>
    <!-- For high-performance Excel writing -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>easyexcel</artifactId>
        <version>3.3.2</version>
    </dependency>
</dependencies>

Step 2: Write the Java Code

This example reads the CSV line by line and writes it to the Excel file row by row without loading everything into memory.

import com.alibaba.excel.EasyExcel;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CsvToExcelHighPerformance {
    public static void main(String[] args) {
        String csvFile = "path/to/your/large_input.csv";
        String excelFile = "path/to/your/output.xlsx";
        // Use try-with-resources to ensure all resources are closed
        try (CSVReader reader = new CSVReader(new FileReader(csvFile))) {
            // Read header
            String[] header = reader.readNext();
            if (header == null) {
                System.out.println("CSV file is empty.");
                return;
            }
            // EasyExcel writes data in a streaming way
            EasyExcel.write(excelFile, String[].class)
                    .sheet("Sheet1")
                    .doWrite(() -> {
                        List<String[]> dataToWrite = new ArrayList<>();
                        String[] nextLine;
                        try {
                            // Read each line from CSV
                            while ((nextLine = reader.readNext()) != null) {
                                dataToWrite.add(nextLine);
                                // Write in batches to balance memory and performance
                                if (dataToWrite.size() >= 1000) {
                                    return dataToWrite;
                                }
                            }
                        } catch (IOException | CsvValidationException e) {
                            throw new RuntimeException("Error reading CSV file", e);
                        }
                        return dataToWrite;
                    });
            System.out.println("High-performance Excel file created successfully!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Method 3: Using Java 8 Streams (Modern & Concise)

This method combines the modern Streams API with Apache POI for a more functional and often more readable approach.

Java CSV如何快速转Excel?-图3
(图片来源网络,侵删)

Step 1: Add Apache POI Dependency (Same as Method 1)

Step 2: Write the Java Code

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Stream;
public class CsvToExcelWithStreams {
    public static void main(String[] args) {
        String csvFile = "path/to/your/input.csv";
        String excelFile = "path/to/your/output.xlsx";
        Path csvPath = Paths.get(csvFile);
        try (BufferedReader br = Files.newBufferedReader(csvPath, StandardCharsets.UTF_8);
             Workbook workbook = new XSSFWorkbook();
             java.io.FileOutputStream fileOut = new java.io.FileOutputStream(excelFile)) {
            Sheet sheet = workbook.createSheet("Sheet1");
            // Use streams to process lines
            try (Stream<String> stream = br.lines()) {
                Object[] data = stream.toArray(); // Collect all lines into an array
                // Create header row from the first line
                if (data.length > 0) {
                    String[] header = ((String) data[0]).split(",");
                    Row headerRow = sheet.createRow(0);
                    createRowFromData(headerRow, header);
                }
                // Fill data rows from the rest of the lines
                for (int i = 1; i < data.length; i++) {
                    String[] lineData = ((String) data[i]).split(",");
                    Row row = sheet.createRow(i);
                    createRowFromData(row, lineData);
                }
            }
            // Auto-size columns
            for (int i = 0; i < sheet.getRow(0).getPhysicalNumberOfCells(); i++) {
                sheet.autoSizeColumn(i);
            }
            System.out.println("Excel file created with streams!");
            workbook.write(fileOut);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void createRowFromData(Row row, String[] data) {
        for (int i = 0; i < data.length; i++) {
            Cell cell = row.createCell(i);
            try {
                // Try to set as numeric value
                cell.setCellValue(Double.parseDouble(data[i]));
            } catch (NumberFormatException e) {
                // Set as string if not a number
                cell.setCellValue(data[i]);
            }
        }
    }
}

Method 4: Simple Manual Parsing (No Libraries)

This is the most basic approach and is not recommended for production use because it's fragile and doesn't handle edge cases (like quoted commas or newlines). It's only suitable for very simple, predictable data.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class CsvToExcelSimple {
    public static void main(String[] args) {
        String csvFile = "path/to/your/input.csv";
        String excelFile = "path/to/your/output.xlsx";
        // Note: This creates a simple XML-based format, not a true XLSX.
        // For a real XLSX, you need a library like POI.
        // This is just to demonstrate the concept without dependencies.
        // A true manual XLSX writer is extremely complex.
        // A better "no library" approach is to create a TSV (Tab-Separated Values)
        // file, which Excel can open easily.
        String tsvFile = "output.tsv";
        try (BufferedReader br = new BufferedReader(new FileReader(csvFile));
             PrintWriter pw = new PrintWriter(tsvFile)) {
            String line;
            while ((line = br.readLine()) != null) {
                // Replace commas with tabs
                String tsvLine = line.replace(",", "\t");
                pw.println(tsvLine);
            }
            System.out.println("TSV file created. You can open it with Excel.");
            // System.out.println("To open in Excel: File -> Open -> select output.tsv");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Recommendation

  • For most projects: Use Apache POI (Method 1). It's the standard, well-documented, and gives you full control over your Excel files.
  • For very large files (>100MB or >100,000 rows): Use the OpenCSV + EasyExcel combination (Method 2). The memory savings will be significant.
  • For a modern, concise codebase: Try the Java 8 Streams approach (Method 3) if your team is comfortable with it.
  • Avoid Method 4 unless you are in a constrained environment with absolutely no external libraries and your data is extremely simple.
分享:
扫描分享到社交APP
上一篇
下一篇