Opening Files with Different Formats

Opening Files with Different Formats

Aspose.Cells allows developers to open spreadsheet files with different formats such as SpreadsheetML, Comma-separated values (CSV), Tab Delimited or Tab-separated values (TSV), ODS files. To open such files, developers can use the same methodology as they use for opening files of different Microsoft Excel versions.

Opening SpreadsheetML Files

SpreadsheetML files are XML representations of spreadsheets including all information about it, such as formatting, formulae etc. Since Microsoft Excel XP, an XML export option is added to Microsoft Excel that exports your spreadsheets to SpreadsheetML files.

Aspose::Cells::Startup();
// The path to the documents directory.
U16String dataDir(u"");
// Opening XML Files
LoadOptions loadOptions(LoadFormat::Xml);
// Create a Workbook object and opening the file from its path
Workbook workbook(dataDir + u"Input.xml", loadOptions);
// Show following message on console
std::cout << "XML file opened successfully!" <<std::endl;
Aspose::Cells::Cleanup();

Opening HTML Files

Aspose.Cells allows you to open HTML file into Workbook object. The HTML file should Microsoft Excel oriented i.e MS-Excel should be able to open it.

Aspose::Cells::Startup();
// The path to the documents directory.
U16String dataDir(u"");
// Opening HTML Files
LoadOptions loadOptions(LoadFormat::Html);
// Create a Workbook object and opening the file from its path
Workbook workbook(dataDir + u"Input.html", loadOptions);
// Show following message on console
std::cout << "HTML file opened successfully!" << std::endl;
Aspose::Cells::Cleanup();

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.

Aspose::Cells::Startup();
// The path to the documents directory.
U16String dataDir = u"";
// Instantiate LoadOptions specified by the LoadFormat.
LoadOptions loadOptions4(LoadFormat::Csv);
// Create a Workbook object and opening the file from its path
Workbook wbCSV(dataDir + u"Book_CSV.csv", loadOptions4);
std::cout << "CSV file opened successfully!" << std::endl;
Aspose::Cells::Cleanup();

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.

Aspose::Cells::Startup();
//Source directory
U16String sourceDir = u"";
U16String filename = sourceDir + u"InvalidCharacters.csv";
//Load CSV file
LoadFilter filter(LoadDataFilterOptions::CellData);
TxtLoadOptions csvOpt;
csvOpt.SetSeparator(';');
csvOpt.SetLoadFilter(&filter);
csvOpt.SetCheckExcelRestriction(false);
csvOpt.SetConvertNumericData(false);
csvOpt.SetConvertDateTimeData(false);
Workbook workbook(filename, csvOpt);
std::cout << "CSV file opened successfully!" << std::endl;
// Save for check
workbook.Save(u"Output.xlsx");
Aspose::Cells::Cleanup();

Sample source file can be downloaded from the following links for testing this feature.

InvalidCharacters.csv

Opening Text Files with Custom Separator

Text files are used to hold spreadsheet data without formatting. The file is a kind of plain text file that can have some customized delimiters.

Aspose::Cells::Startup();
// The path to the documents directory.
U16String dataDir = u"";
U16String filePath = dataDir + u"CustomSeparator.txt";
// Instantiate Text File's LoadOptions
TxtLoadOptions txtLoadOptions;
// Specify the separator
txtLoadOptions.SetSeparator(u',');
// Specify the encoding type
txtLoadOptions.SetEncoding(EncodingType::UTF8);
// Create a Workbook object and opening the file from its path
Workbook wb(filePath, txtLoadOptions);
// Save file
wb.Save(dataDir + u"output.xlsx");
Aspose::Cells::Cleanup();

Sample source file can be downloaded from the following links for testing this feature.

CustomSeparator.txt

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.

Aspose::Cells::Startup();
// The path to the documents directory.
U16String dataDir = u"";
// Opening Tab Delimited Files
// Instantiate LoadOptions specified by the LoadFormat.
LoadOptions loadOptions5(LoadFormat::TabDelimited);
// Create a Workbook object and opening the file from its path
Workbook wbTabDelimited(dataDir + u"Book1TabDelimited.txt", loadOptions5);
// Show following message on console
std::cout << "TabDelimited file opened successfully!" << std::endl;
Aspose::Cells::Cleanup();

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.

Aspose::Cells::Startup();
//Source directory
U16String sourceDir = u"";
// Instantiate LoadOptions specified by the LoadFormat.
LoadOptions loadOptions(LoadFormat::Tsv);
// Create a Workbook object and opening the file from its path
Workbook workbook(sourceDir + u"SampleTSVFile.tsv", loadOptions);
// Using the Sheet 1 in Workbook
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Accessing a cell using its name
Cell cell = worksheet.GetCells().Get(u"C3");
std::cout << "Cell Name: " << cell.GetName().ToUtf8() << " Value: " << cell.GetStringValue().ToUtf8() << std::endl;
Aspose::Cells::Cleanup();

Opening SXC Files

StarOffice Calc is similar to Microsoft Excel and supports formulas, charts, functions, and macros. The spreadsheets created with this software are saved with the SXC extension. The SXC file is also used for OpenOffice.org Calc spreadsheet files. Aspose.Cells can read SXC files as demonstrated by the following code sample.

Aspose::Cells::Startup();
//Source directory
U16String sourceDir = u"";
// Instantiate LoadOptions specified by the LoadFormat.
LoadOptions loadOptions(LoadFormat::Sxc);
// Create a Workbook object and opening the file from its path
Workbook workbook(sourceDir + u"SampleSXC.sxc", loadOptions);
// Using the Sheet 1 in Workbook
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Accessing a cell using its name
Cell cell = worksheet.GetCells().Get(u"C3");
std::cout << "Cell Name: " << cell.GetName().ToUtf8() << " Value: " << cell.GetStringValue().ToUtf8() << std::endl;
Aspose::Cells::Cleanup();

Opening FODS Files

FODS file is spreadsheet saved in OpenDocument XML without any compression. Aspose.Cells can read FODS files as demonstrated by the following code sample.

Aspose::Cells::Startup();
//Source directory
U16String sourceDir = u"";
// 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);
// Show following message on console
std::cout << "FODS file opened successfully!" << std::endl;
Aspose::Cells::Cleanup();