Cách làm việc với bảng trong .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.
Hướng Dẫn Từng Bước
Bước 1: Cài Đặt Gói
dotnet add package Aspose.Slides.FossBước 2: Tạo hoặc Mở một Bản trình bày
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);Bước 3: Xác định Độ rộng Cột và Chiều cao Hàng
Bảng yêu cầu độ rộng cột và chiều cao hàng được chỉ định rõ ràng bằng điểm (1 point = 1/72 inch). Một slide tiêu chuẩn có độ rộng 720 điểm và chiều cao 540 điểm.
var colWidths = new double[] { 200.0, 150.0, 150.0 }; // 3 columns
var rowHeights = new double[] { 45.0, 40.0, 40.0 }; // 3 rowsBước 4: Thêm Bảng
slide.Shapes.AddTable(x, y, columnWidths, rowHeights) tạo bảng tại vị trí (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);Bước 5: Đặt Văn bản cho Ô
Truy cập các ô qua table.Rows[rowIndex][colIndex] và gán văn bản thông qua .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);Bước 6: Định dạng Văn bản Ô Tiêu đề
Áp dụng định dạng in đậm cho các ô tiêu đề bằng cách sử dụng 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);
}
}Ví dụ Hoạt động Đầy đủ
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");Các Vấn Đề Thường Gặp và Cách Khắc Phục
IndexOutOfRangeException khi truy cập table.Rows[row][col]
Chỉ số hàng và cột bắt đầu từ 0. Nếu bạn đã định nghĩa rowHeights với 3 phần tử, các chỉ số hàng hợp lệ là 0, 1, 2.
Văn bản ô không hiển thị trong tệp đã lưu
Luôn gán qua .TextFrame.Text, không phải qua .Text trực tiếp trên đối tượng ô:
// Correct
table.Rows[0][0].TextFrame.Text = "Header";
// Wrong: will not compile or silent failure
// table.Rows[0][0].Text = "Header";Vị trí bảng bị lệch khỏi slide
Kiểm tra rằng x + sum(colWidths) <= 720 và y + sum(rowHeights) <= 540 cho một slide tiêu chuẩn.
Câu hỏi thường gặp
Tôi có thể hợp nhất các ô trong bảng không?
Việc gộp ô không được hỗ trợ trong phiên bản này.
Tôi có thể áp dụng màu nền cho toàn bộ bảng không?
Áp dụng định dạng tô màu cho từng ô riêng lẻ:
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);
}Tôi có thể đặt kiểu viền cho ô không?
Các thuộc tính viền ô có thể truy cập qua cell.CellFormat.BorderLeft, BorderTop, BorderRight, và BorderBottom các thuộc tính. Tham khảo tài liệu API để biết danh sách đầy đủ các thuộc tính định dạng viền.