Loading and Managing Excel, OpenOffice, Json, Csv, and Html Files with C++
Contents
[
Hide
]
With Aspose.Cells for C++, it is simple to create, open, and manage Excel files, for example, to retrieve data or use a designer template to speed up the development process.
Creating a New Workbook
The following example creates a new workbook from scratch.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
try
{
// Create a License object
License license;
// Set the license of Aspose.Cells to avoid the evaluation limitations
license.SetLicense(srcDir + u"Aspose.Cells.lic");
}
catch (const std::exception& ex)
{
std::cerr << ex.what() << std::endl;
}
// Instantiate a Workbook object that represents Excel file.
Workbook wb;
// When you create a new workbook, a default "Sheet1" is added to the workbook.
Worksheet sheet = wb.GetWorksheets().Get(0);
// Access the "A1" cell in the sheet.
Cell cell = sheet.GetCells().Get(u"A1");
// Input the "Hello World!" text into the "A1" cell
cell.PutValue(u"Hello World!");
// Save the Excel file.
wb.Save(srcDir + u"MyBook_out.xlsx");
Aspose::Cells::Cleanup();
return 0;
}
Opening and Saving a File
With Aspose.Cells for C++, it is simple to open, save, and manage Excel files.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"Book1.xlsx";
// Path of output Excel file
U16String outputFilePath = outDir + u"dest.xlsx";
// Create a Workbook object and open an Excel file using its file path
Workbook workbook1(inputFilePath);
// Adding new sheet
WorksheetCollection sheets = workbook1.GetWorksheets();
Worksheet sheet = sheets.Add(u"MySheet");
// Setting active sheet
sheets.SetActiveSheetIndex(1);
// Setting values
Cells cells = sheet.GetCells();
// Setting text
cells.Get(u"A1").PutValue(u"Hello!");
// Setting number
cells.Get(u"A2").PutValue(1000);
// Setting Date Time
Cell cell = cells.Get(u"A3");
Date currentDate;
currentDate.year = 2023; // Replace with actual current year
currentDate.month = 10; // Replace with actual current month
currentDate.day = 5; // Replace with actual current day
currentDate.hour = 12; // Replace with actual current hour
currentDate.minute = 30; // Replace with actual current minute
currentDate.second = 0; // Replace with actual current second
cell.PutValue(currentDate);
// Setting style for date
Style style = cell.GetStyle();
style.SetNumber(14);
cell.SetStyle(style);
// Setting formula
cells.Get(u"A4").SetFormula(u"=SUM(A1:A3)");
// Saving the workbook to disk
workbook1.Save(outputFilePath);
std::cout << "Workbook saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Advance Topics
- Different Ways to Open Files
- Filter Defined Names While Loading Workbook
- Filter Objects While Loading Workbook or Worksheet
- Filtering the Kind of Data While Loading the Workbook from Template File
- Get Warnings While Loading Excel File
- Load Source Excel File Without Charts
- Load Specific Worksheets in a Workbook
- Load Workbook with Specified Printer Paper Size
- Opening Different Microsoft Excel Versions Files
- Opening Files with Different Formats
- Optimizing Memory Usage While Working with Big Files Having Large Datasets
- Read Numbers Spreadsheet Developed by Apple Inc. Using Aspose.Cells
- Stop Conversion or Loading Using InterruptMonitor When It Is Taking Too Long
- Using LightCells API
- Convert CSV to JSON
- Convert Excel to JSON
- Convert JSON to CSV
- Convert JSON to Excel
- Convert Excel to Html