یک جدول از یک DataTable بسازید

Contents
[ ]

اغلب برنامه شما داده ها را از یک پایگاه داده بیرون می کشد و آن ها را در قالب یک DataTable ذخیره می کند. شما به راحتی می توانید این داده ها را به عنوان یک جدول جدید در سند خود وارد کنید و به سرعت قالب بندی را در کل جدول اعمال کنید.

با استفاده از Aspose.Words، می توانید به راحتی داده ها را از یک پایگاه داده بازیابی کرده و به صورت جدول ذخیره کنید:

  1. یک شی DocumentBuilder جدید در Document خود ایجاد کنید.
  2. یک جدول جدید با استفاده از DocumentBuilder شروع کنید.
  3. اگر می‌خواهیم نام هر یک از ستون‌ها را از DataTable خود به‌عنوان یک ردیف هدر درج کنیم، در هر ستون داده‌ها را تکرار کرده و نام ستون‌ها را در یک ردیف در جدول بنویسیم.
  4. از طریق هر DataRow در DataTable تکرار کنید:
    1. از طریق هر شی در DataRow تکرار کنید.
    2. شی را با استفاده از DocumentBuilder در سند وارد کنید. روش استفاده شده بستگی به نوع شیء درج شده دارد، مثلاً DocumentBuilder.Writeln برای متن و DocumentBuilder.InsertImage برای آرایه بایتی که یک تصویر را نشان می دهد.
    3. در پایان پردازش DataRow همچنین ردیف ایجاد شده توسط DocumentBuilder را با استفاده از DocumentBuilder.EndRow پایان دهید.
  5. هنگامی که تمام ردیف های DataTable پردازش شدند، جدول را با فراخوانی DocumentBuilder.EndTable تمام کنید.
  6. در نهایت می توانیم سبک جدول مورد نظر را با استفاده از یکی از ویژگی های جدول مناسب مانند Table.StyleIdentifier تنظیم کنیم تا به طور خودکار قالب بندی را در کل جدول اعمال کنیم.

روش ImportTableFromDataTable یک شی DocumentBuilder را می پذیرد، DataTable حاوی داده ها و یک پرچم است که مشخص می کند عنوان ستون از DataTable در بالای جدول گنجانده شده است یا خیر. این روش یک جدول از این پارامترها با استفاده از موقعیت فعلی سازنده و قالب بندی می سازد. روشی را برای وارد کردن داده ها از DataTable و درج آن در جدول جدید با استفاده از DocumentBuilder ارائه می دهد.

داده های زیر در DataTable ما در این مثال استفاده شده است:

how-to-build-a-table-from-a-datatable-aspose-words-net

مثال کد زیر نحوه اجرای الگوریتم فوق را در Aspose.Words نشان می دهد:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
/// <summary>
/// Imports the content from the specified DataTable into a new Aspose.Words Table object.
/// The table is inserted at the document builder's current position and using the current builder's formatting if any is defined.
/// </summary>
public Table ImportTableFromDataTable(DocumentBuilder builder, DataTable dataTable,
bool importColumnHeadings)
{
Table table = builder.StartTable();
// Check if the columns' names from the data source are to be included in a header row.
if (importColumnHeadings)
{
// Store the original values of these properties before changing them.
bool boldValue = builder.Font.Bold;
ParagraphAlignment paragraphAlignmentValue = builder.ParagraphFormat.Alignment;
// Format the heading row with the appropriate properties.
builder.Font.Bold = true;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
// Create a new row and insert the name of each column into the first row of the table.
foreach (DataColumn column in dataTable.Columns)
{
builder.InsertCell();
builder.Writeln(column.ColumnName);
}
builder.EndRow();
// Restore the original formatting.
builder.Font.Bold = boldValue;
builder.ParagraphFormat.Alignment = paragraphAlignmentValue;
}
foreach (DataRow dataRow in dataTable.Rows)
{
foreach (object item in dataRow.ItemArray)
{
// Insert a new cell for each object.
builder.InsertCell();
switch (item.GetType().Name)
{
case "DateTime":
// Define a custom format for dates and times.
DateTime dateTime = (DateTime) item;
builder.Write(dateTime.ToString("MMMM d, yyyy"));
break;
default:
// By default any other item will be inserted as text.
builder.Write(item.ToString());
break;
}
}
// After we insert all the data from the current record, we can end the table row.
builder.EndRow();
}
// We have finished inserting all the data from the DataTable, we can end the table.
builder.EndTable();
return table;
}

سپس می توان روش را با استفاده از DocumentBuilder و داده های شما به راحتی فراخوانی کرد.

مثال کد زیر نحوه وارد کردن داده ها از یک DataTable و درج آن را در جدول جدید در سند نشان می دهد:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
// We can position where we want the table to be inserted and specify any extra formatting to the table.
DocumentBuilder builder = new DocumentBuilder(doc);
// We want to rotate the page landscape as we expect a wide table.
doc.FirstSection.PageSetup.Orientation = Orientation.Landscape;
DataSet ds = new DataSet();
ds.ReadXml(MyDir + "List of people.xml");
// Retrieve the data from our data source, which is stored as a DataTable.
DataTable dataTable = ds.Tables[0];
// Build a table in the document from the data contained in the DataTable.
Table table = ImportTableFromDataTable(builder, dataTable, true);
// We can apply a table style as a very quick way to apply formatting to the entire table.
table.StyleIdentifier = StyleIdentifier.MediumList2Accent1;
table.StyleOptions = TableStyleOptions.FirstRow | TableStyleOptions.RowBands | TableStyleOptions.LastColumn;
// For our table, we want to remove the heading for the image column.
table.FirstRow.LastCell.RemoveAllChildren();
doc.Save(ArtifactsDir + "WorkingWithTables.BuildTableFromDataTable.docx");