Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create Excel Files in C++
//Path of output excel file
StringPtr outputData = resultPath->StringAppend(new String("outputData.xlsx"));
//Read input excel file
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook();
//Accessing the second worksheet in the Excel file
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
//Adding a string value to the cell
worksheet->GetICells()->GetObjectByIndex(new String("A1"))->PutValue("Hello World");
//Adding a double value to the cell
worksheet->GetICells()->GetObjectByIndex(new String("A2"))->PutValue(20.5);
//Adding an integer value to the cell
worksheet->GetICells()->GetObjectByIndex(new String("A3"))->PutValue(15);
//Adding a boolean value to the cell
worksheet->GetICells()->GetObjectByIndex(new String("A4"))->PutValue(true);
//Setting the display format of the date
intrusive_ptr<ICell> cell = worksheet->GetICells()->GetObjectByIndex(new String("A5"));
intrusive_ptr<IStyle> style = cell->GetIStyle();
style->SetNumber(15);
cell->SetIStyle(style);
//Save the workbook
workbook->Save(outputData);
//Create a new workbook
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook();
//Get first worksheet which is created by default
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0);
//Adding a value to "A1" cell
intrusive_ptr<ICell> cell = ws->GetICells()->GetObjectByIndex(new String("A1"));
cell->PutValue(5);
//Adding a value to "A2" cell
cell = ws->GetICells()->GetObjectByIndex(new String("A2"));
cell->PutValue(15);
//Adding a value to "A3" cell
cell = ws->GetICells()->GetObjectByIndex(new String("A3"));
cell->PutValue(25);
//Adding SUM formula to "A4" cell
cell = ws->GetICells()->GetObjectByIndex(new String("A4"));
cell->SetFormula(new String("=SUM(A1:A3)"));
//Calculating the results of formulas
wb->CalculateFormula();
//Get the calculated value of the cell "A4" and print it on console
cell = ws->GetICells()->GetObjectByIndex(new String("A4"));
StringPtr sCalcuInfo = new String(L"Calculated Value of Cell A4: ");
Console::WriteLine(sCalcuInfo->StringAppend(cell->GetStringValue()));
// Path of output excel file
StringPtr outputChartTypePyramid = resultPath->StringAppend(new String("Exce_Pyramid_Chart.xlsx"));
// Create a new workbook
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook();
// Get first worksheet which is created by default
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
// Adding sample values to cells
worksheet->GetICells()->GetObjectByIndex(new String("A1"))->PutValue(50);
worksheet->GetICells()->GetObjectByIndex(new String("A2"))->PutValue(100);
worksheet->GetICells()->GetObjectByIndex(new String("A3"))->PutValue(150);
worksheet->GetICells()->GetObjectByIndex(new String("B1"))->PutValue(4);
worksheet->GetICells()->GetObjectByIndex(new String("B2"))->PutValue(20);
worksheet->GetICells()->GetObjectByIndex(new String("B3"))->PutValue(50);
// Adding a chart to the worksheet
int chartIndex = worksheet->GetICharts()->Add(Aspose::Cells::Charts::ChartType::ChartType_Pyramid, 5, 0, 20, 8);
// Accessing the instance of the newly added chart
intrusive_ptr<Aspose::Cells::Charts::IChart> chart = worksheet->GetICharts()->GetObjectByIndex(chartIndex);
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart->GetNISeries()->Add(new String("A1:B3"), true);
// Saving the Excel file
workbook->Save(outputChartTypePyramid);
/*create a new workbook*/
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook();
/*get the first worksheet*/
intrusive_ptr<IWorksheetCollection> wsc = wb->GetIWorksheets();
intrusive_ptr<IWorksheet> ws = wsc->GetObjectByIndex(0);
/*get cell(0,0)*/
intrusive_ptr<ICells> cells = ws->GetICells();
intrusive_ptr<ICell> cell = cells->GetObjectByIndex(0, 0);
/*write "Hello World" to cell(0,0) of the first sheet*/
intrusive_ptr<String> str = new String("Hello World!");
cell->PutValue(str);
/*save this workbook to resultFile folder*/
wb->Save(resultPath->StringAppend(new String("workbook.xlsx")));
// Instantiate a Workbook object
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook();
// Obtaining the reference of the default(first) worksheet
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);
// Obtaining Worksheet's cells collection
intrusive_ptr<ICells> cells = worksheet->GetICells();
// Setting the value to the cells
cells->GetObjectByIndex(new String("A1"))->PutValue("Employee");
cells->GetObjectByIndex(new String("B1"))->PutValue("Quarter");
cells->GetObjectByIndex(new String("C1"))->PutValue("Product");
cells->GetObjectByIndex(new String("D1"))->PutValue("Continent");
cells->GetObjectByIndex(new String("E1"))->PutValue("Country");
cells->GetObjectByIndex(new String("F1"))->PutValue("Sale");
cells->GetObjectByIndex(new String("A2"))->PutValue("David");
cells->GetObjectByIndex(new String("A3"))->PutValue("David");
cells->GetObjectByIndex(new String("A4"))->PutValue("David");
cells->GetObjectByIndex(new String("A5"))->PutValue("David");
cells->GetObjectByIndex(new String("A6"))->PutValue("James");
cells->GetObjectByIndex(new String("B2"))->PutValue(1);
cells->GetObjectByIndex(new String("B3"))->PutValue(2);
cells->GetObjectByIndex(new String("B4"))->PutValue(3);
cells->GetObjectByIndex(new String("B5"))->PutValue(4);
cells->GetObjectByIndex(new String("B6"))->PutValue(1);
cells->GetObjectByIndex(new String("C2"))->PutValue("Maxilaku");
cells->GetObjectByIndex(new String("C3"))->PutValue("Maxilaku");
cells->GetObjectByIndex(new String("C4"))->PutValue("Chai");
cells->GetObjectByIndex(new String("C5"))->PutValue("Maxilaku");
cells->GetObjectByIndex(new String("C6"))->PutValue("Chang");
cells->GetObjectByIndex(new String("D2"))->PutValue("Asia");
cells->GetObjectByIndex(new String("D3"))->PutValue("Asia");
cells->GetObjectByIndex(new String("D4"))->PutValue("Asia");
cells->GetObjectByIndex(new String("D5"))->PutValue("Asia");
cells->GetObjectByIndex(new String("D6"))->PutValue("Europe");
cells->GetObjectByIndex(new String("E2"))->PutValue("China");
cells->GetObjectByIndex(new String("E3"))->PutValue("India");
cells->GetObjectByIndex(new String("E4"))->PutValue("Korea");
cells->GetObjectByIndex(new String("E5"))->PutValue("India");
cells->GetObjectByIndex(new String("E6"))->PutValue("France");
cells->GetObjectByIndex(new String("F2"))->PutValue(2000);
cells->GetObjectByIndex(new String("F3"))->PutValue(500);
cells->GetObjectByIndex(new String("F4"))->PutValue(1200);
cells->GetObjectByIndex(new String("F5"))->PutValue(1500);
cells->GetObjectByIndex(new String("F6"))->PutValue(500);
// Adding a new List Object to the worksheet
worksheet->GetIListObjects()->Add(new String("A1"), new String("F6"), true);
intrusive_ptr<IListObject> listObject = worksheet->GetIListObjects()->GetObjectByIndex(0);
// Adding Default Style to the table
listObject->SetTableStyleType(TableStyleType_TableStyleMedium10);
// Show Total
listObject->SetShowTotals(true);
// Saving the Excel file
workbook->Save(resultPath->StringAppend(new String("Excel_Table.xlsx")));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.