ワークシート内のセルの範囲をイメージにエクスポート
Contents
[
Hide
]
可能な使用シナリオ
Aspose.Cellsを使用してワークシートのイメージを作成できます。ただし、ワークシート内のセルの範囲をイメージにエクスポートする必要がある場合があります。この記事では、これをどのように行うかについて説明します。
ワークシートのセルの範囲をイメージにエクスポート
範囲のイメージを取得するには、印刷範囲を所定の範囲に設定し、すべての余白を0に設定し、さらにImageOrPrintOptions.OnePagePerSheet を trueに設定します。次のコードは、範囲D8:G16のイメージを取得します。下のスクリーンショットは、コードで使用されるサンプルExcelファイルのイメージです。任意のExcelファイルでコードを試すことができます。
サンプルExcelファイルのスクリーンショットとそのエクスポートされたイメージ
コードを実行すると、範囲D8:G16のイメージが作成されます。
サンプルコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |