Öppna filer med olika format

Öppna filer med olika format

Aspose.Cells tillåter utvecklare att öppna kalkylbladsfiler med olika format som SpreadsheetML, kommaseparerade värden (CSV), tabbavgränsade eller tabbavgränsade värden (TSV), ODS filer. För att öppna sådana filer kan utvecklare använda samma metod som de använder för att öppna filer av olika Microsoft Excel-versioner.

Öppnar SpreadsheetML Filer

SpreadsheetML-filer är XML-representationer av kalkylblad inklusive all information om det, såsom formatering, formler etc. Sedan Microsoft Excel XP har ett XML-exportalternativ lagts till i Microsoft Excel som exporterar dina kalkylblad till SpreadsheetML-filer.

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Opening XML Files
intrusive_ptr <Aspose::Cells::ILoadOptions> loadOptions = Factory::CreateILoadOptions(LoadFormat::LoadFormat_Xml);
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("Input.xml")), loadOptions);
// Show following message on console
Console::WriteLine(new String("XML file opened successfully!"));

Öppnar HTML Filer

Aspose.Cells låter dig öppna HTML-filen i ett arbetsboksobjekt. HTML-filen bör Microsoft Excel-orienterad, dvs MS-Excel ska kunna öppna den.

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Opening HTML Files
intrusive_ptr <Aspose::Cells::ILoadOptions> loadOptions = Factory::CreateILoadOptions(LoadFormat::LoadFormat_Html);
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("Input.html")), loadOptions);
// Show following message on console
Console::WriteLine(new String("HTML file opened successfully!"));

Öppnar 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 kommatecken och citattecken med dubbla citattecken. Om ett fältvärde innehåller ett dubbelt citattecken escapes det med ett par dubbla citattecken. Du kan också använda Microsoft Excel för att exportera kalkylbladsdata till CSV.

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Opening CSV Files
intrusive_ptr <Aspose::Cells::ILoadOptions> loadOptions = Factory::CreateILoadOptions(LoadFormat::LoadFormat_CSV);
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("Input.csv")), loadOptions);
// Show following message on console
Console::WriteLine(new String("CSV file opened successfully!"));
view raw OpenCSVFile.cpp hosted with ❤ by GitHub

Öppnar CSV-filer och ersätter ogiltiga tecken

I Excel, när CSV-filen med specialtecken öppnas, ersätts tecknen automatiskt. Detsamma görs av Aspose.Cells API vilket visas i kodexemplet nedan.

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Opening CSV Files
// Instantiate LoadOptions specified by the LoadFormat.
intrusive_ptr <Aspose::Cells::ITxtLoadOptions> loadOptions = Factory::CreateITxtLoadOptions(LoadFormat::LoadFormat_CSV);
loadOptions->SetSeparator(';');
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("InvalidCharacters.csv")), loadOptions);
// Show following message on console
Console::WriteLine(new String("CSV file opened successfully!"));
// Save for check
workbook->Save(new String("Output.xlsx"));

Exempel på källfil kan laddas ner från följande länkar för att testa den här funktionen.

InvalidCharacters.csv

Öppna textfiler med anpassad separator

Textfiler används för att hålla kalkylbladsdata utan formatering. Filen är en sorts vanlig textfil som kan ha några anpassade avgränsare.

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Set for TxtLoadOptions
intrusive_ptr <Aspose::Cells::ITxtLoadOptions> loadOptions = Factory::CreateITxtLoadOptions();
loadOptions->SetSeparator(',');
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("CustomSeparator.txt")), loadOptions);
// Show following message on console
Console::WriteLine(new String("TXT file opened successfully!"));
// Save for check
workbook->Save(new String("Output.xlsx"));

Exempel på källfil kan laddas ner från följande länkar för att testa den här funktionen.

CustomSeparator.txt

Öppnar flikavgränsade filer

Tabbavgränsad (text) fil innehåller kalkylbladsdata men utan någon formatering. Data är ordnade i rader och kolumner som i tabeller och kalkylblad. I grund och botten är en tabbavgränsad fil en speciell typ av vanlig textfil med en tabb mellan varje kolumn.

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Set LoadOptions
intrusive_ptr <Aspose::Cells::ILoadOptions> loadOptions = Factory::CreateILoadOptions(LoadFormat::LoadFormat_TabDelimited);
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("TabDelimited.txt")), loadOptions);
// Show following message on console
Console::WriteLine(new String("TabDelimited file opened successfully!"));
// Save for check
workbook->Save(new String("Output.xlsx"));

Öppna flikseparerade värden (TSV) filer

Filen med tabbavgränsade värden (TSV) innehåller kalkylbladsdata men utan någon formatering. Det är samma sak med Tab Delimited fil där data är ordnade i rader och kolumner som i tabeller och kalkylblad.

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Set LoadOptions
intrusive_ptr <Aspose::Cells::ILoadOptions> loadOptions = Factory::CreateILoadOptions(LoadFormat::LoadFormat_TSV);
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("Input.tsv")), loadOptions);
// Show following message on console
Console::WriteLine(new String("TSV file opened successfully!"));
// Save for check
workbook->Save(new String("Output.xlsx"));
view raw OpenTSVFile.cpp hosted with ❤ by GitHub

Öppnar SXC Filer

StarOffice Calc liknar Microsoft Excel och stöder formler, diagram, funktioner och makron. Kalkylbladen som skapas med denna programvara sparas med tillägget SXC. Filen SXC används också för OpenOffice.org Calc-kalkylbladsfiler. Aspose.Cells kan läsa SXC-filer som visas av följande kodexempel.

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Set LoadOptions
intrusive_ptr <Aspose::Cells::ILoadOptions> loadOptions = Factory::CreateILoadOptions(LoadFormat::LoadFormat_SXC);
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("Input.sxc")), loadOptions);
// Show following message on console
Console::WriteLine(new String("SXC file opened successfully!"));
// Save for check
workbook->Save(new String("Output.xlsx"));
view raw OpenSXCFile.cpp hosted with ❤ by GitHub

Öppnar FODS Filer

FODS-filen är ett kalkylblad som sparas i OpenDocument XML utan någon komprimering. Aspose.Cells kan läsa FODS-filer som visas av följande kodexempel.

#include "Aspose.Cells.h"
// The path to the documents directory.
StringPtr dataDir = new String("");
// Set LoadOptions
intrusive_ptr <Aspose::Cells::ILoadOptions> loadOptions = Factory::CreateILoadOptions(LoadFormat::LoadFormat_FODS);
// Create a Workbook object and opening the file from its path
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(
dataDir->StringAppend(new String("Input.fods")), loadOptions);
// Show following message on console
Console::WriteLine(new String("FODS file opened successfully!"));
// Save for check
workbook->Save(new String("Output.xlsx"));