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

Contents
[ ]

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

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

  1. یک شی جدید DocumentBuilder در Document خود ایجاد کنید.
  2. شروع یک جدول جدید با استفاده از DocumentBuilder.
  3. اگر می خواهیم نام هر یک از ستون ها را از DataTable خود به عنوان یک ردیف سرصفحه وارد کنیم، سپس از طریق هر ستون داده تکرار کنید و نام ستون ها را به یک ردیف در جدول بنویسید.
  4. از طریق هر DataRow در DataTableتکرار کنید:
    1. از طریق هر شی در DataRow تکرار کنید.
    2. شی را با استفاده از DocumentBuilder وارد سند کنید. روش مورد استفاده به نوع شیء وارد شده بستگی دارد به عنوان مثال DocumentBuilder.writeln() برای متن و DocumentBuilder.insertImage() برای یک آرایه بایت که یک تصویر را نشان می دهد.
    3. در پایان پردازش ردیف داده ها نیز ردیف ایجاد شده توسط DocumentBuilder با استفاده از DocumentBuilder.endRow() به پایان می رسد.
  5. هنگامی که تمام ردیف های DataTable پردازش شده اند، جدول را با فراخوانی DocumentBuilder.endTable() به پایان برسانید.
  6. در نهایت، ما می توانیم سبک جدول مورد نظر را با استفاده از یکی از ویژگی های جدول مناسب مانند Table.getStyleIdentifier() تنظیم کنیم تا به طور خودکار قالب بندی را به کل جدول اعمال کنیم. داده های زیر در DataTable ما در این مثال استفاده می شود:

build-a-table-from-a-datatable-aspose-words-java-1

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.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,
boolean 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.
boolean boldValue = builder.getFont().getBold();
int paragraphAlignmentValue = builder.getParagraphFormat().getAlignment();
// Format the heading row with the appropriate properties.
builder.getFont().setBold(true);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
// Create a new row and insert the name of each column into the first row of the table.
for (DataColumn column : dataTable.getColumns())
{
builder.insertCell();
builder.writeln(column.getColumnName());
}
builder.endRow();
// Restore the original formatting.
builder.getFont().setBold(boldValue);
builder.getParagraphFormat().setAlignment(paragraphAlignmentValue);
}
for (DataRow dataRow : (Iterable<DataRow>) dataTable.getRows())
{
for (Object item : dataRow.getItemArray())
{
// Insert a new cell for each object.
builder.insertCell();
switch (item.getClass().getName())
{
case "DateTime":
// Define a custom format for dates and times.
Date dateTime = (Date) item;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM d, yyyy");
builder.write(simpleDateFormat.format(dateTime));
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-Java.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.getFirstSection().getPageSetup().setOrientation(Orientation.LANDSCAPE);
DataSet ds = new DataSet();
ds.readXml(getMyDir() + "List of people.xml");
// Retrieve the data from our data source, which is stored as a DataTable.
DataTable dataTable = ds.getTables().get(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.setStyleIdentifier(StyleIdentifier.MEDIUM_LIST_2_ACCENT_1);
table.setStyleOptions(TableStyleOptions.FIRST_ROW | TableStyleOptions.ROW_BANDS | TableStyleOptions.LAST_COLUMN);
// For our table, we want to remove the heading for the image column.
table.getFirstRow().getLastCell().removeAllChildren();
doc.save(getArtifactsDir() + "WorkingWithTables.BuildTableFromDataTable.docx");

جدول نشان داده شده در تصویر زیر با اجرای کد بالا تولید می شود.

build-a-table-from-a-datatable-aspose-words-java-2