在加载工作簿或工作表时过滤对象
可能的使用场景
在从工作簿中过滤数据时,请使用LoadOptions.LoadFilter属性。但如果要从单个工作表中过滤数据,则必须覆盖LoadFilter.StartSheet方法。在创建或使用LoadFilter时,请根据需要提供LoadDataFilterOptions枚举的适当值。
枚举 LoadDataFilterOptions 具有以下可能的值。
- 所有
- 文档设置
- 单元格空白
- 单元格布尔
- 单元格数据
- 单元格错误
- 单元格数值
- 单元格字符串
- 单元格值
- Chart
- 条件格式
- 数据验证
- 定义名称
- 文档属性
- 公式
- 超链接
- 合并区域
- 数据透视表
- 设置
- 形状
- 表单数据
- 表格设置
- 结构
- 样式
- 表
- VBA
- Xml映射
加载工作簿时过滤对象
以下示例代码说明了如何从工作簿中筛选图表。请查看此代码中使用的示例excel文件和由此生成的输出PDF。从输出PDF中可以看出,所有图表都已从工作簿中筛选出。
// 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); |
加载工作表时过滤对象
以下示例代码加载了源excel文件,并使用自定义过滤器从其工作表中筛选以下数据。
- 它会从名为NoCharts的工作表中筛选图表。
- 它会从名为NoShapes的工作表中筛选形状。
- 它会从名为NoConditionalFormatting的工作表中筛选条件格式。
一旦使用自定义过滤器加载了源excel文件,它会逐个工作表地获取所有工作表的图像。以下是用于参考的输出图像。可以看出,第一张图像没有图表,第二张图像没有形状,第三张图像没有条件格式。
// 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; | |
} | |
} | |
} |
这是如何根据工作表名称使用CustomLoadFilter类。
// 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"); | |
} | |
} |