לבנות שולחן מ DataTable

Contents
[ ]

לעתים קרובות היישום שלך ימשוך נתונים ממסד נתונים ולאחסן אותו בצורה של DataTable. ייתכן שתרצה להוסיף בקלות נתונים אלה לתוך המסמך שלך כשולחן חדש וליישם במהירות פורמט לכל השולחן.

שימוש Aspose.Words, אתה יכול בקלות לשחזר נתונים ממסד נתונים ולאחסן אותו כשולחן:

1.1 1. ליצור חדש DocumentBuilder אובייקט על Document. 1.1 1. התחל שולחן חדש באמצעות DocumentBuilder. 1.1 1. אם אנו רוצים להוסיף את השמות של כל אחד מהעמודות שלנו DataTable כשורה אחורית, הוא מחלחל דרך כל עמודה נתונים וכותב את שמות העמודה לשורה בטבלה. 1.1 1. דרך כל אחד DataRow בתוך DataTable: 1.לעבור כל אובייקט ב DataRow. 1 הכנס את האובייקט לתוך המסמך באמצעות DocumentBuilder. השיטה המשמשת תלויה בסוג האובייקט מוכנס e.g DocumentBuilder.writeln() לטקסט ו DocumentBuilder.insertImage() עבור מערך עוטה המייצג תמונה. בסופו של עיבוד של שורת הנתונים מסתיים השורה שנוצרת על ידי DocumentBuilder באמצעות DocumentBuilder.endRow(). 1.1 1. פעם כל שורות מן DataTable מעובד לסיים את השולחן באמצעות קריאה DocumentBuilder.endTable(). 1.1 1. לבסוף, אנו יכולים להגדיר את סגנון השולחן הרצוי באמצעות אחד מתכונות השולחן המתאימות כגון: 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