Konvertieren Sie CSV, TSV und TXT in Excel

Öffnen von CSV-Dateien

Dateien mit kommagetrennten Werten (CSV) enthalten Datensätze, in denen die Werte durch Kommas getrennt sind. Die Daten werden als Tabelle gespeichert, in der jede Spalte durch das Kommazeichen getrennt und durch das doppelte Anführungszeichen in Anführungszeichen gesetzt wird. Wenn ein Feldwert ein doppeltes Anführungszeichen enthält, wird es mit einem Paar doppelter Anführungszeichen maskiert. Sie können auch Microsoft Excel verwenden, um Tabellendaten nach 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

Wenn in Excel eine CSV-Datei mit Sonderzeichen geöffnet wird, werden die Zeichen automatisch ersetzt. Dasselbe wird von Aspose.Cells API durchgeführt, was in dem unten angegebenen 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();
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

Dies ist nicht immer erforderlich, um die Standard-Parser-Einstellungen zum Öffnen der CSV-Dateien zu verwenden. Manchmal erzeugt das Importieren der Datei CSV nicht die erwartete Ausgabe, da das Datumsformat nicht wie erwartet ist oder leere Felder anders behandelt werden. Für diesen ZweckTxtLoadOptions.PreferredParsersist verfügbar, um einen eigenen bevorzugten Parser bereitzustellen, um verschiedene Datentypen gemäß den Anforderungen zu analysieren. Der folgende Beispielcode demonstriert die Verwendung des bevorzugten Parsers.

Beispiel-Quelldatei und Ausgabedateien können von den folgenden Links heruntergeladen werden, um diese Funktion zu testen.

samplePreferredParser.csv

AusgabebeispielPreferredParser.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 Tabellenkalkulationsdaten ohne Formatierung zu speichern. Die Datei ist eine Art einfache 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");

Tabulatorgetrennte Dateien öffnen

Tabulatorgetrennte (Text-)Datei enthält Tabellenkalkulationsdaten, jedoch ohne Formatierung. Daten werden wie in Tabellen und Tabellenkalkulationen in Zeilen und Spalten angeordnet. Grundsätzlich ist eine tabulatorgetrennte Datei eine spezielle Art von einfacher Textdatei mit einem Tabulator zwischen jeder Spalte.

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

Dateien mit tabulatorgetrennten Werten (TSV) öffnen

Die Datei mit tabulatorgetrennten Werten (TSV) enthält Tabellendaten, jedoch ohne Formatierung. Das Gleiche gilt für tabulatorgetrennte Dateien, in denen 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);

Themen vorantreiben