.NET에서 테이블 작업하는 방법

.NET에서 테이블 작업하는 방법

Aspose.Slides FOSS for .NET supports creating tables on slides with configurable column widths and row heights. This guide shows how to add a table, populate it with data, and apply basic text formatting to cells.

단계별 가이드

단계 1: 패키지 설치

dotnet add package Aspose.Slides.Foss

단계 2: 프레젠테이션 만들기 또는 열기

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);

단계 3: 열 너비와 행 높이 정의하기

테이블은 포인트 단위(1포인트 = 1/72인치)로 명시적인 열 너비와 행 높이가 필요합니다. 표준 슬라이드의 크기는 가로 720포인트, 세로 540포인트입니다.

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

단계 4: 테이블 추가하기

slide.Shapes.AddTable(x, y, columnWidths, rowHeights) 위치에 테이블을 생성합니다 (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);

단계 5: 셀 텍스트 설정하기

셀에 접근하려면 table.Rows[rowIndex][colIndex] 그리고 다음을 사용하여 텍스트를 할당합니다 .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);

단계 6: 헤더 셀 텍스트 서식 지정하기

다음을 사용하여 헤더 셀에 굵게 서식을 적용합니다 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);
    }
}

완전한 작동 예제

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");

일반적인 문제 및 해결책

IndexOutOfRangeException 접근할 때 table.Rows[row][col]

행 및 열 인덱스는 0부터 시작합니다. 만약 정의했다면 rowHeights 3개의 요소가 있는 경우, 유효한 행 인덱스는 0, 1, 2입니다.

셀 텍스트가 저장된 파일에 표시되지 않음

항상 다음을 통해 할당하세요 .TextFrame.Text, 다음이 아니라 .Text 셀 객체에 직접:

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

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

표 위치가 슬라이드 밖에 있습니다.

다음이 확인되었는지 확인하세요 x + sum(colWidths) <= 720y + sum(rowHeights) <= 540 표준 슬라이드에 대해.


자주 묻는 질문

표 셀을 병합할 수 있나요?

이 버전에서는 셀 병합을 지원하지 않습니다.

표 전체에 배경색을 적용할 수 있나요?

각 개별 셀에 채우기 서식을 적용하십시오:

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);
    }

셀 테두리 스타일을 설정할 수 있나요?

셀 테두리 속성은 다음을 통해 접근할 수 있습니다 cell.CellFormat.BorderLeft, BorderTop, BorderRight, 및 BorderBottom 속성. 테두리 서식 속성 전체 목록은 API 레퍼런스를 참조하십시오.


또 보기

 한국어