ใช้รูปแบบตาราง

ลักษณะตารางกำหนดชุดของการจัดรูปแบบที่สามารถนำไปใช้กับตาราง การจัดรูปแบบเช่นเส้นขอบแรเงาการจัดตำแหน่งและแบบอักษรสามารถตั้งค่าในลักษณะต.

Aspose.Wordsสนับสนุนการใช้รูปแบบตารางกับตารางและยังอ่านคุณสมบัติของรูปแบบตารางใดๆ รูปแบบตารางจะถูกเก็บรักษาไว้ในระหว่างการโหลดและการบันทึกในรูปแบบต่อไปนี้:

  • รูปแบบตารางในDOCXและรูปแบบเวิร์ดมิลล์จะถูกเก็บรักษาไว้เมื่อโหลดและบันทึกในรูปแบบเหล่
  • รูปแบบตารางจะถูกเก็บรักษาไว้เมื่อโหลดและบันทึกในรูปแบบDOC(แต่ไม่รวมรูปแบบอื่นๆ)
  • เมื่อส่งออกไปยังรูปแบบอื่นๆการแสดงผลหรือการพิมพ์รูปแบบตารางจะขยายไปสู่การจัดรูป

สร้างสไตล์ตาราง

ผู้ใช้สามารถสร้างสไตล์ใหม่และเพิ่มลงในคอลเล็กชันสไตล์ วิธีการAddถูกใช้เพื่อสร้างสไตล์ตารางใหม่.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการสร้างลักษณะตารางที่ผู้ใช้กำหนดใหม่:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Name");
builder->InsertCell();
builder->Write(u"Value");
builder->EndRow();
builder->InsertCell();
builder->InsertCell();
builder->EndTable();
auto tableStyle = System::ExplicitCast<TableStyle>(doc->get_Styles()->Add(StyleType::Table, u"MyTableStyle1"));
tableStyle->get_Borders()->set_LineStyle(LineStyle::Double);
tableStyle->get_Borders()->set_LineWidth(1);
tableStyle->set_LeftPadding(18);
tableStyle->set_RightPadding(18);
tableStyle->set_TopPadding(12);
tableStyle->set_BottomPadding(12);
table->set_Style(tableStyle);
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.CreateTableStyle.docx");

คัดลอกสไตล์ตารางที่มีอยู่

หากจำเป็น คุณสามารถคัดลอกรูปแบบตารางที่มีอยู่แล้วในเอกสารบางฉบับลงในคอลเล็กชันรูปแบบของคุณโดยใช้เมธอด AddCopy.

สิ่งสำคัญคือต้องทราบว่าด้วยการคัดลอกนี้ลักษณะที่เชื่อมโยงจะถูกคัดลอกด้วย.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการนำเข้าสไตล์จากเอกสารหนึ่งไปยังเอกสารอื่น:

EXAMPLE

ใช้รูปแบบตารางที่มีอยู่

Aspose.Wordsให้TableStyleสืบทอดมาจากชั้นเรียนStyle TableStyleอำนวยความสะดวกให้ผู้ใช้สามารถใช้ตัวเลือกสไตล์ที่แตกต่างกันเช่นการแรเงา,ช่องว่าง,เยื้อง,CellSpacingและFontฯลฯ.

นอกจากนี้Aspose.Wordsให้StyleCollectionชั้นและคุณสมบัติไม่กี่ของชั้นเรียนTableเพื่อระบุรูปแบบตารางที่เราจะทำงานกับ: Style, StyleIdentifier, StyleName, และStyleOptions.

Aspose.Words ยังให้คลาส ConditionalStyle ที่แสดงการจัดรูปแบบพิเศษที่นำไปใช้กับพื้นที่บางส่วนของตารางที่มีรูปแบบตารางที่กำหนดไว้ และคลาส ConditionalStyleCollection ที่แสดงชุดของอ็อบเจ็กต์ ConditionalStyle คอลเลกชันนี้ประกอบด้วยชุดรายการถาวรที่แสดงรายการหนึ่งรายการสำหรับแต่ละค่าของประเภทการแจงนับ ConditionalStyleType การแจงนับ ConditionalStyleType กำหนดพื้นที่ตารางที่เป็นไปได้ทั้งหมดซึ่งอาจกำหนดการจัดรูปแบบตามเงื่อนไขในรูปแบบตาราง.

