← Back to list

How We can Convert CSV to Excel Using Java — Simple and Practical Way

A quick and practical way to automate CSV to Excel conversion using Java, Apache POI, and OpenCSV.

S. Sandeep · 2025-11-09 10:35 · 0 claps · 2.3 min read
#java #automation #prodcutivity #apache-poi #opencsv
Open on Medium ↗

How We can Convert CSV to Excel Using Java — Simple and Practical Way

Recently, I came across a small but boring task — converting CSV files into Excel. It sounds simple, right? But when you have to do it regularly or integrate it inside automation or CI/CD, manual conversion becomes painful.

So I thought — let’s just automate it using Java.

This blog is not theory-heavy — it’s exactly how I implemented it in a real scenario, step by step.

💡 Why We Need This

In automation work, we often export test data or reports in CSV format. But users prefer Excel because:

  • It’s easier to filter, style, and share.
  • It can be attached in email reports or dashboards.
  • It looks clean.

Instead of manually opening and saving as .xlsx, We can write a simple Java utility that does it in seconds.

⚙️ What We Use

We keep it minimal — just what’s needed:

  • Java 8+
  • Apache POI → for Excel creation
  • OpenCSV → for reading CSV easily
  • Maven → to manage dependencies

Dependencies (pom.xml)

<dependencies>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.5</version>
    </dependency>
    <dependency>
        <groupId>com.opencsv</groupId>
        <artifactId>opencsv</artifactId>
        <version>5.9</version>
    </dependency>
</dependencies>

🧩 Step 1 — Reading CSV

First, I created a small helper method using OpenCSV to read the CSV file.

import com.opencsv.CSVReader;
import java.io.FileReader;
import java.util.List;

public class CsvReader {
    public static List<String[]> readCsv(String filePath) throws Exception {
        try (CSVReader reader = new CSVReader(new FileReader(filePath))) {
            return reader.readAll();
        }
    }
}

This gives me a list of all rows, where each row is a string array — very easy to loop over later.

🧱 Step 2 — Writing Excel File

Now comes the main part — converting that list into an Excel sheet using Apache POI.

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.util.List;

public class CsvToExcelConverter {

    public static void convertCsvToExcel(List<String[]> data, String outputPath) throws Exception {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Data");

        int rowCount = 0;
        for (String[] rowData : data) {
            Row row = sheet.createRow(rowCount++);
            for (int i = 0; i < rowData.length; i++) {
                Cell cell = row.createCell(i);
                cell.setCellValue(rowData[i]);
            }
        }

        // Auto-size columns for better visibility
        for (int i = 0; i < data.get(0).length; i++) {
            sheet.autoSizeColumn(i);
        }

        try (FileOutputStream outputStream = new FileOutputStream(outputPath)) {
            workbook.write(outputStream);
        }

        workbook.close();
        System.out.println("Excel file created successfully at: " + outputPath);
    }
}

At this point, even with just a few lines, the conversion worked perfectly.

⚙️ Step 3 — Bringing It Together

Here’s the main class that connects everything.

import java.util.List;

public class CsvToExcelMain {
    public static void main(String[] args) {
        try {
            String csvFile = "C:/files/input.csv";
            String excelFile = "C:/files/output.xlsx";

            List<String[]> data = CsvReader.readCsv(csvFile);
            CsvToExcelConverter.convertCsvToExcel(data, excelFile);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Now I can just run this utility and get a clean .xlsx file every time.

🎨 Step 4 — Small Enhancement: Add Styling

After the first run, I realized Excel looked too plain. So I added a simple header style — bold and neat.

if (rowCount == 0) {
    CellStyle headerStyle = workbook.createCellStyle();
    Font font = workbook.createFont();
    font.setBold(true);
    headerStyle.setFont(font);
    for (Cell cell : row) {
        cell.setCellStyle(headerStyle);
    }
}

That’s enough to make reports look professional without overcomplicating things.

🧠 Final Thoughts

This started as a small idea, but now this utility is part of my automation toolkit. We even can plug this into Jenkins pipelines to generate Excel reports automatically after test runs.

If you work in testing or automation or devops, small scripts like this save a lot of time. It’s not just about code — it’s about automating what slows you down.

✍️ If you found this helpful, follow me on Medium for more automation tips!


메타데이터
post_id
9d53bd7b0a37
slug
how-we-can-convert-csv-to-excel-using-java-simple-and-practical-way-9d53bd7b0a37
url
https://medium.com/@sandeep031/how-we-can-convert-csv-to-excel-using-java-simple-and-practical-way-9d53bd7b0a37
canonical_url
https://medium.com/@sandeep031/how-we-can-convert-csv-to-excel-using-java-simple-and-practical-way-9d53bd7b0a37
author_url
https://medium.com/@sandeep031
status
ok
fetched_at
2026-07-15 06:39:36