Öffnen von Dateien mit verschiedenen Formaten
Mit Aspose.Cells können Sie Dateien mit verschiedenen Formaten öffnen. Aspose.Cells kann eine Vielzahl von Dateiformaten wie Microsoft Excel-Tabellen (XLS, XLSX, XLSM, XLSB), SpreadsheetML, durch Kommas getrennte Werte (CSV), Tab- oder Tabstopp-getrennte Werte (TSV)-Dateien usw. öffnen.
Wenn Sie alle unterstützten Dateiformate kennen müssen, verweisen Sie bitte auf die folgenden Seiten: Unterstützte Dateiformate
Öffnen von Dateien mit verschiedenen Formaten
Aspose.Cells ermöglicht Entwicklern, Tabellendateien mit verschiedenen Formaten wie SpreadsheetML, durch Kommas getrennte Werte (CSV), Tab- oder Tabstopp-getrennte Werte (TSV), ODS-Dateien zu öffnen. Um solche Dateien zu öffnen, können Entwickler die gleiche Methodik verwenden wie beim Öffnen von Dateien verschiedener Microsoft Excel-Versionen.
Öffnen von SpreadsheetML-Dateien
SpreadsheetML-Dateien sind XML-Repräsentationen von Tabellen, die alle Informationen darüber enthalten, wie z.B. Formatierung, Formeln usw. Seit Microsoft Excel XP gibt es eine XML-Exportoption in Microsoft Excel, die Ihre Tabellen als SpreadsheetML-Dateien exportiert.
// 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 SpreadsheetML Files | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions3 = new LoadOptions(LoadFormat.SpreadsheetML); | |
// Create a Workbook object and opening the file from its path | |
Workbook wbSpreadSheetML = new Workbook(dataDir + "Book3.xml", loadOptions3); | |
Console.WriteLine("SpreadSheetML file opened successfully!"); |
Öffnen von HTML-Dateien
Aspose.Cells ermöglicht es Ihnen, eine HTML-Datei in ein Arbeitsmappenobjekt zu öffnen. Die HTML-Datei sollte auf Microsoft Excel ausgerichtet sein, d.h. MS-Excel sollte in der Lage sein, sie zu öffnen.
// 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 filePath = dataDir + "Book1.html"; | |
// Instantiate LoadOptions specified by the LoadFormat. | |
HtmlLoadOptions loadOptions = new HtmlLoadOptions(LoadFormat.Html); | |
// Create a Workbook object and opening the file from its path | |
Workbook wb = new Workbook(filePath, loadOptions); | |
// Save the MHT file | |
wb.Save(filePath + "output.xlsx"); |
Öffnen von CSV-Dateien
Durch Kommas getrennte Werte (CSV)-Dateien enthalten Datensätze, bei denen die Werte durch Kommas getrennt sind. Daten werden als Tabelle gespeichert, wobei jeder Spalte das Kommazeichen trennt und durch das doppelte Anführungszeichen gekennzeichnet ist. Enthält ein Feldwert ein doppeltes Anführungszeichen, wird es mit einem Paar doppelter Anführungszeichen maskiert. Sie können auch Microsoft Excel verwenden, um Tabellendaten in CSV zu exportieren.
// 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); | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions4 = new LoadOptions(LoadFormat.Csv); | |
// Create a Workbook object and opening the file from its path | |
Workbook wbCSV = new Workbook(dataDir + "Book_CSV.csv", loadOptions4); | |
Console.WriteLine("CSV file opened successfully!"); |
Öffnen von CSV-Dateien und Ersetzen ungültiger Zeichen
In Excel werden beim Öffnen von CSV-Dateien mit Sonderzeichen die Zeichen automatisch ersetzt. Das gleiche geschieht durch die Aspose.Cells API, wie im folgenden Beispielcode dargestellt.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
var filename = sourceDir + "[20180220142533][ASPOSE_CELLS_TEST].csv"; | |
//Load CSV file | |
var workbook = new Workbook(filename, new TxtLoadOptions() { Separator = ';', LoadFilter = new LoadFilter(LoadDataFilterOptions.CellData), CheckExcelRestriction = false, ConvertNumericData = false, ConvertDateTimeData = false }); | |
Console.WriteLine(workbook.Worksheets[0].Name); // (20180220142533)(ASPOSE_CELLS_T | |
Console.WriteLine(workbook.Worksheets[0].Name.Length); // 31 | |
Console.WriteLine("CSV file opened successfully!"); |
Verwenden des bevorzugten Parsers
Es ist nicht immer erforderlich, die Standardeinstellungen des Parsers für das Öffnen von CSV-Dateien zu verwenden. Manchmal erzeugt das Importieren einer CSV-Datei nicht das erwartete Ergebnis, beispielsweise wird das Datumsformat nicht wie erwartet oder leere Felder werden anders behandelt. Hierfür steht TxtLoadOptions.PreferredParsers zur Verfügung, um einen bevorzugten Parser bereitzustellen, der verschiedene Datentypen entsprechend den Anforderungen analysiert. Das folgende Beispielskript zeigt die Verwendung des bevorzugten Parsers.
Die Quell- und Ausgabedateien können von den folgenden Links heruntergeladen werden, um diese Funktion zu testen.
outputsamplePreferredParser.xlsx
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
class TextParser : ICustomParser | |
{ | |
public object ParseObject(string value) | |
{ | |
return value; | |
} | |
public string GetFormat() | |
{ | |
return ""; | |
} | |
} | |
class DateParser : ICustomParser | |
{ | |
public object ParseObject(string value) | |
{ | |
DateTime myDate = DateTime.ParseExact(value, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture); | |
return myDate; | |
} | |
public string GetFormat() | |
{ | |
return "dd/MM/yyyy"; | |
} | |
} | |
public static void Main() | |
{ | |
// Initialize Text File's LoadFormat | |
LoadFormat oLoadFormat = LoadFormat.Csv; | |
// Initialize Text File's Load options | |
TxtLoadOptions oTxtLoadOptions = new TxtLoadOptions(oLoadFormat); | |
// Specify the separatot character | |
oTxtLoadOptions.Separator = Convert.ToChar(","); | |
// Specify the encoding scheme | |
oTxtLoadOptions.Encoding = System.Text.Encoding.UTF8; | |
// Set the flag to true for converting datetime data | |
oTxtLoadOptions.ConvertDateTimeData = true; | |
// Set the preferred parsers | |
oTxtLoadOptions.PreferredParsers = new ICustomParser[] { new TextParser(), new DateParser() }; | |
// Initialize the workbook object by passing CSV file and text load options | |
Workbook oExcelWorkBook = new Aspose.Cells.Workbook(sourceDir + "samplePreferredParser.csv", oTxtLoadOptions); | |
// Get the first cell | |
Cell oCell = oExcelWorkBook.Worksheets[0].Cells["A1"]; | |
// Display type of value | |
Console.WriteLine("A1: " + oCell.Type.ToString() + " - " + oCell.DisplayStringValue); | |
// Get the second cell | |
oCell = oExcelWorkBook.Worksheets[0].Cells["B1"]; | |
// Display type of value | |
Console.WriteLine("B1: " + oCell.Type.ToString() + " - " + oCell.DisplayStringValue); | |
// Save the workbook to disc | |
oExcelWorkBook.Save(outputDir + "outputsamplePreferredParser.xlsx"); | |
Console.WriteLine("OpeningCSVFilesWithPreferredParser executed successfully.\r\n"); | |
} |
Öffnen von Textdateien mit benutzerdefiniertem Trennzeichen
Textdateien werden verwendet, um Tabellendaten ohne Formatierung zu halten. Die Datei ist eine Art reine Textdatei, die einige benutzerdefinierte Trennzeichen haben kann.
// 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 filePath = dataDir + "Book11.csv"; | |
// Instantiate Text File's LoadOptions | |
TxtLoadOptions txtLoadOptions = new TxtLoadOptions(); | |
// Specify the separator | |
txtLoadOptions.Separator = Convert.ToChar(","); | |
// Specify the encoding type | |
txtLoadOptions.Encoding = System.Text.Encoding.UTF8; | |
// Create a Workbook object and opening the file from its path | |
Workbook wb = new Workbook(filePath, txtLoadOptions); | |
// Save file | |
wb.Save(dataDir+ "output.txt"); |
Öffnen von tabstoppgetrennten Dateien
Eine tabstoppgetrennte (Text)Datei enthält Tabellendaten, jedoch ohne jegliche Formatierung. Die Daten sind in Zeilen und Spalten wie in Tabellen und Tabellenkalkulationen angeordnet. Im Grunde genommen ist eine tabstoppgetrennte Datei eine spezielle Art einer einfachen Textdatei, bei der ein Tabulator zwischen jeder Spalte steht.
// 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 Tab Delimited Files | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions5 = new LoadOptions(LoadFormat.TabDelimited); | |
// Create a Workbook object and opening the file from its path | |
Workbook wbTabDelimited = new Workbook(dataDir + "Book1TabDelimited.txt", loadOptions5); | |
Console.WriteLine("Tab delimited file opened successfully!"); |
Öffnen von tabstoppgetrennten Werten (TSV) Dateien
Eine tabstoppgetrennte Werte (TSV) Datei enthält Tabellendaten, jedoch ohne jegliche Formatierung. Es ist dasselbe wie bei einer tabstoppgetrennten Datei, bei der die Daten in Zeilen und Spalten wie in Tabellen und Tabellenkalkulationen angeordnet sind.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.Tsv); | |
// Create a Workbook object and opening the file from its path | |
Workbook workbook = new Workbook(sourceDir + "SampleTSVFile.tsv", loadOptions); | |
// Using the Sheet 1 in Workbook | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Accessing a cell using its name | |
Cell cell = worksheet.Cells["C3"]; | |
Console.WriteLine("Cell Name: " + cell.Name + " Value: " + cell.StringValue); | |
Öffnen von SXC Dateien
StarOffice Calc ist ähnlich wie Microsoft Excel und unterstützt Formeln, Diagramme, Funktionen und Makros. Die mit dieser Software erstellten Tabellenkalkulationen werden mit der SXC-Erweiterung gespeichert. Die SXC-Datei wird auch für OpenOffice.org Calc-Tabellenkalkulationsdateien verwendet. Aspose.Cells kann SXC-Dateien lesen, wie im folgenden Codebeispiel demonstriert wird.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.Sxc); | |
// Create a Workbook object and opening the file from its path | |
Workbook workbook = new Workbook(sourceDir + "SampleSXC.sxc", loadOptions); | |
// Using the Sheet 1 in Workbook | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Accessing a cell using its name | |
Cell cell = worksheet.Cells["C3"]; | |
Console.WriteLine("Cell Name: " + cell.Name + " Value: " + cell.StringValue); | |
Öffnen von FODS Dateien
Eine FODS-Datei ist eine in OpenDocument XML ohne jegliche Komprimierung gespeicherte Tabelle. Aspose.Cells kann FODS-Dateien lesen, wie im folgenden Codebeispiel demonstriert wird.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.Fods); | |
// Create a Workbook object and opening the file from its path | |
Workbook workbook = new Workbook(sourceDir + "SampleFods.fods", loadOptions); | |
Console.WriteLine("FODS file opened successfully!"); | |