在加载工作簿或工作表时过滤对象

可能的使用场景

在从工作簿中过滤数据时,请使用 LoadOptions.LoadFilter 属性。但是,如果要从单独的工作表中过滤数据,则必须重写 LoadFilter.startSheet 方法。在创建或处理 LoadFilter 时,请使用 LoadDataFilterOptions 枚举中的适当值。

LoadDataFilterOptions 枚举具有以下值。

在加载工作簿时筛选对象

以下示例代码演示了如何从工作簿中筛选图表。请查看此代码中使用的示例 Excel 文件和生成的输出 PDF。如您在输出 PDF 中所见,所有图表都已从工作簿中筛选出。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(FilterObjectsLoadingWorkbook.class) + "articles/";
//Filter charts from entire workbook
LoadOptions ldOpts = new LoadOptions();
ldOpts.setLoadFilter(new LoadFilter(LoadDataFilterOptions.ALL & ~LoadDataFilterOptions.CHART));
//Load the workbook with above filter
Workbook wb = new Workbook(dataDir + "sampleFilterCharts.xlsx", ldOpts);
//Save entire worksheet into a single page
PdfSaveOptions opts = new PdfSaveOptions();
opts.setOnePagePerSheet(true);
//Save the workbook in pdf format with the above pdf save options
wb.save(dataDir + "sampleFilterCharts.pdf", opts);

在加载工作表时过滤对象

以下示例代码加载源Excel文件并使用自定义筛选器从其工作表中过滤以下数据。

  • 它会从名为NoCharts的工作表中筛选图表。
  • 它会从名为NoShapes的工作表中筛选形状。
  • 它会从名为NoConditionalFormatting的工作表中筛选条件格式。

一旦带有自定义筛选器加载源Excel文件,它逐个获取所有工作表的图像。以下是供您参考的输出图像。正如您所见,第一个图像没有图表,第二个图像没有形状,第三个图像没有条件格式。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
public class FilterObjectsLoadingWorksheets {
// Implement your own custom load filter, it will enable you to filter your
// individual worksheet
class CustomLoadFilter extends LoadFilter {
public void startSheet(Worksheet sheet) {
if (sheet.getName().equals("NoCharts")) {
// Load everything and filter charts
this.setLoadDataFilterOptions(LoadDataFilterOptions.ALL& ~LoadDataFilterOptions.CHART);
}
if (sheet.getName().equals("NoShapes")) {
// Load everything and filter shapes
this.setLoadDataFilterOptions(LoadDataFilterOptions.ALL& ~LoadDataFilterOptions.DRAWING);
}
if (sheet.getName().equals("NoConditionalFormatting")) {
// Load everything and filter conditional formatting
this.setLoadDataFilterOptions(LoadDataFilterOptions.ALL& ~LoadDataFilterOptions.CONDITIONAL_FORMATTING);
}
}// End StartSheet method.
}// End CustomLoadFilter class.
public static void main(String[] args) throws Exception {
FilterObjectsLoadingWorksheets pg = new FilterObjectsLoadingWorksheets();
pg.Run();
}
public void Run() throws Exception {
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(FilterObjectsLoadingWorksheets.class) + "TechnicalArticles/";
// Filter worksheets using custom load filter
LoadOptions ldOpts = new LoadOptions();
ldOpts.setLoadFilter(new CustomLoadFilter());
// Load the workbook with above filter
Workbook wb = new Workbook(dataDir + "sampleFilterDifferentObjects.xlsx", ldOpts);
// Take the image of all worksheets one by one
for (int i = 0; i < wb.getWorksheets().getCount(); i++) {
// Access worksheet at index i
Worksheet ws = wb.getWorksheets().get(i);
// Create image or print options, we want the image of entire
// worksheet
ImageOrPrintOptions opts = new ImageOrPrintOptions();
opts.setOnePagePerSheet(true);
opts.setImageType(ImageType.PNG);
// Convert worksheet into image
SheetRender sr = new SheetRender(ws, opts);
sr.toImage(0, dataDir + ws.getName() + ".png");
}
}
}