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

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

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

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

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
builder.insertCell();
builder.write("Name");
builder.insertCell();
builder.write("Value");
builder.endRow();
builder.insertCell();
builder.insertCell();
builder.endTable();
TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1");
tableStyle.getBorders().setLineStyle(LineStyle.DOUBLE);
tableStyle.getBorders().setLineWidth(1.0);
tableStyle.setLeftPadding(18.0);
tableStyle.setRightPadding(18.0);
tableStyle.setTopPadding(12.0);
tableStyle.setBottomPadding(12.0);
table.setStyle(tableStyle);
doc.save(getArtifactsDir() + "WorkingWithTableStylesAndFormatting.CreateTableStyle.docx");

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

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document srcDoc = new Document();
// Create a custom style for the source document.
Style srcStyle = srcDoc.getStyles().add(StyleType.PARAGRAPH, "MyStyle");
srcStyle.getFont().setColor(Color.RED);
// Import the source document's custom style into the destination document.
Document dstDoc = new Document();
Style newStyle = dstDoc.getStyles().addCopy(srcStyle);
// The imported style has an appearance identical to its source style.
Assert.assertEquals("MyStyle", newStyle.getName());
Assert.assertEquals(Color.RED.getRGB(), newStyle.getFont().getColor().getRGB());

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

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-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
builder.insertCell();
builder.write("Name");
builder.insertCell();
builder.write("Value");
builder.endRow();
builder.insertCell();
builder.insertCell();
builder.endTable();
TableStyle tableStyle = (TableStyle) doc.getStyles().add(StyleType.TABLE, "MyTableStyle1");
tableStyle.getConditionalStyles().getFirstRow().getShading().setBackgroundPatternColor(Color.yellow);
tableStyle.getConditionalStyles().getFirstRow().getShading().setTexture(TextureIndex.TEXTURE_NONE);
table.setStyle(tableStyle);
doc.save(getArtifactsDir() + "WorkingWithTableStylesAndFormatting.DefineConditionalFormatting.docx");

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
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.setStyleIdentifier(StyleIdentifier.MEDIUM_SHADING_1_ACCENT_1);
// Apply which features should be formatted by the style.
table.setStyleOptions(TableStyleOptions.FIRST_COLUMN | TableStyleOptions.ROW_BANDS | TableStyleOptions.FIRST_ROW);
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_CONTENTS);
builder.writeln("Item");
builder.getCellFormat().setRightPadding(40.0);
builder.insertCell();
builder.writeln("Quantity (kg)");
builder.endRow();
builder.insertCell();
builder.writeln("Apples");
builder.insertCell();
builder.writeln("20");
builder.endRow();
builder.insertCell();
builder.writeln("Bananas");
builder.insertCell();
builder.writeln("40");
builder.endRow();
builder.insertCell();
builder.writeln("Carrots");
builder.insertCell();
builder.writeln("50");
builder.endRow();
doc.save(getArtifactsDir() + "WorkingWithTableStylesAndFormatting.BuildTableWithStyle.docx");

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

table-style-aspose-words-java

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "Tables.docx");
// Get the first cell of the first table in the document.
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
Cell firstCell = table.getFirstRow().getFirstCell();
// First print the color of the cell shading.
// This should be empty as the current shading is stored in the table style.
Color cellShadingBefore = firstCell.getCellFormat().getShading().getBackgroundPatternColor();
System.out.println("Cell shading before style expansion: " + cellShadingBefore);
doc.expandTableStylesToDirectFormatting();
// Now print the cell shading after expanding table styles.
// A blue background pattern color should have been applied from the table style.
Color cellShadingAfter = firstCell.getCellFormat().getShading().getBackgroundPatternColor();
System.out.println("Cell shading after style expansion: " + cellShadingAfter);