.NET에서 테이블 작업하기

.NET에서 테이블 작업하기

Aspose.Slides FOSS for .NET은 구성 가능한 열 너비와 행 높이를 가진 슬라이드에 표를 만들 수 있도록 지원합니다. 이 가이드는 표를 추가하고 데이터를 채우며 셀에 기본 텍스트 서식을 적용하는 방법을 보여줍니다.

단계별 가이드

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입니다.

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

셀 객체에 직접 .Text을 사용하지 말고, 항상 .TextFrame.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을 확인하십시오.


자주 묻는 질문

테이블 셀을 병합할 수 있나요?

예. Table.MergeCells(ICell cell1, ICell cell2, bool allowSplitting)를 사용하여 인접한 두 셀을 병합합니다. allowSplittingfalse로 설정하여 병합된 셀이 다시 분할되는 것을 방지합니다. 예시:

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

테이블 전체에 배경색을 적용할 수 있나요?

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

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, BorderRightBorderBottom 속성을 통해 액세스할 수 있습니다. 전체 테두리 형식 속성 목록은 API 참조를 확인하십시오.


또 보기

 한국어