ワークシートを画像に変換 データ周りの余白を削除する
Contents
[
Hide
]
時には、ワークシートの画像をアプリケーションやWebページで表示する必要があります。たとえば、画像をWord文書、PDFファイル、PowerPointプレゼンテーション、あるいは他のドキュメントに挿入する必要があるかもしれません。基本的に、ワークシートを画像としてレンダリングして、他のアプリケーションに貼り付けられるようにしたいと思うでしょう。Aspose.Cellsを使用すると、Microsoft Excelのワークシートを画像に変換することができます。
データ周りの余白を削除してください
Aspose.Cells.Rendering.SheetRenderAPIは、ワークシートを指定された属性(たとえば、画像形式、ページ化されたシートなど)で画像ファイルに変換します。いくつかの画像形式がサポートされており、BMP、GIF、JPG、TIFF、EMFなどが含まれています。
シートを画像化する際、出力画像にはデフォルトで余白(ボーダー)があります。これを削除するには、元のワークシートの上部、下部、左側、右側のページ設定のマージンを0に設定し、それに応じてAspose.Cells.Rendering.ImageOrPrintOptions属性を指定してください。
次のコードスニペットは、出力画像のデータ周りの余白を削除します。
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(); | |
// Open the template file | |
Workbook book = new Workbook(sourceDir + "Book1.xlsx"); | |
// Get the first worksheet | |
Worksheet sheet = book.Worksheets[0]; | |
LoadOptions options = new LoadOptions(); | |
options.LoadFilter = new LoadFilter(LoadDataFilterOptions.All); | |
// Specify your print area if you want | |
// Sheet.PageSetup.PrintArea = "A1:H8"; | |
// To remove the white border around the image. | |
sheet.PageSetup.LeftMargin = 0; | |
sheet.PageSetup.RightMargin = 0; | |
sheet.PageSetup.BottomMargin = 0; | |
sheet.PageSetup.TopMargin = 0; | |
// Define ImageOrPrintOptions | |
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions(); | |
imgOptions.ImageType = Drawing.ImageType.Emf; | |
// Set only one page would be rendered for the image | |
imgOptions.OnePagePerSheet = true; | |
imgOptions.PrintingPage = PrintingPageType.IgnoreBlank; | |
// Create the SheetRender object based on the sheet with its | |
// ImageOrPrintOptions attributes | |
SheetRender sr = new SheetRender(sheet, imgOptions); | |
// Convert the image | |
sr.ToImage(0, outputDir + "outputRemoveWhitespaceAroundData.emf"); |