在此指南中,我们将通过将单个 Excel 细胞转换为使用 Aspose.Cells for .NET 的图像文件的过程,这特别有用,当您需要从分布表中提取特定值或标签并将其呈现到视觉上有吸引力的格式时.

现实世界使用案例

  • 出口价格或产品显示总额
  • 隔离键计为Dashboards
  • 创建个别值的图像小组

步骤指南

步骤 1: 安装 Aspose.Cells 为 .NET

要开始,您需要通过 NuGet Package Manager 安装 Aspose.Cells 包.

$ dotnet add package Aspose.Cells

步骤2:下载工作簿和工作表

加载您的 Excel 文件并访问目标细胞居住的工作表.

Workbook workbook = new Workbook("KPIReport.xlsx");
Worksheet sheet = workbook.Worksheets[0];

步骤3:选择目标细胞

识别您想要作为图像出口的特定细胞,例如,让我们将目标为B5.

Cell cell = sheet.Cells["B5"];

步骤4:将印刷区域设置为单元格

设置工作表的打印区域以专注于单独选择的单元格.

sheet.PageSetup.PrintArea = "B5";

步骤5:设置图像转换选项

设置显示图像的选项,包括分辨率和格式类型.

ImageOrPrintOptions options = new ImageOrPrintOptions
{
    ImageType = ImageType.Png,
    OnePagePerSheet = true,
    HorizontalResolution = 300,
    VerticalResolution = 300
};

步骤6:使用 SheetRender 转换

使用 The SheetRender 分类为单细胞打印区域,并将其保存为图像.

SheetRender renderer = new SheetRender(sheet, options);
renderer.ToImage(0, "cell_b5_output.png");

步骤7:保存和审查输出

运行代码后,您将有一个干净的 PNG 文件显示一个单元格与格式不完整.

完整的例子代码

下面是如何将 Excel 单元格作为图像出口的完整例子,使用 Aspose.Cells for .NET:

using System;
using Aspose.Cells;
class Program
{
    static void Main()
    {
        // Load workbook
        Workbook workbook = new Workbook("KPIReport.xlsx");

        // Access the worksheet and target cell
        Worksheet sheet = workbook.Worksheets[0];
        Cell cell = sheet.Cells["B5"];

        // Set print area to that cell
        sheet.PageSetup.PrintArea = "B5";

        // Image export settings
        ImageOrPrintOptions options = new ImageOrPrintOptions
        {
            ImageType = ImageType.Png,
            OnePagePerSheet = true,
            HorizontalResolution = 300,
            VerticalResolution = 300
        };

        // Render and save
        SheetRender renderer = new SheetRender(sheet, options);
        renderer.ToImage(0, "cell_b5_output.png");

        Console.WriteLine("Cell B5 exported successfully as image.");
    }
}

有助的提示

提示描述
提高阅读能力增强分辨率或字体大小
添加背景或边界细胞格式化之前的 rendering
Align 内容使用 cell.GetStyle() 要调和或粘贴

More in this category