[]
        
(Showing Draft Content)

分组列

分组列可用于将大量数据组织成有意义的组。

GcExcel 允许您添加分组列以在树视图中查看分层数据,并在视图中显示或隐藏它。IWorksheet接口的getOutlineColumn方法可用于添加分组列。 行分组是通过添加分组列自动创建的。 将工作表保存到 Excel 时,不显示分组列,但保留行分组

可以使用IRange接口的setIndentLevel方法设置单元格的缩进级别。 可以使用IOutlineColumn接口的setMaxLevel方法设置最大缩进级别,默认值为10。


分组列也可以导出为PDF,导入或导出为JSON,与SpreadJS进行交互。

示例代码

请参考下面的示例代码来创建分组列

// create a new workbook
 Workbook workbook = new Workbook();
 IWorksheet worksheet = workbook.getWorksheets().get(0);

 // Set data.
 Object[][] data = new Object[][] { { "Preface", "1", 1, 0 }, { "Java SE5 and SE6", "1.1", 2, 1 },
 { "Java SE6", "1.1.1", 2, 2 }, { "The 4th edition", "1.2", 2, 1 }, { "Changes", "1.2.1", 3, 2 },
 { "Note on the cover design", "1.3", 4, 1 }, { "Acknowledgements", "1.4", 4, 1 },
 { "Introduction", "2", 9, 0 }, { "Prerequisites", "2.1", 9, 1 }, { "Learning Java", "2.2", 10, 1 },
 { "Goals", "2.3", 10, 1 }, { "Teaching from this book", "2.4", 11, 1 },
 { "JDK HTML documentation", "2.5", 11, 1 }, { "Exercises", "2.6", 12, 1 },
 { "Foundations for Java", "2.7", 12, 1 }, { "Source code", "2.8", 12, 1 },
 { "Coding standards", "2.8.1", 14, 2 }, { "Errors", "2.9", 14, 1 },
 { "Introduction to Objects", "3", 15, 0 }, { "The progress of abstraction", "3.1", 15, 1 },
 { "An object has an interface", "3.2", 17, 1 }, { "An object provides services", "3.3", 18, 1 },
 { "The hidden implementation", "3.4", 19, 1 }, { "Reusing the implementation", "3.5", 20, 1 },
 { "Inheritance", "3.6", 21, 1 }, { "Is-a vs. is-like-a relationships", "3.6.1", 24, 2 },
 { "Interchangeable objects with polymorphism", "3.7", 25, 1 },
 { "The singly rooted hierarchy", "3.8", 28, 1 }, { "Containers", "3.9", 28, 1 },
 { "Parameterized types (Generics)", "3.10", 29, 1 }, { "Object creation & lifetime", "3.11", 30, 1 },
 { "Exception handling: dealing with errors", "3.12", 31, 1 },
 { "Concurrent programming", "3.13", 32, 1 }, { "Java and the Internet", "3.14", 33, 1 },
 { "What is the Web?", "3.14.1", 33, 2 }, { "Client-side programming", "3.14.2", 34, 2 },
 { "Server-side programming", "3.14.3", 38, 2 }, { "Summary", "3.15", 38, 1 } };
 worksheet.getRange("A1:C38").setValue(data);

 // Set ColumnWidth.
 worksheet.getRange("A:A").setColumnWidthInPixel(310);
 worksheet.getRange("B:C").setColumnWidthInPixel(150);

 // Set IndentLevel.
 for (int i = 0; i < data.length; i++) {
 worksheet.getRange(i, 0).setIndentLevel((int) data[i][3]);
 }
 // Show the summary row above the detail rows.
 worksheet.getOutline().setSummaryRow(SummaryRow.Above);

 // Don't show the row outline when interacting with SJS, the exported excel file
 // still show the row outline.
 worksheet.setShowRowOutline(false);

 // Set outline column, the corresponding row outlines will also be automatically
 // created.
 worksheet.getOutlineColumn().setColumnIndex(0);
 worksheet.getOutlineColumn().setShowCheckBox(true);
 worksheet.getOutlineColumn().setShowImage(true);
 worksheet.getOutlineColumn().setMaxLevel(2);

 worksheet.getOutlineColumn().getImages()
 .add(new ImageSource(new FileInputStream("archiverFolder.png"), ImageType.PNG));
 worksheet.getOutlineColumn().getImages()
 .add(new ImageSource(new FileInputStream("newFloder.png"), ImageType.PNG));
 worksheet.getOutlineColumn().getImages()
 .add(new ImageSource(new FileInputStream("docFile.png"), ImageType.PNG));
 worksheet.getOutlineColumn()
 .setCollapseIndicator(new ImageSource(new FileInputStream("decreaseIndicator.png"), ImageType.PNG));
 worksheet.getOutlineColumn()
 .setExpandIndicator(new ImageSource(new FileInputStream("increaseIndicator.png"), ImageType.PNG));

 worksheet.getOutlineColumn().setCheckStatus(0, true);
 worksheet.getOutlineColumn().setCollapsed(1, true);

 // MSExcel does not support the outline column, so when exporting to the excel file,
 // The checkbox, level images, expand&collapse images are not visible.
 // But the data is seen with heirarchical structure.

 // save to an excel and PDF file
 workbook.save("outlinecolumn.pdf");
 workbook.save("outlinecolumn.xlsx");

注意: 图像,复选框,展开或折叠指标图像在Excel中不可见,因为Excel不支持它们,但它们可以在PDF和SpreadJS中查看。

下面的图像显示了上述代码片段的Excel输出:

Excel 输出


以下图像显示上述代码片段的PDF输出:

PDF 输出