Markdown to Excel Conversion
25 Jun 202610 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 Markdown to an Excel 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 a Markdown file to an Excel document using the .NET Excel Library (XlsIO).
Convert Markdown to Excel document
Convert an existing Markdown file to an Excel document using the .NET Excel (XlsIO) library.
The following code example shows how to convert Markdown to an Excel document.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(@"Data/Sample.md", ExcelOpenType.Markdown);
workbook.SaveAs(Path.GetFullPath("Output/MarkdownToExcel.xlsx"));
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(@"Data/Sample.md", ExcelOpenType.Markdown);
workbook.SaveAs(Path.GetFullPath("Output/MarkdownToExcel.xlsx"));
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.md", ExcelOpenType.Markdown)
workbook.SaveAs("MarkdownToExcel.xlsx")
End UsingYou can download a complete working sample from this GitHub page.
Customize image data
The .NET Excel (XlsIO) library provides an ImageNodeVisited event, which allows you to customize image data while importing a Markdown file. Implement the logic to customize the image data by using this ImageNodeVisited event.
The following code example shows how to load image data based on the image source path when importing a Markdown file.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
MdImportSettings settings = new MdImportSettings();
settings.ImageNodeVisited += MdImportSettings_ImageNodeVisited;
IWorkbook workbook = application.Workbooks.Open(@"Data/Sample1.md", settings);
workbook.SaveAs(Path.GetFullPath("Output/MarkdownToExcel.xlsx"));
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
MdImportSettings settings = new MdImportSettings();
settings.ImageNodeVisited += MdImportSettings_ImageNodeVisited;
IWorkbook workbook = application.Workbooks.Open(@"Data/Sample1.md", settings);
workbook.SaveAs(Path.GetFullPath("Output/MarkdownToExcel.xlsx"));
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim settings As New MdImportSettings()
AddHandler settings.ImageNodeVisited, AddressOf MdImportSettings_ImageNodeVisited
Dim workbook As IWorkbook = application.Workbooks.Open("Sample1.md", settings)
workbook.SaveAs("MarkdownToExcel.xlsx")
End UsingThe following code examples show the event handler to customize the image based on the source path.
private static void MdImportSettings_ImageNodeVisited(object sender, MdImageNodeVisitedEventArgs args)
{
//Set the image stream based on the image name from the input Markdown.
if (args.Uri == "Image_1.png")
args.ImageStream = new FileStream(Path.GetFullPath("Data/Image_1.png"), FileMode.Open);
else if (args.Uri == "Image_2.png")
args.ImageStream = new FileStream(Path.GetFullPath("Data/Image_2.png"), FileMode.Open);
//Retrieve the image from the website and use it.
else if (args.Uri.StartsWith("https://"))
{
WebClient client = new WebClient();
byte[] image = client.DownloadData(args.Uri);
Stream stream = new MemoryStream(image);
args.ImageStream = stream;
}
}private static void MdImportSettings_ImageNodeVisited(object sender, MdImageNodeVisitedEventArgs args)
{
//Set the image stream based on the image name from the input Markdown.
if (args.Uri == "Image_1.png")
args.ImageStream = new FileStream(Path.GetFullPath("Data/Image_1.png"), FileMode.Open);
else if (args.Uri == "Image_2.png")
args.ImageStream = new FileStream(Path.GetFullPath("Data/Image_2.png"), FileMode.Open);
//Retrieve the image from the website and use it.
else if (args.Uri.StartsWith("https://"))
{
WebClient client = new WebClient();
byte[] image = client.DownloadData(args.Uri);
Stream stream = new MemoryStream(image);
args.ImageStream = stream;
}
}Private Sub MdImportSettings_ImageNodeVisited(sender As Object, args As MdImageNodeVisitedEventArgs)
' Set the image stream based on the image name from the input Markdown.
If args.Uri = "Image_1.png" Then
args.ImageStream = New FileStream(Path.GetFullPath("Data/Image_1.png"), FileMode.Open)
ElseIf args.Uri = "Image_2.png" Then
args.ImageStream = New FileStream(Path.GetFullPath("Data/Image_2.png"), FileMode.Open)
ElseIf args.Uri.StartsWith("https://") Then
Dim client As New WebClient()
Dim imageBytes As Byte() = client.DownloadData(args.Uri)
Dim stream As New MemoryStream(imageBytes)
args.ImageStream = stream
End If
End SubYou can download a complete working sample from this GitHub page.
NOTE
Hook the event handler before opening the Markdown document as per the above code example.
Markdown file opening options
Using MarkdownDocument object
The following code examples shows the open method takes parameter as MarkdownDocument by providing file stream.
FileStream fileStream = new FileStream(@"Data/ExcelToMarkdown.md", FileMode.Open, FileAccess.Write);
MarkdownDocument markdownDocument = new MarkdownDocument(fileStream);
IWorkbook workbook = application.Workbooks.Open(markdownDocument);FileStream fileStream = new FileStream(@"Data/ExcelToMarkdown.md", FileMode.Open, FileAccess.Write);
MarkdownDocument markdownDocument = new MarkdownDocument(fileStream);
IWorkbook workbook = application.Workbooks.Open(markdownDocument);Dim fileStream As New FileStream("Data/ExcelToMarkdown.md", FileMode.Open, FileAccess.Write)
Dim markdownDocument As New MarkdownDocument(fileStream)
Dim workbook As IWorkbook = application.Workbooks.Open(markdownDocument)The following code examples shows the open method takes parameter as MarkdownDocument by providing file path directly.
MarkdownDocument markdownDocument = new MarkdownDocument(@"Data/ExcelToMarkdown.md");
IWorkbook workbook = application.Workbooks.Open(markdownDocument);MarkdownDocument markdownDocument = new MarkdownDocument("Data/ExcelToMarkdown.md");
IWorkbook workbook = application.Workbooks.Open(markdownDocument);Dim markdownDocument As New MarkdownDocument("Data/ExcelToMarkdown.md")
Dim workbook As IWorkbook = application.Workbooks.Open(markdownDocument)