Convert Sparkline to Image and HTML in Aspose.Cells for Go via C++
Cell.EmbeddedImage property used in this article is available in Aspose.Cells 26.5 and later.
Introduction
Sparklines are a compact way to visualize trends directly inside a worksheet. While Excel users see them in place, many real-world scenarios require a sparkline to leave the cell — for example, to be embedded into a different cell as a static picture, attached to an automated email, or rendered as part of an HTML report published to the web.
Aspose.Cells supports both of these operations. The Sparkline.ToImage method renders an individual sparkline to a Vector<uint8_t> byte array, and the resulting bytes can be assigned to Cell.EmbeddedImage so the picture is stored inside a single cell of the workbook. Separately, HtmlSaveOptions lets you convert the entire workbook — sparklines and all — into a self-contained HTML file. This article walks through both workflows end to end.
Workflow 1 — Render Sparklines to Images and Embed Them into Cells
In this workflow you will build a worksheet that contains a small range of source values, attach three different sparkline groups (Line, Column, and Stacked/Win-Loss) to that range, render each group as a PNG, and write those PNG bytes into adjacent cells as embedded images. The final result is a single .xlsx file that contains both the live sparklines and their rendered picture counterparts.
Step-by-Step Instructions
- Define a working directory and ensure it exists on disk.
- Create a new
Workbookand obtain a reference to the firstWorksheet. - Populate cells
A1throughE1with five sample numeric values (for example, daily sales or temperature readings). - Add three
SparklineGroupobjects to the worksheet by callingworksheet.SparklineGroups.Add(...):- A
SparklineType.Linegroup anchored atF1, with data rangeA1:E1. - A
SparklineType.Columngroup anchored atG1, with data rangeA1:E1. - A
SparklineType.Stacked(win/loss) group anchored atH1, with data rangeA1:E1.
- A
- Build an
ImageOrPrintOptionsinstance and set itsImageTypetoImageType.Pngso each sparkline is rendered as a transparent PNG. - Save the workbook as
output_with_sparklines.xlsx.
The code above produces a workbook where each visual representation of a sparkline is duplicated in two forms: the live, native sparkline anchored at row 1, and a static PNG picture embedded directly into a neighboring cell on row 2. Because the pictures live inside the file itself, the workbook remains a single self-contained artifact that can be emailed or archived without breaking the embedded image references. Render each sparkline group as a PNG — Sparkline.ToImage(ImageOrPrintOptions) returns the picture bytes directly as a Vector<uint8_t> — and assign the array to the EmbeddedImage property of the target cell — the assignment is what makes the picture part of the cell’s stored contents.
Because each sparkline group is anchored to a single cell, you can address it through the indexer group.Sparklines[0] instead of enumerating with foreach. This keeps the rendering code short and matches the typical “one sparkline per anchor cell” pattern. Storing the picture bytes via Cell.EmbeddedImage requires Aspose.Cells 26.5 or later.
Workflow 2 — Export the Sparkline Worksheet to HTML
Once the workbook contains live sparklines (and optionally embedded picture counterparts), the entire worksheet can be published to the web by saving it as HTML. The HtmlSaveOptions class exposes the knobs you need to control this export; in this workflow you will reuse the output_with_sparklines.xlsx file produced by Workflow 1 and convert it to a clean, single-page HTML document.
Step-by-Step Instructions
- Ensure the
output_with_sparklines.xlsxfile produced by Workflow 1 is available on disk in your working directory. - Load that file into a new
Workbookinstance. - Instantiate
HtmlSaveOptionsand set itsExportActiveWorksheetOnlyproperty totrueso the resulting HTML file contains only the active worksheet rather than the entire workbook. - Call
workbook.Save("sparklines.html", htmlOptions)to write the HTML output to disk.
The code above takes the sparkline-rich workbook from Workflow 1 and turns it into a portable HTML file. Sparklines are preserved as inline SVG or PNG renderings inside the generated HTML, depending on the export mode, so end users can view the trends in any modern browser without needing Excel installed. By setting ExportActiveWorksheetOnly to true, you avoid accidentally publishing hidden sheets or auxiliary data — only the worksheet currently visible to the user is exported.
The HtmlSaveOptions class offers additional properties for fine-tuning the output, such as ExportHiddenWorksheet, ExportImagesAsBase64, and Encoding. Adjust these as needed for your deployment target.
API Summary
The workflows above rely on a small set of Aspose.Cells APIs working together.
SparklineGroupand the collection accessorworksheet.SparklineGroupsare used to declare the type (Line, Column, Stacked), the data range, and the anchor cell for each sparkline group. In this article each group is anchored to a single cell, so the group is reached throughworksheet.SparklineGroups[i].Sparklineand the indexergroup.Sparklines[0]return the individual sparkline inside a group. Because every group in the example contains exactly one sparkline, noforeachloop is required.Sparkline.ToImage(ImageOrPrintOptions)is the rendering method that returns a picture of the sparkline directly as aVector<uint8_t>byte array.HtmlSaveOptions.ExportActiveWorksheetOnly(abool) restricts HTML export to the active worksheet. It is one of the most commonly used properties onHtmlSaveOptionswhen generating single-page reports.ImageOrPrintOptions.ImageTypelives in theAspose.Cells.Drawingnamespace and selects the picture format (for example,ImageType.Png) used when rendering withToImageand when printing worksheets to images.