Aspose.Cells for C ++是本机C ++库,可让您创建,读取,解析和转换电子表格文档,而无需Microsoft Excel。它提供了一套完整的Excel自动化功能,可用于生成和操作XLS / XLSX电子表格。
- 使用C ++创建Excel XLS或XLSX文件
- 使用C ++将数据添加到Excel工作表中
- 使用C ++计算工作簿公式
- 使用C ++在Excel工作表中创建表
- 使用C ++在Excel XLS / XLSX中创建图表
如果你还没有使用过Aspose.Cells for C ++,可以点击此处下载最新版体验。
使用C ++创建Excel XLS或XLSX文件
一个工作簿由一个或多个工作表组成,每个工作表都包含行和列形式的数据。因此,为了创建Excel电子表格,您需要先创建一个工作簿,然后在其中添加工作表。以下是使用Aspose.Cells for C ++创建Excel文件的步骤。
- 创建IWorkbook类的对象。
- 从IWorksheetCollection将工作簿的第一个工作表(默认情况下创建)放入IWorksheet对象。
- 使用IWorksheet-> GetICells()方法将工作表的单元格访问到ICells对象中。
- 通过指定行和列索引,使用ICells-> GetObjectByIndex()方法将工作表的所需单元格访问到ICell对象中。
- 使用ICell-> PutValue()方法将值添加到单元格。
- 使用IWorkbook-> Save()方法将工作簿另存为.xlsx文件。
下面的代码示例演示如何使用C ++创建Excel XLSX文件。
/*create a new workbook*/intrusive_ptrwb = Factory::CreateIWorkbook();/*get the first worksheet*/intrusive_ptrwsc = wb->GetIWorksheets();intrusive_ptrws = wsc->GetObjectByIndex(0);/*get cell(0,0)*/intrusive_ptrcells = ws->GetICells();intrusive_ptrcell = cells->GetObjectByIndex(0, 0);/*write "Hello World" to cell(0,0) of the first sheet*/intrusive_ptrstr = new String("Hello World!");cell->PutValue(str);/*save this workbook to resultFile folder*/wb->Save(resultPath->StringAppend(new String("workbook.xlsx")));
Excel工作簿
以下是我们刚刚创建的Excel工作簿的屏幕截图。

使用C ++计算工作簿公式
在Excel工作簿中设置公式是一项出色的功能,可以对数据执行计算。它使有效高效地对数据执行复杂的计算变得相当容易。以下是在Excel工作表中设置和计算公式的步骤。
- 创建IWorkbook类的对象。
- 从IWorksheetCollection将工作表放入IWorksheet对象。
- 使用ICell类访问您要对其应用公式的单元格。
- 使用ICell-> SetFormula()方法设置单元格的公式。
- 使用IWorkbook-> CalculateFormula()方法计算公式。
- 使用IWorkbook-> Save()方法保存工作簿。
下面的代码示例演示如何使用C ++在Excel XLSX工作簿中添加和计算公式。
//Create a new workbookintrusive_ptrwb = Factory::CreateIWorkbook();//Get first worksheet which is created by defaultintrusive_ptrws = wb->GetIWorksheets()->GetObjectByIndex(0);//Adding a value to "A1" cellintrusive_ptrcell = ws->GetICells()->GetObjectByIndex(new String("A1"));cell->PutValue(5);//Adding a value to "A2" cellcell = ws->GetICells()->GetObjectByIndex(new String("A2"));cell->PutValue(15);//Adding a value to "A3" cellcell = ws->GetICells()->GetObjectByIndex(new String("A3"));cell->PutValue(25);//Adding SUM formula to "A4" cellcell = ws->GetICells()->GetObjectByIndex(new String("A4"));cell->SetFormula(new String("=SUM(A1:A3)"));//Calculating the results of formulaswb->CalculateFormula();//Get the calculated value of the cell "A4" and print it on consolecell = ws->GetICells()->GetObjectByIndex(new String("A4"));StringPtr sCalcuInfo = new String(L"Calculated Value of Cell A4: ");Console::WriteLine(sCalcuInfo->StringAppend(cell->GetStringValue()));
使用C ++在Excel工作表中创建表
Excel工作表中的表用于组织位于一系列单元格中的一组数据。表格还可以帮助您维护工作表中的不同类型的列表。以下是在Excel工作表中创建表的步骤。
- 使用IWorkbook类创建一个新的Excel工作簿。
- 将默认工作表访问到IWorksheet对象中,或添加一个新的工作表。
- 以与上一个示例相同的方式将值/数据插入到单元格中。
- 使用IWorksheet-> GetIListObjects()-> Add()方法将单元格范围添加到工作表的列表集合。
- 将列表获取到IListObject对象中。
- 使用IListObject-> SetTableStyleType()方法将样式应用于表。
- 将工作簿另存为.xlsx文件。
下面的代码示例演示如何使用C ++在Excel XLSX文件中创建表。
// Instantiate a Workbook objectintrusive_ptrworkbook = Factory::CreateIWorkbook();// Obtaining the reference of the default(first) worksheetintrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Obtaining Worksheet's cells collectionintrusive_ptrcells = worksheet->GetICells();// Setting the value to the cellscells->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 worksheetworksheet->GetIListObjects()->Add(new String("A1"), new String("F6"), true);intrusive_ptrlistObject = worksheet->GetIListObjects()->GetObjectByIndex(0);// Adding Default Style to the tablelistObject->SetTableStyleType(TableStyleType_TableStyleMedium10);// Show TotallistObject->SetShowTotals(true);// Saving the Excel fileworkbook->Save(resultPath->StringAppend(new String("Excel_Table.xlsx")));
带表的Excel工作簿

使用C ++在Excel XLSX中创建图表
Excel电子表格中的图表用于使用不同类型的图形对象来可视化数据。它们使我们可以快速了解和理解数据,尤其是在数据量巨大时。C ++的Aspose.Cells支持各种图表,包括森伯斯特,树形图,直方图,金字塔,气泡,折线等。以下是使用Aspose.Cells for C ++在Excel工作簿中创建图表的步骤。
- 创建一个新的Excel工作簿。
- 将所需的工作表访问到IWorksheet对象中。
- 通过传递图表的类型,使用IWorksheet-> GetICharts()-> Add()方法将新图表添加到工作表中。
- 将新添加的图表访问到IChart对象中。
- 使用IChart-> GetNISeries()-> Add()方法设置图表的数据源。
- 将工作簿另存为.xlsx文件。
下面的代码示例演示如何使用C ++在Excel XLSX文件中创建图表。
// Path of output excel fileStringPtr outputChartTypePyramid = resultPath->StringAppend(new String("Exce_Pyramid_Chart.xlsx"));// Create a new workbookintrusive_ptrworkbook = Factory::CreateIWorkbook();// Get first worksheet which is created by defaultintrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Adding sample values to cellsworksheet->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 worksheetint chartIndex = worksheet->GetICharts()->Add(Aspose::Cells::Charts::ChartType::ChartType_Pyramid, 5, 0, 20, 8);// Accessing the instance of the newly added chartintrusive_ptrchart = 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 fileworkbook->Save(outputChartTypePyramid);
带有图表的Excel工作簿

还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时联系客服,我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!