打开文件的不同方式
Contents
[
Hide
]
使用 Aspose.Cells 可以轻松打开文件,例如检索数据,或使用设计器模板来加快开发过程。
通过路径打开文件
开发人员可以通过在本地计算机上指定它的文件路径来打开 Microsoft Excel 文件**工作簿**类构造函数。只需将构造函数中的路径作为*细绳*Aspose.Cells 会自动检测文件格式类型。
This file contains hidden or 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 | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening through Path | |
// Creating a Workbook object and opening an Excel file using its file path | |
Workbook workbook1 = new Workbook(dataDir + "Book1.xlsx"); | |
Console.WriteLine("Workbook opened using path successfully!"); |
通过流打开文件
将 Excel 文件作为流打开也很简单。为此,请使用构造函数的重载版本,该版本采用溪流包含文件的对象。
This file contains hidden or 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 | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening through Stream | |
// Create a Stream object | |
FileStream fstream = new FileStream(dataDir + "Book2.xls", FileMode.Open); | |
// Creating a Workbook object, open the file from a Stream object | |
// That contains the content of file and it should support seeking | |
Workbook workbook2 = new Workbook(fstream); | |
Console.WriteLine("Workbook opened using stream successfully!"); | |
fstream.Close(); |
打开一个只有数据的文件
要打开仅包含数据的文件,请使用**加载选项**和**加载过滤器**classes 为要加载的模板文件设置类的相关属性和选项。
This file contains hidden or 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 | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Load only specific sheets with data and formulas | |
// Other objects, items etc. would be discarded | |
// Instantiate LoadOptions specified by the LoadFormat | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.Xlsx); | |
// Set LoadFilter property to load only data & cell formatting | |
loadOptions.LoadFilter = new LoadFilter(LoadDataFilterOptions.CellData); | |
// Create a Workbook object and opening the file from its path | |
Workbook book = new Workbook(dataDir + "Book1.xlsx", loadOptions); | |
Console.WriteLine("File data imported successfully!"); |
仅加载可见表
在加载一个**工作簿**有时您可能只需要工作簿中可见工作表中的数据。 Aspose.Cells 允许您在加载工作簿时跳过不可见工作表中的数据。为此,创建一个继承**加载过滤器**类并将其实例传递给**LoadOptions.LoadFilter**财产。
This file contains hidden or 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 | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string sampleFile = "output.xlsx"; | |
string samplePath = dataDir + sampleFile; | |
// Create a sample workbook | |
// and put some data in first cell of all 3 sheets | |
Workbook createWorkbook = new Workbook(); | |
createWorkbook.Worksheets["Sheet1"].Cells["A1"].Value = "Aspose"; | |
createWorkbook.Worksheets.Add("Sheet2").Cells["A1"].Value = "Aspose"; | |
createWorkbook.Worksheets.Add("Sheet3").Cells["A1"].Value = "Aspose"; | |
createWorkbook.Worksheets["Sheet3"].IsVisible = false; | |
createWorkbook.Save(samplePath); | |
// Load the sample workbook | |
LoadOptions loadOptions = new LoadOptions(); | |
loadOptions.LoadFilter = new CustomLoad(); | |
Workbook loadWorkbook = new Workbook(samplePath, loadOptions); | |
Console.WriteLine("Sheet1: A1: {0}", loadWorkbook.Worksheets["Sheet1"].Cells["A1"].Value); | |
Console.WriteLine("Sheet1: A2: {0}", loadWorkbook.Worksheets["Sheet2"].Cells["A1"].Value); | |
Console.WriteLine("Sheet1: A3: {0}", loadWorkbook.Worksheets["Sheet3"].Cells["A1"].Value); |
下面是实现自定义加载上面代码段中引用的类。
This file contains hidden or 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 | |
class CustomLoad : LoadFilter | |
{ | |
public override void StartSheet(Worksheet sheet) | |
{ | |
if (sheet.IsVisible) | |
{ | |
// Load everything from visible worksheet | |
this.LoadDataFilterOptions = LoadDataFilterOptions.All; | |
} | |
else | |
{ | |
// Load nothing | |
this.LoadDataFilterOptions = LoadDataFilterOptions.Structure; | |
} | |
} | |
} |
Aspose.Cells尝试打开非原生Excel文件或其他文件格式(例如PPT/PPTX、DOC/DOCX等)会抛出异常。
很有可能**工作簿**构造函数可能会抛出*System.OutOfMemoryException异常*在加载大型电子表格时。此异常表明可用内存不足以将电子表格完全加载到内存中,因此必须在启用内存首选项时加载电子表格。