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 the LoadFilter.StartSheet method. Please provide appropriate value from the LoadDataFilterOptions enumeration while creating or working with LoadFilter.
The LoadDataFilterOptions enumeration has the following possible values.
- All
- BookSettings
- CellBlank
- CellBool
- CellData
- CellError
- CellNumeric
- CellString
- CellValue
- Chart
- ConditionalFormatting
- DataValidation
- DefinedNames
- DocumentProperties
- Formula
- Hyperlinks
- MergedArea
- PivotTable
- Settings
- Shape
- SheetData
- SheetSettings
- Structure
- Style
- Table
- VBA
- XmlMap
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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Filter charts from the workbook. | |
LoadOptions lOptions = new LoadOptions(); | |
lOptions.LoadFilter = new LoadFilter(LoadDataFilterOptions.All & ~LoadDataFilterOptions.Chart); | |
// Load the workbook with above filter. | |
Workbook workbook = new Workbook(dataDir + "sampleFilterCharts.xlsx", lOptions); | |
// Save worksheet to a single PDF page. | |
PdfSaveOptions pOptions = new PdfSaveOptions(); | |
pOptions.OnePagePerSheet = true; | |
// Save the workbook in PDF format. | |
workbook.Save(dataDir + "sampleFilterCharts.pdf", pOptions); |
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-.NET | |
public class CustomLoadFilter : LoadFilter | |
{ | |
public override void StartSheet(Worksheet sheet) | |
{ | |
if (sheet.Name == "NoCharts") | |
{ | |
//Load everything and filter charts | |
this.LoadDataFilterOptions = LoadDataFilterOptions.All & ~LoadDataFilterOptions.Chart; | |
} | |
if (sheet.Name == "NoShapes") | |
{ | |
//Load everything and filter shapes | |
this.LoadDataFilterOptions = LoadDataFilterOptions.All & ~LoadDataFilterOptions.Drawing; | |
} | |
if (sheet.Name == "NoConditionalFormatting)") | |
{ | |
//Load everything and filter conditional formatting | |
this.LoadDataFilterOptions = LoadDataFilterOptions.All & ~LoadDataFilterOptions.ConditionalFormatting; | |
} | |
} | |
} |
This is how to use the CustomLoadFilter class as per worksheet names.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
public static void Run() | |
{ | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Filter worksheets using CustomLoadFilter class | |
LoadOptions loadOpts = new LoadOptions(); | |
loadOpts.LoadFilter = new CustomLoadFilter(); | |
// Load the workbook with filter defined in CustomLoadFilter class | |
Workbook workbook = new Workbook(sourceDir + "sampleCustomFilteringPerWorksheet.xlsx", loadOpts); | |
// Take the image of all worksheets one by one | |
for (int i = 0; i < workbook.Worksheets.Count; i++) | |
{ | |
// Access worksheet at index i | |
Worksheet worksheet = workbook.Worksheets[i]; | |
// Create an instance of ImageOrPrintOptions | |
// Render entire worksheet to image | |
ImageOrPrintOptions imageOpts = new ImageOrPrintOptions(); | |
imageOpts.OnePagePerSheet = true; | |
imageOpts.ImageType = Drawing.ImageType.Png; | |
// Convert worksheet to image | |
SheetRender render = new SheetRender(worksheet, imageOpts); | |
render.ToImage(0, outputDir + "outputCustomFilteringPerWorksheet_" + worksheet.Name + ".png"); | |
} | |
} |