Convert CSV, TSV and TXT to Excel

Opening CSV Files

Comma Separated Values (CSV) files contain records where the values are separated by commas. Data is stored as a table where each column is separated by the comma character and quoted by the double quote character. If a field value contains a double quote character it is escaped with a pair of double quote characters. You can also use Microsoft Excel to export spreadsheet data to CSV.

from aspose.cells import LoadFormat, LoadOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions4 = LoadOptions(LoadFormat.CSV)
# Create a Workbook object and opening the file from its path
wbCSV = Workbook(dataDir + "Book_CSV.csv", loadOptions4)
print("CSV file opened successfully!")

Opening CSV files and replacing invalid characters

In Excel, when CSV file with special characters is opened, the characters are automatically replaced. The same is done by Aspose.Cells API which is demonstrated in the code example given below.

from aspose.cells import LoadDataFilterOptions, LoadFilter, TxtLoadOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
filename = sourceDir + "[20180220142533][ASPOSE_CELLS_TEST].csv"
options = TxtLoadOptions()
options.separator = ';'
options.load_filter = LoadFilter(LoadDataFilterOptions.CELL_DATA)
options.check_excel_restriction = False
options.convert_numeric_data = False
options.convert_date_time_data = false
# Load CSV file
workbook = Workbook(filename, options)
print(workbook.worksheets[0].name)
print(len(workbook.worksheets[0].name))
print("CSV file opened successfully!")

Opening Tab Delimited Files

Tab delimited (Text) file contains spreadsheet data but without any formatting. Data is arranged in rows and columns like in tables and spreadsheets. Basically, a tab delimited file is a special kind of plain text file with a tab between each column.

from aspose.cells import LoadFormat, LoadOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Opening Tab Delimited Files
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions5 = LoadOptions(LoadFormat.TAB_DELIMITED)
# Create a Workbook object and opening the file from its path
wbTabDelimited = Workbook(dataDir + "Book1TabDelimited.txt", loadOptions5)
print("Tab delimited file opened successfully!")

Opening Tab-Separated Values (TSV) Files

Tab-separated values (TSV) file contains spreadsheet data but without any formatting. It is the same with Tab Delimited file where data is arranged in rows and columns like in tables and spreadsheets.

from aspose.cells import LoadFormat, LoadOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Instantiate LoadOptions specified by the LoadFormat.
loadOptions = LoadOptions(LoadFormat.TSV)
# Create a Workbook object and opening the file from its path
workbook = Workbook(sourceDir + "SampleTSVFile.tsv", loadOptions)
# Using the Sheet 1 in Workbook
worksheet = workbook.worksheets[0]
# Accessing a cell using its name
cell = worksheet.cells.get("C3")
print("Cell Name: " + cell.name + " Value: " + cell.string_value)

Advance topics