Görüntü veya Yazdırma Seçenekleri Kullanarak Çalışsayı Yada Çalışma Kitabını Görüntüleme

Genel Bakış

Bazen çalışsayılarınızı resimsel bir temsil olarak sunmanız gerekebilir. Çalışsayı resimlerini uygulamalarınıza veya web sayfalarınıza eklemeniz veya kullanmanız gerekebilir. Resimlerini bir Word belgesine, bir PDF dosyasına, bir PowerPoint sunumuna eklemeniz veya bunları başka bir senaryoda kullanmanız gerekebilir. Basitçe başka bir yerde kullanabilmek için çalışsayısının bir resim olarak görüntülenmesini istersiniz. Aspose.Cells, Excel dosyalarındaki çalışsayıları resme dönüştürmeyi destekler. Ayrıca, Aspose.Cells, resim formatı, çözünürlük (dikey ve yatay), resim kalitesi ve diğer resim ve yazdırma seçenekleri belirleme gibi farklı seçenekleri destekler.

API, örneğin SheetRender, ImageOrPrintOptions, WorkbookRender gibi birkaç değerli sınıf sağlar.

SheetRender sınıfı, çalışsayısı için resimleri oluşturma görevini üstlenirken WorkbookRender aynı görevi bir çalışma kitabı için yapar. Yukarıda bahsedilen her iki sınıfın da, bir çalışsayısını veya bir çalışma kitabını doğrudan dönüştürebilen toImage yönteminin farklı yüklenmiş sürümleri vardır ve bunlar tercih ettiğiniz özellikler veya seçeneklerle belirtilmiş resim dosyasına dönüştürebilir. Resim dosyasını diske/akışa kaydedebilirsiniz. BMP, PNG, GIFF, JPEG, TIFF, EMF ve benzeri birçok resim formatı desteklenmektedir.

Çalışsayısını Resme Dönüştür

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ConvertWorksheettoImage.class) + "TechnicalArticles/";
//Instantiate a new Workbook object
//Open template
Workbook book = new Workbook(dataDir + "book1.xlsx");
//Get the first worksheet
Worksheet sheet = book.getWorksheets().get(0);
//Apply different Image and Print options
ImageOrPrintOptions options = new ImageOrPrintOptions();
//Set Horizontal Resolution
options.setHorizontalResolution(300);
//Set Vertical Resolution
options.setVerticalResolution(300);
//Set TiffCompression
options.setTiffCompression(TiffCompression.COMPRESSION_LZW);
//Set Image Format
options.setImageType(ImageType.TIFF);
//Set printing page type
options.setPrintingPage(PrintingPageType.DEFAULT);
//Render the sheet with respect to specified image/print options
SheetRender sr = new SheetRender(sheet, options);
//Render/save the image for the sheet
sr.toImage(0, dataDir + "CWorksheettoImage_out.tiff");

Dönüşüm Seçenekleri

Belirli sayfaları resme kaydetmek mümkündür. Aşağıdaki kod, bir çalışma kitabındaki ilk ve ikinci çalışsayılarını JPG resimlerine dönüştürür.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ConversionOptions.class) + "TechnicalArticles/";
// Instantiate a new Workbook object
// Open template
Workbook book = new Workbook(dataDir + "book1.xlsx");
// Get the first worksheet
Worksheet sheet = book.getWorksheets().get(0);
// Apply different Image and Print options
ImageOrPrintOptions options = new ImageOrPrintOptions();
// Set Horizontal Resolution
options.setHorizontalResolution(300);
// Set Vertical Resolution
options.setVerticalResolution(300);
// Set Image Format
options.setImageType(ImageType.JPEG);
// If you want entire sheet as a single image
options.setOnePagePerSheet(true);
// Render the sheet with respect to specified image/print options
SheetRender sr = new SheetRender(sheet, options);
// Render/save the image for the sheet
sr.toImage(0, dataDir + "ConversionOptions_out.jpg");

Veya çalışma kitabı üzerinde döngü yaparak her çalışsayısını ayrı bir resime dönüştürebilirsiniz:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(WorksheetToSeparateImage.class) + "TechnicalArticles/";
// Instantiate a new Workbook object
// Open template
Workbook book = new Workbook(dataDir + "book1.xlsx");
// Iterate over all worksheets in the workbook
for (int i = 0; i < book.getWorksheets().getCount(); i++) {
Worksheet sheet = book.getWorksheets().get(i);
// Apply different Image and Print options
ImageOrPrintOptions options = new ImageOrPrintOptions();
// Set Horizontal Resolution
options.setHorizontalResolution(300);
// Set Vertical Resolution
options.setVerticalResolution(300);
// Set Image Format
options.setImageType(ImageType.JPEG);
// If you want entire sheet as a single image
options.setOnePagePerSheet(true);
// Render to image
SheetRender sr = new SheetRender(sheet, options);
sr.toImage(0, dataDir + "WSheetToSImage_out-" + sheet.getName() + ".jpg");
}

Çalışma Kitabını Resme Dönüştür:

Tüm çalışma kitabını resim formatına dönüştürmek için yukarıdaki yaklaşımı kullanabilir veya sadece WorkbookRender sınıfını ve aynı zamanda Workbook sınıfının bir örneğini ve ImageOrPrintOptions nesnesini kabul eden basitçe kullanabilirsiniz.

Tüm çalışma kitabını tek bir TIFF resme çoklu çerçeve veya sayfa olarak kaydedebilirsiniz:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ConvertWorkbooktoImage.class) + "TechnicalArticles/";
// Instantiate a new Workbook object
Workbook book = new Workbook(dataDir + "book1.xlsx");
// Apply different Image and Print options
ImageOrPrintOptions options = new ImageOrPrintOptions();
// Set Image Format
options.setImageType(ImageType.TIFF);
// If you want entire sheet as a single image
options.setOnePagePerSheet(true);
// Render to image
WorkbookRender render = new WorkbookRender(book, options);
render.toImage(dataDir + "CWorkbooktoImage_out.tiff");

İlgili Makaleler