Kako raditi s tablicama u .NET-u

Kako raditi s tablicama u .NET-u

Aspose.Slides FOSS for .NET podržava stvaranje tablica na slajdovima s konfigurabilnim širinama stupaca i visinama redaka. Ovaj vodič pokazuje kako dodati tablicu, popuniti je podacima i primijeniti osnovno formatiranje teksta na ćelije.

Vodič korak po korak

Korak 1: Instalirajte paket

dotnet add package Aspose.Slides.Foss

Korak 2: Stvori ili otvori prezentaciju

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];
// ... add table ...
prs.Save("table.pptx", SaveFormat.Pptx);

Korak 3: Definirajte širine stupaca i visine redaka

Tablice zahtijevaju eksplicitne širine stupaca i visine redaka u točkama (1 točka = 1/72 inča). Standardni slajd je širok 720 točaka i visok 540 točaka.

var colWidths = new double[] { 200.0, 150.0, 150.0 };   // 3 columns
var rowHeights = new double[] { 45.0, 40.0, 40.0 };     // 3 rows

Korak 4: Dodaj tablicu

slide.Shapes.AddTable(x, y, columnWidths, rowHeights) stvara tablicu na položaju (x, y):

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];

var colWidths = new double[] { 200.0, 150.0, 150.0 };
var rowHeights = new double[] { 45.0, 40.0, 40.0 };
var table = slide.Shapes.AddTable(50, 100, colWidths, rowHeights);

prs.Save("table.pptx", SaveFormat.Pptx);

Korak 5: Postavi tekst ćelije

Pristupite ćelijama putem table.Rows[rowIndex][colIndex] i dodijelite tekst putem .TextFrame.Text:

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Export;

using var prs = new Presentation();
var slide = prs.Slides[0];

var colWidths = new double[] { 200.0, 150.0, 150.0 };
var rowHeights = new double[] { 45.0, 40.0, 40.0 };
var table = slide.Shapes.AddTable(50, 100, colWidths, rowHeights);

// Header row (row 0)
string[] headers = { "Product", "Units Sold", "Revenue" };
for (int col = 0; col < headers.Length; col++)
    table.Rows[0][col].TextFrame.Text = headers[col];

// Data rows
string[][] data = {
    new[] { "Widget A", "1,200", "$24,000" },
    new[] { "Widget B", "850", "$17,000" },
};
for (int rowIdx = 0; rowIdx < data.Length; rowIdx++)
    for (int col = 0; col < data[rowIdx].Length; col++)
        table.Rows[rowIdx + 1][col].TextFrame.Text = data[rowIdx][col];

prs.Save("sales-table.pptx", SaveFormat.Pptx);

Korak 6: Formatiraj tekst zaglavlja ćelije

Primijenite podebljano formatiranje na zaglavlja ćelija pomoću PortionFormat:

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;

for (int col = 0; col < headers.Length; col++)
{
    var cell = table.Rows[0][col];
    var portions = cell.TextFrame.Paragraphs[0].Portions;
    if (portions.Count > 0)
    {
        var fmt = portions[0].PortionFormat;
        fmt.FontBold = NullableBool.True;
        fmt.FillFormat.FillType = FillType.Solid;
        fmt.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 255, 255, 255);
    }
}

Potpuni radni primjer

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;
using Aspose.Slides.Foss.Export;

string[][] dataRows = {
    new[] { "North", "$1.2M", "+8%" },
    new[] { "South", "$0.9M", "+4%" },
    new[] { "East",  "$1.5M", "+12%" },
    new[] { "West",  "$0.7M", "+2%" },
};
string[] headers = { "Region", "Revenue", "Growth" };

using var prs = new Presentation();
var slide = prs.Slides[0];

var colWidths = new double[] { 180.0, 140.0, 120.0 };
var rowHeights = new double[dataRows.Length + 1];
rowHeights[0] = 45.0;
for (int i = 1; i < rowHeights.Length; i++) rowHeights[i] = 38.0;

var table = slide.Shapes.AddTable(60, 80, colWidths, rowHeights);

// Header row
for (int col = 0; col < headers.Length; col++)
{
    var cell = table.Rows[0][col];
    cell.TextFrame.Text = headers[col];
    if (cell.TextFrame.Paragraphs[0].Portions.Count > 0)
    {
        var fmt = cell.TextFrame.Paragraphs[0].Portions[0].PortionFormat;
        fmt.FontBold = NullableBool.True;
    }
}

// Data rows
for (int rowIdx = 0; rowIdx < dataRows.Length; rowIdx++)
    for (int col = 0; col < dataRows[rowIdx].Length; col++)
        table.Rows[rowIdx + 1][col].TextFrame.Text = dataRows[rowIdx][col];

prs.Save("regional-revenue.pptx", SaveFormat.Pptx);
Console.WriteLine("Saved regional-revenue.pptx");

Uobičajeni problemi i rješenja

IndexOutOfRangeException prilikom pristupa table.Rows[row][col]

Indeksi redaka i stupaca su indeksirani od nule. Ako ste definirali rowHeights s 3 elementa, valjani indeksi redaka su 0, 1, 2.

Tekst ćelije ne pojavljuje se u spremljenoj datoteci

Uvijek dodjeljujte putem .TextFrame.Text, a ne izravno putem .Text na objekt ćelije:

// Correct
table.Rows[0][0].TextFrame.Text = "Header";

// Wrong: will not compile or silent failure
// table.Rows[0][0].Text = "Header";

Pozicija tablice je izvan slajda

Provjerite x + sum(colWidths) <= 720 i y + sum(rowHeights) <= 540 za standardni slajd.


Često postavljana pitanja

Mogu li spojiti ćelije tablice?

Da. Koristite Table.MergeCells(ICell cell1, ICell cell2, bool allowSplitting) za spajanje dviju susjednih ćelija. Postavite allowSplitting na false kako biste spriječili ponovno razdvajanje spojene ćelije. Primjer:

// Merge cell (row 0, col 0) with cell (row 0, col 1)
var cell1 = table.Rows[0][0];
var cell2 = table.Rows[0][1];
table.MergeCells(cell1, cell2, false);

Mogu li primijeniti boju pozadine za cijelu tablicu?

Primijenite format ispune na svaku pojedinu ćeliju:

using Aspose.Slides.Foss;
using Aspose.Slides.Foss.Drawing;

for (int row = 0; row < table.Rows.Count; row++)
    for (int col = 0; col < table.Rows[row].Count; col++)
    {
        var cell = table.Rows[row][col];
        cell.CellFormat.FillFormat.FillType = FillType.Solid;
        cell.CellFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(255, 240, 248, 255);
    }

Mogu li postaviti stilove rubova ćelija?

Svojstva granica ćelije dostupna su putem svojstava cell.CellFormat.BorderLeft, BorderTop, BorderRight i BorderBottom. Pogledajte referencu API-ja za potpuni popis atributa formata granica.


Vidi također

 Hrvatski