Quantcast
Channel: Developer Express Inc.
Viewing all articles
Browse latest Browse all 2370

.NET Spreadsheet (WinForms, WPF, Office File API) – Export and Copy Cell Ranges as Images (v21.1)

$
0
0

Our Spreadsheet Document API v21.1 allows you to save a cell range as an image. The following image formats are supported:

  • PNG
  • JPEG
  • BMP
  • TIFF
  • GIF
  • EMF (not available for Linux and macOS)
  • WMF (not available for Linux and macOS)

Note: You need an active license for the DevExpress Office File API Subscription or the DevExpress Universal Subscription to use this API in production code. If you’d like to purchase the Office File API or if you wish to upgrade your current subscription to DevExpress Universal, write to us at clientservices@devexpress.com.

Getting Started

To use this option and create an image from worksheet cells, call the CellRangeExtensions.ExportToImage extension method for a CellRange object. This method allows you to save the output image as a file or write it to a stream.

You can customize export settings as needed. To customize settings, create a RangeImageOptions class instance, pass it to the ExportToImage method, and specify the following options:

  • Define image resolution (in DPI)
  • Scale worksheet content before export
  • Set image background color
  • Create a black and white image
  • Export cell gridlines and specify gridline color
  • Display row and column headings
  • Export drawing objects (charts, shapes, and pictures) located in the exported range

The code below defines export settings and exports a given cell range to an image.

using DevExpress.Spreadsheet;
using System.Drawing;
// ...

using (Workbook workbook = new Workbook())
{
    workbook.LoadDocument("Document.xlsx", DocumentFormat.Xlsx);
    Worksheet worksheet = workbook.Worksheets.ActiveWorksheet;
    var exportOptions = new RangeImageOptions
    {
        Resolution = 192,
        ExportHeadings = true,
        ExportGridlines = true,
        GridlineColor = Color.Gray,
        BackgroundColor = Color.FromArgb(0xF2, 0xF2, 0xF2)
    };
    worksheet.Range["B1:I25"].ExportToImage("RangeImage.png", 
    	ImageFileFormat.Png, exportOptions);
}

Copy a Spreadsheet Cell Range as an Image

As you may already know, our Spreadsheet Document API and Spreadsheet UI components (for both WinForms and WPF) can copy cells to the clipboard using different file formats: XLS, XLSB, tab-delimited text, CSV, and HTML. In v21.1, we added support for bitmap images. You can now copy a cell range and paste it as an image into another application.

You can also use the IClipboardManager.Copy method to programmatically copy selected cells to the clipboard from the Spreadsheet control or a non-visual Workbook instance.

// Access active Spreadsheet worksheet.
var activeSheet = spreadsheetControl1.ActiveWorksheet;
// Specify cell selection.
spreadsheetControl1.Selection = activeSheet.Range["B1:H21"];
// Copy selected cells to the clipboard.
spreadsheetControl1.Clipboard.Copy();

Tell Us What You Think

As always, we welcome your feedback. Please feel free to leave comments below or contact us via the DevExpress Support Center.


Viewing all articles
Browse latest Browse all 2370

Trending Articles