跪拜 Guibai
← All articles
Java

Java Library Turns Excel Tables Into Markdown and Back

By 用户372157426135 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Teams that live in Markdown for docs but in Excel for source data can now automate the round trip inside a Java build, eliminating copy-paste and a separate conversion service. The cost is data fidelity: formulas and cell types do not survive, so the workflow suits report and documentation output rather than analytical pipelines.

Summary

Spire.XLS for Java now spans both directions of the Excel-Markdown gap. `Workbook.loadFromFile()` followed by `saveToFile(..., FileFormat.Markdown)` exports a workbook as a Markdown table in a couple of lines, and `loadFromMarkdown()` imports a Markdown document back to XLSX. The library also exposes `MarkdownOptions` so exported images can use relative paths and hyperlinks can take reference-style syntax, which matters when the output is headed for GitHub or a static doc site.

The import side needs attention. Markdown carries no cell type information, so numbers and dates may come back as text; `setNumberValue()` keeps them calculable. Formulas, merged cells, conditional formatting, and multi-level headers have no Markdown equivalent, so a workbook needs to be cleaned into a regular data region before conversion.

For documents holding several tables, `loadFromMarkdown()` converts the whole file. A separate parsing routine built on the Java standard library reads the file line by line, skips fenced code blocks, matches header and separator rows, and writes each table to its own worksheet with bold headers and auto-fitted columns. The routine only targets simple pipe tables; complex escapes or pipes inside inline code require a full Markdown parser.

Takeaways
— Excel-to-Markdown conversion support starts at Spire.XLS for Java 15.11.3; the examples use 16.10.1.
— A workbook exports to Markdown with `saveToFile(file, FileFormat.Markdown)` after loading via `loadFromFile()`.
— `MarkdownOptions.isSavePicInRelativePath(true)` makes image references relative; `isSaveHyperlinkAsRef(true)` switches hyperlinks to reference-style.
— `Workbook.loadFromMarkdown()` imports a Markdown file; `saveToFile(file, ExcelVersion.Version2016)` writes it to XLSX.
— Markdown does not preserve formulas, merged cells, conditional formatting, or complex styles; clean the workbook into a regular data region first.
— Imported numbers can arrive as text because Markdown lacks cell types; use `setNumberValue()` for numeric fields.
— The multi-table extractor identifies tables by header/separator rows, skips fenced code blocks, handles escaped pipes, and creates one worksheet per table with bold headers and auto-fitted columns.
— The extraction routine only supports simple tables with leading and trailing pipes; complex escapes or inline-code pipes need a dedicated Markdown parser.
Conclusions

Whole-file conversion is a two-line call, but the lossy part is Markdown's flat text model; anyone expecting a clean round trip has to reapply data types after import.

Because `loadFromMarkdown()` pulls in an entire file, selective table extraction leaves you writing a custom parser; the example's hand-rolled code only handles simple pipe tables.

The `MarkdownOptions` for relative image paths and reference links are aimed at repository-hosted docs, so the likely home for this feature is README and static-site publishing, not interactive spreadsheet workflows.

Concepts & terms
Spire.XLS for Java
A Java API for reading, editing, and converting Excel files without Microsoft Office; since 15.11.3 it supports Markdown as an input and output format.
Reference-style Markdown link
A Markdown link syntax that separates the display text from the URL by declaring a reference, e.g. `[text][ref]` followed by `[ref]: https://example.com`, instead of an inline `[text](url)`.
Markdown table separator row
The second row of a Markdown table, built from hyphens and colons to set column alignment; the example detects it with the pattern `:?-+:?`.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