In Java data pipelines, converting between Excel and CSV comes up often, but the two formats are designed for different purposes. Understanding where each fits matters more than the conversion code itself, because the code is only a few lines while picking the wrong format tends to surface problems after the data has already been delivered.
What each format is actually for
CSV is plain text. Values are organized by a delimiter, usually a comma, and the format carries no type, style, or structural information. Its strength is universality: any text editor opens it, any language parses it, Git can diff it line by line, and database import tools accept it natively. The cost is that it represents exactly one table, and every value is a string.
An .xlsx file is a binary container that holds styles. It can store formulas, multiple worksheets, merged cells, charts, and conditional formatting, and cells carry explicit types such as number, date, or text. The cost is that it does not diff well, is awkward for non-Office tools to parse directly, and has a complex internal structure.
A common misjudgment is to treat Excel as the "more advanced" format and therefore the better archive. In practice CSV is usually the stronger choice for long-term readability and machine processing, while Excel's advantage is human review and interactive analysis. When data is headed for another system to consume automatically, CSV's flat structure is often more reliable than Excel's richer one.
When conversion is needed
Excel to CSV typically appears when a table maintained by business users needs to be loaded into a database or warehouse, when a report containing formulas must be exported as pure data for another system, or when a multi-sheet workbook has to be split into separate files for batch processing.
CSV to Excel appears when raw system output needs to be packaged as a formatted report for human review, when Excel's filtering, sorting, and conditional formatting are needed on top of CSV data, or when several CSV files are combined into a single multi-sheet workbook for distribution.
Neither direction is cosmetic. Both are a change of form as data moves between different consumers.
How to convert Excel to CSV and vice versa
Both directions use the same Workbook object as the entry point: load the source file, then save it in the target format, with the delimiter and encoding specified at the appropriate step. The two subsections below cover each direction.
Excel to CSV
CSV holds one table, so a specific worksheet must be chosen. If the source contains formulas, calling calculateAllValue() ensures the export receives evaluated results rather than formula strings.
import com.spire.xls.*;
import java.nio.charset.Charset;
Workbook workbook = new Workbook();
workbook.loadFromFile("input.xlsx");
workbook.calculateAllValue();
Worksheet sheet = workbook.getWorksheets().get(0);
sheet.saveToFile("output.csv", ",", Charset.forName("UTF-8"));
workbook.dispose();
Multiple worksheets require a per-sheet export:
for (int i = 0; i < workbook.getWorksheets().getCount(); i++) {
Worksheet ws = workbook.getWorksheets().get(i);
ws.saveToFile("output_" + i + ".csv", ",", Charset.forName("UTF-8"));
}
CSV to Excel
The basic conversion is two lines. Opening the result in Excel, however, often shows green triangle warnings on numeric columns. These are Excel's "number stored as text" indicators, and they appear because CSV cannot tell the library whether a value like 00123 is a number or an identifier with leading zeros.
Workbook workbook = new Workbook();
workbook.loadFromFile("data.csv", ",");
workbook.saveToFile("output.xlsx", ExcelVersion.Version2013);
workbook.dispose();
To suppress the warning and auto-fit the sheet:
import com.spire.xls.*;
import java.util.EnumSet;
Workbook workbook = new Workbook();
workbook.loadFromFile("data.csv", ",", 1, 1);
Worksheet sheet = workbook.getWorksheets().get(0);
sheet.getCellRange("A1:D6")
.setIgnoreErrorOptions(EnumSet.of(IgnoreErrorType.NumberAsText));
sheet.getAllocatedRange().autoFitColumns();
sheet.getAllocatedRange().autoFitRows();
workbook.saveToFile("output.xlsx", ExcelVersion.Version2013);
workbook.dispose();
setIgnoreErrorOptions only suppresses the warning marker; it does not change the cell's actual type. 007 remains text with its leading zeros intact. That is the right behavior for identifier columns and the wrong one for columns that a downstream formula needs to treat as numeric.
What to watch for
Encoding. CSV has no metadata describing its encoding, so UTF-8 is the safe default. If the source uses GBK or ISO-8859-1, the load must match it or non-ASCII characters turn into question marks. In the other direction, specifying UTF-8 explicitly on export avoids depending on the JVM's default, which varies by platform and locale.
Formulas versus values. Whether to call calculateAllValue() depends on what the downstream consumer needs. Database imports usually want evaluated values, while a diff tool comparing two versions of a model may want the formula text preserved.
Multiple worksheets. CSV has no concept of a sheet. A five-sheet workbook becomes five CSV files, and there is no lossless way to compress them into one without losing the structure.
Type ambiguity. When importing CSV into Excel, the library cannot tell whether 00123 is a number or a code. Leaving it alone produces warnings; forcing the type can drop leading zeros. There is no universally correct answer, only one that matches the column's real meaning.
Conclusion
Converting between Excel and CSV is simple at the code level: load, select a sheet, save. The judgment lies in the format choice itself. CSV suits machine consumption and long-term archiving; Excel suits human review and interactive analysis. The conversion code solves format compatibility, but it cannot recover the data semantics that plain text drops along the way. Understanding that distinction matters more than memorizing the arguments to saveToFile.
Top comments (0)