使用 CustomImplementationFactory 创建内存流的自定义实现
Contents
[
Hide
]
可能的使用场景
Aspose.Cells 提供了一个名为 APICellsHelper.CustomImplementationFactory这使用户能够提供自定义实现,例如使用可回收内存实现而不是默认的 MemoryStream。
使用 CustomImplementationFactory 创建内存流的自定义实现
下面的示例代码说明了如何使用CellsHelper.CustomImplementationFactory在你的程序中。有时,系统中有足够的内存,但内存不连续。 Memory Stream 对象使用连续内存,但您可以提供 Memory Stream 的实现,使其使用非连续内存,
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Implement CustomImplementationFactory - CELLSNET-45461 | |
public class MM : CustomImplementationFactory | |
{ | |
RecyclableMemoryStreamManager manager = new RecyclableMemoryStreamManager(); | |
public override MemoryStream CreateMemoryStream() | |
{ | |
return manager.GetStream("MM"); | |
} | |
public override MemoryStream CreateMemoryStream(int capacity) | |
{ | |
return manager.GetStream("MM", capacity); | |
} | |
} | |
//---------------------------------------- | |
//---------------------------------------- | |
//Assign implementation instance of CustomImplementationFactory | |
CellsHelper.CustomImplementationFactory = new MM(); | |
//Light cells data handler implementation | |
LightCellsDataHandlerVisitCells v = new LightCellsDataHandlerVisitCells(); | |
//Create workbook object | |
Workbook wb = new Workbook(sourceDir, new LoadOptions() { MemorySetting = MemorySetting.MemoryPreference, LightCellsDataHandler = v }); | |
//Print miscellaneous data | |
Console.WriteLine("Total sheets: " + wb.Worksheets.Count + ", cells: " + v.CellCount + ", strings: " + v.StringCount + ", formulas: " + v.FormulaCount); |