Verschiedene Möglichkeiten zum Öffnen von Dateien

Öffnen einer Datei über einen Pfad

Entwickler können eine Microsoft-Excel-Datei mit ihrem Dateipfad auf dem lokalen Computer öffnen, indem sie ihn in der Datei angeben**Arbeitsmappe**Klassenkonstrukteur. Übergeben Sie den Pfad einfach im Konstruktor als a*Schnur*. Aspose.Cells erkennt automatisch den Dateiformattyp.

// 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!");

Öffnen einer Datei über einen Stream

Es ist auch einfach, eine Excel-Datei als Stream zu öffnen. Verwenden Sie dazu eine überladene Version des Konstruktors, der die akzeptiertStromObjekt, das die Datei enthält.

// 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();

Öffnen einer Datei nur mit Daten

Um eine Datei nur mit Daten zu öffnen, verwenden Sie die**Ladeoptionen** und**LoadFilter**Klassen, um die zugehörigen Attribute und Optionen der Klassen für die zu ladende Vorlagendatei festzulegen.

// 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!");

Nur sichtbare Blätter laden

Beim Laden a**Arbeitsmappe**Manchmal benötigen Sie möglicherweise nur Daten in sichtbaren Arbeitsblättern in einer Arbeitsmappe. Aspose.Cells ermöglicht es Ihnen, Daten in unsichtbaren Arbeitsblättern zu überspringen, während Sie eine Arbeitsmappe laden. Erstellen Sie dazu eine benutzerdefinierte Funktion, die die erbt**LoadFilter**Klasse und übergeben Sie ihre Instanz an**LoadOptions.LoadFilter**Eigentum.

// 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);

Hier ist die Umsetzung derCustomnLoadKlasse, auf die im obigen Snippet verwiesen wird.

// 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;
}
}
}