ในกรณีนี้ สามารถกำหนดการจัดรูปแบบตามเงื่อนไขสำหรับพื้นที่ตารางที่เป็นไปได้ทั้งหมดที่กำหนดไว้ภายใต้ชนิดการแจงนับ ConditionalStyleType ได้.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการกำหนดรูปแบบตามเงื่อนไขสำหรับแถวส่วนหัวของตาราง:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Name");
builder->InsertCell();
builder->Write(u"Value");
builder->EndRow();
builder->InsertCell();
builder->InsertCell();
builder->EndTable();
auto tableStyle = System::ExplicitCast<TableStyle>(doc->get_Styles()->Add(StyleType::Table, u"MyTableStyle1"));
tableStyle->get_ConditionalStyles()->get_FirstRow()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_GreenYellow());
tableStyle->get_ConditionalStyles()->get_FirstRow()->get_Shading()->set_Texture(TextureIndex::TextureNone);
table->set_Style(tableStyle);
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.DefineConditionalFormatting.docx");

นอกจากนี้คุณยังสามารถเลือกส่วนตารางที่จะใช้ลักษณะเช่นคอลัมน์แรกคอลัมน์สุดท้ายแถว นการแจงนับTableStyleOptionsและถูกนำไปใช้ผ่านคุณสมบัติStyleOptions นับTableStyleOptionsอนุญาตให้รวมกันบิตของคุณลักษณะเหล่านี้.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการสร้างตารางใหม่ที่มีลักษณะตารางที่ใช้:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
// We must insert at least one row first before setting any table formatting.
builder->InsertCell();
// Set the table style used based on the unique style identifier.
table->set_StyleIdentifier(StyleIdentifier::MediumShading1Accent1);
// Apply which features should be formatted by the style.
table->set_StyleOptions(TableStyleOptions::FirstColumn | TableStyleOptions::RowBands | TableStyleOptions::FirstRow);
table->AutoFit(AutoFitBehavior::AutoFitToContents);
builder->Writeln(u"Item");
builder->get_CellFormat()->set_RightPadding(40);
builder->InsertCell();
builder->Writeln(u"Quantity (kg)");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Apples");
builder->InsertCell();
builder->Writeln(u"20");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Bananas");
builder->InsertCell();
builder->Writeln(u"40");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Carrots");
builder->InsertCell();
builder->Writeln(u"50");
builder->EndRow();
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.BuildTableWithStyle.docx");

รูปภาพด้านล่างแสดงตัวแทนของTable StylesในMicrosoft Wordและคุณสมบัติที่สอดคล้องกันในAspose.Words.

formatting-table-style-aspose-words-cpp

ใช้การจัดรูปแบบจากรูปแบบตารางและใช้มันเป็นรูปแบบโดยตรง

Aspose.Wordsนอกจากนี้ยังมีวิธีการExpandTableStylesToDirectFormattingในการจัดรูปแบบที่พบในสไตล์ตารางและขยายไปยังแถวและเซลล์ของตารางเป็นการจัดรูปแบบโดยตรง ลองรวมการจัดรูปแบบกับรูปแบบตารางและรูปแบบเซลล์.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการขยายการจัดรูปแบบจากลักษณะไปยังตารางแถวและเซล:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
// Get the first cell of the first table in the document.
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
SharedPtr<Cell> firstCell = table->get_FirstRow()->get_FirstCell();
// First print the color of the cell shading.
// This should be empty as the current shading is stored in the table style.
System::Drawing::Color cellShadingBefore = firstCell->get_CellFormat()->get_Shading()->get_BackgroundPatternColor();
std::cout << (String(u"Cell shading before style expansion: ") + cellShadingBefore) << std::endl;
doc->ExpandTableStylesToDirectFormatting();
// Now print the cell shading after expanding table styles.
// A blue background pattern color should have been applied from the table style.
System::Drawing::Color cellShadingAfter = firstCell->get_CellFormat()->get_Shading()->get_BackgroundPatternColor();
std::cout << (String(u"Cell shading after style expansion: ") + cellShadingAfter) << std::endl;