Spreadsheet formats & CSV
How To Convert Excel To CSV In Java?
Export an Excel worksheet to CSV in Java with Apache POI, preserving column positions, quoted fields, formula results and an explicit encoding.
To convert an Excel workbook to CSV in Java, read the workbook with a spreadsheet library such as Apache POI, select a worksheet, and write its cells with CSV escaping. This is a file-format conversion. It does not involve transliteration, translation, or automating the Excel interface. A workbook can contain many sheets; a CSV file represents one rectangular table. Decide which sheet and columns the receiving system expects before you export.
Choose the exported representation
There are two useful contracts. A report export preserves displayed strings such as 00123, 12.50% and a formatted date. A machine-data export preserves typed values with an agreed decimal separator and date format. Mixing the contracts can lose information: a formatted percentage is not the same field value as the underlying decimal, and a formatted identifier should not be reinterpreted as an integer. The example below deliberately exports displayed strings.
Add Apache POI's spreadsheet support to your Java project, including poi-ooxml for XLSX. Choose and lock a supported release using the project's dependency policy. The required imports are org.apache.poi.ss.usermodel.*, java.io.*, java.nio.file.*, java.nio.charset.StandardCharsets, and java.util.Locale. The snippet belongs inside a method that handles or declares its I/O exceptions. It is an illustrative conversion core, not a complete command-line application.
Export one sheet with quoted fields
try (Workbook wb = WorkbookFactory.create(new File("input.xlsx"));
BufferedWriter out = Files.newBufferedWriter(
Path.of("output.csv"), StandardCharsets.UTF_8)) {
Sheet sheet = wb.getSheetAt(0);
DataFormatter format = new DataFormatter(Locale.ROOT);
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
int columns = 3; // Defined by the export contract, not by one sparse row.
for (int r = 0; r <= sheet.getLastRowNum(); r++) {
Row row = sheet.getRow(r);
for (int c = 0; c < columns; c++) {
if (c > 0) out.write(',');
Cell cell = row == null ? null : row.getCell(c);
String value = format.formatCellValue(cell, evaluator);
out.write("\"" + value.replace("\"", "\"\"") + "\"");
}
out.write("\r\n");
}
}Every field is enclosed in double quotes. An embedded quote is doubled, so the value He said "yes" becomes "He said ""yes""". Quoting all fields is valid CSV and keeps commas and embedded line breaks inside their fields. It also makes empty cells explicit. The loop uses numeric column positions so a missing middle cell does not shift later values to the left. Set columns from your documented schema; do not infer it from an arbitrary row.
Use a small verification workbook
Create three columns named id, name and note. Add an identifier stored as text, 00123; a name containing a comma, Lee, Ana; and a note containing quotes. Add another row with an empty middle cell. The first exported data record should parse back into exactly three strings, including the unchanged five-character identifier. Inspect the file with a CSV parser before reopening it in Excel, because Excel's automatic import can change values that were correctly exported.
Add a formula, a non-ASCII name and a date to a second test sheet. Document which locale and date representation you need. The formatter/evaluator combination requests a displayed formula result; unsupported functions, external workbook references or missing dependencies require an explicit policy. Do not silently convert an evaluation error to an empty field. A cached result can also be stale if its workbook was not recalculated after source values changed.
Limits that require a separate decision
CSV does not retain formulas, styles, charts, merged cells, comments or worksheet tabs. For multiple sheets, produce separate named files and keep a manifest mapping each file to its sheet. Very large XLSX files may require a streaming reader rather than loading the complete workbook into memory. Start with a bounded file, measure memory, and handle corrupt or encrypted workbooks as errors. Keep the original workbook until the exported data is independently checked.
If other people will open the CSV in spreadsheet software, consider formula-injection handling for untrusted strings beginning with characters such as =. CSV quoting alone does not guarantee that a spreadsheet treats the value as inert text. Apply a receiving-system policy and record any intentional transformation; do not silently alter legitimate identifiers or formulas in a data interchange pipeline.
When the browser text tool helps
Convert Text to CSV can quote and separate already extracted plain text. It does not read an XLSX workbook, evaluate formulas or replace Apache POI. Use it only after the spreadsheet data has become plain text and confirm that its delimiter settings match the destination.


