Miten muuttaa työpöytä kuviin C#: ssä

Miten muuttaa työpöytä kuviin C#: ssä

Yhden Excel-työkalun vienti kuvanmuodossa (esim. PNG, JPEG) on hyödyllistä, kun luot ennakkoluuloja, viedä kaavioita tai jakaa lukemattomia visuaalisia esityksiä levytiedoston sisällöstä.Tämä opas näyttää sinulle, miten muuntaa yksi työkalu Excelin työkirjastosta kuviin käyttämällä Aspose.Cells for .NET.

Käytä tapauksia

  • Luo etukäteen tietyn työpöydän
  • Vienti muodostetut raportit sähköpostille tai dokumentaatiolle
  • Sisällytä yksi lehti verkkosivulle tai PDF-tiedostoon

Vaiheittainen opas

Vaihe 1: Asenna Aspose.Cells .NET

dotnet add package Aspose.Cells

Vaihe 2: Lataa Excel-tiedosto

Workbook workbook = new Workbook("SalesData.xlsx");
Worksheet sheet = workbook.Worksheets["Q1 Report"]; // Access specific worksheet

Vaihe 3: Määritä kuvan rendering vaihtoehdot

ImageOrPrintOptions options = new ImageOrPrintOptions
{
    ImageType = ImageType.Png,
    OnePagePerSheet = true,
    HorizontalResolution = 200,
    VerticalResolution = 200,
    PrintingPageType = PrintingPageType.Default
};

Vaihe 4: Luo SheetRender Object

SheetRender renderer = new SheetRender(sheet, options);

Vaihe 5: Siirrä jokainen sivu kuviin

for (int pageIndex = 0; pageIndex < renderer.PageCount; pageIndex++)
{
    string imageName = $"worksheet_q1_page_{pageIndex + 1}.png";
    renderer.ToImage(pageIndex, imageName);
}

Vaihe 6: Tallenna kuvat

Tämä koodi tallentaa automaattisesti yhden kuvan painettavissa olevasta sivusta työpöydässä.

Vaihe 7: Vaihtoehtoiset parannukset

Voit soveltaa lisättyjä asetuksia:

// Show gridlines in the output image
options.ShowGridLines = true;

// Fit all content on a single page
options.AllColumnsInOnePagePerSheet = true;

Täydellinen esimerkki koodi

using System;
using Aspose.Cells;

class Program
{
    static void Main()
    {
        // Load the Excel workbook
        Workbook workbook = new Workbook("SalesData.xlsx");

        // Access a specific worksheet
        Worksheet sheet = workbook.Worksheets["Q1 Report"];

        // Define image rendering options
        ImageOrPrintOptions options = new ImageOrPrintOptions
        {
            ImageType = ImageType.Png,
            OnePagePerSheet = true,
            HorizontalResolution = 200,
            VerticalResolution = 200,
            PrintingPageType = PrintingPageType.Default
        };

        // Enable gridlines if desired
        options.ShowGridLines = true;

        // Render the sheet to image(s)
        SheetRender renderer = new SheetRender(sheet, options);

        for (int pageIndex = 0; pageIndex < renderer.PageCount; pageIndex++)
        {
            string imageName = $"worksheet_q1_page_{pageIndex + 1}.png";
            renderer.ToImage(pageIndex, imageName);
            Console.WriteLine($"Saved: {imageName}");
        }

        Console.WriteLine("Worksheet successfully rendered to image(s).");
    }
}

Yhteiset skenaariot ja ongelmanratkaisu

OngelmaRatkaisu
Cut-off sisältöUse AllColumnsInOnePagePerSheet = true
Tuotto on alhainen laatuKuvan resoluution lisääminen
Puuttuvat linjatSet ShowGridLines = true
 Suomi