Export Range of Cells in a Worksheet to Image
Possible Usage Scenarios
You can make an image of a worksheet using Aspose.Cells. However, sometimes you need to export only a range of cells in a worksheet to an image. This article explains how to achieve this.
Export Range of Cells in a Worksheet to Image
To take an image of a range, set the print area to the desired range and then set all margins to 0. Also set ImageOrPrintOptions.OnePagePerSheet to true. The following code takes an image of the range D8:G16. Below is a screenshot of the sample Excel file used in the code. You can try the code with any Excel file.
Screenshot of Sample Excel File and its Exported Image
Executing the code creates an image of the range D8:G16 only.
Sample Code
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Create workbook from source file. | |
Workbook workbook = new Workbook(sourceDir + "sampleExportRangeOfCellsInWorksheetToImage.xlsx"); | |
// Access the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Set the print area with your desired range | |
worksheet.PageSetup.PrintArea = "D8:G16"; | |
// Set all margins as 0 | |
worksheet.PageSetup.LeftMargin = 0; | |
worksheet.PageSetup.RightMargin = 0; | |
worksheet.PageSetup.TopMargin = 0; | |
worksheet.PageSetup.BottomMargin = 0; | |
// Set OnePagePerSheet option as true | |
ImageOrPrintOptions options = new ImageOrPrintOptions(); | |
options.OnePagePerSheet = true; | |
options.ImageType = ImageType.Jpeg; | |
options.HorizontalResolution = 200; | |
options.VerticalResolution = 200; | |
// Take the image of your worksheet | |
SheetRender sr = new SheetRender(worksheet, options); | |
sr.ToImage(0, outputDir + "outputExportRangeOfCellsInWorksheetToImage.jpg"); |