Filter Objects while loading Workbook or Worksheet
Possible Usage Scenarios
Please use LoadOptions.LoadFilter property while filtering data from the workbook. But if you want to filter data from individual worksheets, then you will have to override LoadFilter.startSheet method. Please provide the appropriate value from the LoadDataFilterOptions enumeration while creating or working with LoadFilter.
The LoadDataFilterOptions enumeration has the following values.
- NONE
- ALL
- CELL_BLANK
- CELL_STRING
- CELL_NUMERIC
- CELL_ERROR
- CELL_BOOL
- CELL_VALUE
- FORMULA
- CELL_DATA
- CHART
- SHAPE
- MERGED_AREA
- CONDITIONAL_FORMATTING
- DATA_VALIDATION
- PIVOT_TABLE
- TABLE
- HYPERLINKS
- SHEET_SETTINGS
- SHEET_DATA
- BOOK_SETTINGS
- SETTINGS
- XML_MAP
- STRUCTURE
- DOCUMENT_PROPERTIES
- DEFINED_NAMES
- VBA
- STYLE
Filter Objects while Loading Workbook
The following sample code illustrates how to filter charts from the workbook. Please check the sample excel file used in this code and the output PDF generated by it. As you can see in the output PDF, all charts have been filtered out of the workbook.
// 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); |
Filter Objects while Loading Worksheet
The following sample code loads the source excel file and filters the following data from its worksheets using a custom filter.
- It filters Charts from worksheet named NoCharts.
- It filters Shapes from worksheet named NoShapes.
- It filters Conditional Formatting from worksheet named NoConditionalFormatting.
Once, it loads the source excel file with a custom filter, it takes the images of all worksheets one by one. Here are the output images for your reference. As you can see, the first image does not have charts, the second image does not have shapes and the third image does not have conditional formatting.
// 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"); | |
} | |
} | |
} |