.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.

स्टेप बाय स्टेप गाइड

Step 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]

पंक्ति और स्तंभ सूचकांक शून्य-आधारित हैं। यदि आपने परिभाषित किया 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) <= 720 और y + 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 properties. API reference में सीमा स्वरूप गुणों की पूरी सूची देखें।.


संबंधित देखें

 हिन्दी