How can I help you?
Excel to Markdown Conversion
17 Jun 202612 minutes to read
Markdown is a lightweight markup language that adds formatting elements to plain text documents. The .NET Excel (XlsIO) library supports the conversion of Excel to Markdown document and vice versa, which mostly follows the CommonMark specification and GitHub-flavored syntax.
Assemblies and NuGet packages required
Refer to the following links for assemblies and NuGet packages required based on platforms to convert an Excel document to Markdown using the .NET Excel Library (XlsIO).
Convert Excel to Markdown document
Convert an existing Excel file to Markdown using the .NET Excel (XlsIO) library.
The following code example shows how to convert an Excel document to a Markdown file.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(@"Data/Markdown.xlsx");
using (FileStream fileStream = new FileStream(@"Output/ExcelToMarkdown.md", FileMode.Create, FileAccess.Write))
{
workbook.SaveAs(fileStream, ExcelSaveType.Markdown);
}
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(@"Data/Markdown.xlsx");
workbook.SaveAs("ExcelToMarkdown.md");
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Markdown.xlsx")
Using fileStream As New FileStream("ExcelToMarkdown.md", FileMode.Create, FileAccess.Write)
workbook.SaveAs(fileStream, ExcelSaveType.Markdown)
End Using
End UsingYou can download a complete working sample from this GitHub page.
Customize image saving
Customize the image path
XlsIO provides an ImageNodeVisited event, which is used to customize the image path in the output Markdown file and save images externally while converting an Excel document to a Markdown file.
The following code example illustrates how to save image files during Excel to Markdown Conversion.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
IWorkbook workbook = application.Workbooks.Open(@"Data/Markdown.xlsx");
MarkdownExportOptions exportOptions = new MarkdownExportOptions();
exportOptions.SaveOptions.ImageNodeVisited += MdExportSettings_ImageNodeVisited;
using (FileStream fileStream = new FileStream(@"ExcelToMarkdown.md", FileMode.Create, FileAccess.Write))
{
workbook.SaveAs(fileStream, exportOptions);
}
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
IWorkbook workbook = application.Workbooks.Open(@"Data/Markdown.xlsx");
MarkdownExportOptions exportOptions = new MarkdownExportOptions();
exportOptions.SaveOptions.ImageNodeVisited += MdExportSettings_ImageNodeVisited;
workbook.SaveAs("ExcelToMarkdown.md", exportOptions);
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
Dim workbook As IWorkbook = application.Workbooks.Open("Markdown.xlsx")
' Configure Markdown export options
Dim exportOptions As New MarkdownExportOptions()
AddHandler exportOptions.SaveOptions.ImageNodeVisited, AddressOf MdExportSettings_ImageNodeVisited
' Save as Markdown file
Using fileStream As New FileStream("Output.md", FileMode.Create, FileAccess.Write)
workbook.SaveAs(fileStream, exportOptions)
End Using
End UsingThe following code examples show the event handler to customize the image path and save the image in an external folder.
private static void MdExportSettings_ImageNodeVisited(object sender, SaveImageNodeVisitedEventArgs args)
{
string imagepath = @"D:\Temp\Image1.png";
//Save the image stream as a file.
using (FileStream fileStreamOutput = File.Create(imagepath))
args.ImageStream.CopyTo(fileStreamOutput);
//Set the image URI to be used in the output markdown.
args.Uri = imagepath;
}private static void MdExportSettings_ImageNodeVisited(object sender, SaveImageNodeVisitedEventArgs args)
{
string imagepath = @"D:\Temp\Image1.png";
//Save the image stream as a file.
using (FileStream fileStreamOutput = File.Create(imagepath))
args.ImageStream.CopyTo(fileStreamOutput);
//Set the image URI to be used in the output markdown.
args.Uri = imagepath;
}Private Sub MdExportSettings_ImageNodeVisited(sender As Object, args As SaveImageNodeVisitedEventArgs)
Dim imagePath As String = "D:\Temp\Image1.png"
' Save the image stream as a file
Using fileStreamOutput As FileStream = File.Create(imagePath)
args.ImageStream.CopyTo(fileStreamOutput)
End Using
' Set the image URI to be used in the output markdown
args.Uri = imagePath
End SubYou can download a complete working sample from this GitHub page.
Markdown Export Options
PreserveEmptyRow
This property ensures that blank rows in the Excel worksheet are retained in the Markdown output, preserving layout and spacing.
// Preserve empty rows during export
MarkdownExportOptions exportOptions = new MarkdownExportOptions();
exportOptions.PreserveEmptyRow = true;// Preserve empty rows during export
MarkdownExportOptions exportOptions = new MarkdownExportOptions();
exportOptions.PreserveEmptyRow = true;' Preserve empty rows during export
Dim exportOptions As New MarkdownExportOptions()
exportOptions.PreserveEmptyRow = TrueUseDisplayText
This property exports the formatted display text of cells (e.g., dates or formatted numbers) instead of raw values, making the Markdown output more user‑friendly.
// Export formatted display text
MarkdownExportOptions exportOptions = new MarkdownExportOptions();
exportOptions.UseDisplayText = true;// Export formatted display text
MarkdownExportOptions exportOptions = new MarkdownExportOptions();
exportOptions.UseDisplayText = true;' Export formatted display text
Dim exportOptions As New MarkdownExportOptions()
exportOptions.UseDisplayText = TrueMarkdown save options
The following code examples shows how to save the sheet as markdown file by providing file path directly.
IWorksheet worksheet = workbook.Worksheets[0];
MarkdownExportOptions exportOptions = new MarkdownExportOptions();
exportOptions.UseDisplayText = true;
worksheet.SaveAs("Output/ExcelToMarkdown.md", exportOptions);IWorksheet worksheet = workbook.Worksheets[0];
MarkdownExportOptions exportOptions = new MarkdownExportOptions();
exportOptions.UseDisplayText = true;
worksheet.SaveAs("Output/ExcelToMarkdown.md", exportOptions);Dim worksheet As IWorksheet = workbook.Worksheets(0)
Dim exportOptions As New MarkdownExportOptions()
exportOptions.UseDisplayText = True
worksheet.SaveAs("Output/ExcelToMarkdown.md", exportOptions)The following code examples shows how to save the sheet as markdown file by providing file stream.
IWorksheet worksheet = workbook.Worksheets[0];
MarkdownExportOptions exportOptions = new MarkdownExportOptions();
exportOptions.UseDisplayText = true;
FileStream fileStream = new FileStream("Output/ExcelToMarkdown.md", FileMode.Create, FileAccess.Write);
worksheet.SaveAs(fileStream, exportOptions);IWorksheet worksheet = workbook.Worksheets[0];
MarkdownExportOptions exportOptions = new MarkdownExportOptions();
exportOptions.UseDisplayText = true;
FileStream fileStream = new FileStream("Output/ExcelToMarkdown.md", FileMode.Create, FileAccess.Write);
worksheet.SaveAs(fileStream, exportOptions);Dim worksheet As IWorksheet = workbook.Worksheets(0)
Dim exportOptions As New MarkdownExportOptions()
exportOptions.UseDisplayText = True
Dim fileStream As New FileStream("Output/ExcelToMarkdown.md", FileMode.Create, FileAccess.Write)
worksheet.SaveAs(fileStream, exportOptions)Get MarkdownDocument object
The following code examples shows how to get the MarkdownDocument object from the worksheet using GetMarkdownDocument method.
IWorkbook workbook = application.Workbooks.Open("Data/Markdown.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
MarkdownDocument markdownDocument = worksheet.GetMarkdownDocument();IWorkbook workbook = application.Workbooks.Open("Data/Markdown.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
MarkdownDocument markdownDocument = worksheet.GetMarkdownDocument();Dim workbook As IWorkbook = application.Workbooks.Open("Data/Markdown.xlsx")
Dim worksheet As IWorksheet = workbook.Worksheets(0)
Dim markdownDocument As MarkdownDocument = worksheet.GetMarkdownDocument()The following code examples shows how to get the MarkdownDocument object from the workbook using GetMarkdownDocument method.
IWorkbook workbook = application.Workbooks.Open("Data/Markdown.xlsx");
MarkdownDocument markdownDocument = workbook.GetMarkdownDocument();IWorkbook workbook = application.Workbooks.Open("Data/Markdown.xlsx");
MarkdownDocument markdownDocument = workbook.GetMarkdownDocument();Dim workbook As IWorkbook = application.Workbooks.Open("Data/Markdown.xlsx")
Dim markdownDocument As MarkdownDocument = workbook.GetMarkdownDocument()