Öppning av filer med olika format
Med Aspose.Cells för Python via .NET kan du öppna filer med olika format. Aspose.Cells för Python via .NET kan öppna ett brett utbud av filformat såsom Microsoft Excel-kalkylblad (XLS, XLSX, XLSM, XLSB), SpreadsheetML, kommaseparerade värden (CSV), tabbseparerade eller tab-separerade värden (TSV) filer etc.
Om du behöver veta alla stödda filformat, se följande sidor: Supported File Formats
Öppna filer med olika format
Aspose.Cells för Python via .NET tillåter utvecklare att öppna kalkylbladsfiler med olika format som SpreadsheetML, kommaseparerade värden (CSV), tabbseparaterade eller tab-separerade värden (TSV), ODS-filer. För att öppna sådana filer kan utvecklare använda samma metod som för att öppna filer av olika Microsoft Excel-versioner.
Öppning av SpreadsheetML Filer
SpreadsheetML-filer är XML-representationer av kalkylblad som innehåller all information om det, som formatering, formler etc. Sedan Microsoft Excel XP har en XML export-alternativ lagts till i Microsoft Excel som exporterar dina kalkylblad till SpreadsheetML-filer.
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 SpreadsheetML Files | |
# Instantiate LoadOptions specified by the LoadFormat. | |
loadOptions3 = LoadOptions(LoadFormat.SPREADSHEET_ML) | |
# Create a Workbook object and opening the file from its path | |
wbSpreadSheetML = Workbook(dataDir + "Book3.xml", loadOptions3) | |
print("SpreadSheetML file opened successfully!") |
Öppning av HTML filer
Aspose.Cells för Python via .NET tillåter dig att öppna HTML-filer i Workbook-objektet. HTML-filen ska vara Microsoft Excel-inriktad, dvs MS-Excel ska kunna öppna den.
from aspose.cells import HtmlLoadOptions, LoadFormat, 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(".") | |
filePath = dataDir + "Book1.html" | |
# Instantiate LoadOptions specified by the LoadFormat. | |
loadOptions = HtmlLoadOptions(LoadFormat.HTML) | |
# Create a Workbook object and opening the file from its path | |
wb = Workbook(filePath, loadOptions) | |
# Save the MHT file | |
wb.save(filePath + "output.xlsx") |
Öppning av CSV-filer
Kommaseparerade värden (CSV) filer innehåller poster där värdena är separerade med kommatecken. Data lagras som en tabell där varje kolumn separeras med kommatecknet och citeras med dubbelcitat-tecknet. Om en fältvärde innehåller ett dubbelcitat-tecken escapas det med ett par av dubbelcitat-tecken. Du kan också använda Microsoft Excel för att exportera kalkylbladsdata till 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!") |
Öppna CSV-filer och ersätt ogiltiga tecken
När du i Excel öppnar en CSV-fil med specialtecken ersätts tecknen automatiskt. Detsamma gör Aspose.Cells för Python via .NET API, vilket demonstreras i kodexemplet nedan.
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!") |
Öppning av tabbehållna filer
Tabbehållna (text) filer innehåller kalkyldata men utan någon formatering. Data är ordnad i rader och kolumner som i tabeller och kalkylblad. En tabbehållen fil är i grund och botten en särskild typ av ren textfil med ett tabstopp mellan varje kolumn.
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!") |
Öppning av tabseparatorvärdefiler (TSV-filer)
Tabseparatorvärdefiler (TSV-filer) innehåller kalkyldata men utan någon formatering. De är samma som tabbehållna filer där data är ordnad i rader och kolumner som i tabeller och kalkylblad.
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) |
Öppning av SXC-filer
StarOffice Calc liknar Microsoft Excel och stöder formler, diagram, funktioner och makron. Kalkylblad skapade med denna programvara sparas med SXC-ändelsen. SXC-filen används även för OpenOffice.org Calc kalkylbladsfiler. Aspose.Cells för Python via .NET kan läsa SXC-filer som demonstreras i följande kodexempel.
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.SXC) | |
# Create a Workbook object and opening the file from its path | |
workbook = Workbook(sourceDir + "SampleSXC.sxc", 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) |
Öppning av FODS-filer
FODS-fil är ett kalkylblad sparat i OpenDocument XML utan komprimering. Aspose.Cells för Python via .NET kan läsa FODS-filer som demonstreras i följande kodexempel.
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.FODS) | |
# Create a Workbook object and opening the file from its path | |
workbook = Workbook(sourceDir + "SampleFods.fods", loadOptions) | |
print("FODS file opened successfully!") |