翻译|使用教程|编辑:李显亮|2020-05-18 10:02:17.277|阅读 769 次
概述:Aspose.Cells for C ++是本机C ++库,可让您创建,读取,解析和转换电子表格文档,而无需Microsoft Excel。在本文中,我们将介绍从头开始创建Excel XLS / XLSX文件的以下功能。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
Aspose.Cells for C ++是本机C ++库,可让您创建,读取,解析和转换电子表格文档,而无需Microsoft Excel。它提供了一套完整的Excel自动化功能,可用于生成和操作XLS / XLSX电子表格。
在本文中,我们将介绍从头开始创建Excel XLS / XLSX文件的以下功能。
如果你还没有使用过Aspose.Cells for C ++,可以点击此处下载最新版体验。
一个工作簿由一个或多个工作表组成,每个工作表都包含行和列形式的数据。因此,为了创建Excel电子表格,您需要先创建一个工作簿,然后在其中添加工作表。以下是使用Aspose.Cells for C ++创建Excel文件的步骤。
下面的代码示例演示如何使用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工作簿的屏幕截图。
在Excel工作簿中设置公式是一项出色的功能,可以对数据执行计算。它使有效高效地对数据执行复杂的计算变得相当容易。以下是在Excel工作表中设置和计算公式的步骤。
下面的代码示例演示如何使用C ++在Excel XLSX工作簿中添加和计算公式。
//Create a new workbook intrusive_ptrwb = Factory::CreateIWorkbook(); //Get first worksheet which is created by default intrusive_ptrws = wb->GetIWorksheets()->GetObjectByIndex(0); //Adding a value to "A1" cell intrusive_ptrcell = 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()));
Excel工作表中的表用于组织位于一系列单元格中的一组数据。表格还可以帮助您维护工作表中的不同类型的列表。以下是在Excel工作表中创建表的步骤。
下面的代码示例演示如何使用C ++在Excel XLSX文件中创建表。
// Instantiate a Workbook object intrusive_ptrworkbook = Factory::CreateIWorkbook(); // Obtaining the reference of the default(first) worksheet intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); // Obtaining Worksheet's cells collection intrusive_ptrcells = 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_ptrlistObject = 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")));
带表的Excel工作簿
Excel电子表格中的图表用于使用不同类型的图形对象来可视化数据。它们使我们可以快速了解和理解数据,尤其是在数据量巨大时。C ++的Aspose.Cells支持各种图表,包括森伯斯特,树形图,直方图,金字塔,气泡,折线等。以下是使用Aspose.Cells for C ++在Excel工作簿中创建图表的步骤。
下面的代码示例演示如何使用C ++在Excel XLSX文件中创建图表。
// Path of output excel file StringPtr outputChartTypePyramid = resultPath->StringAppend(new String("Exce_Pyramid_Chart.xlsx")); // Create a new workbook intrusive_ptrworkbook = Factory::CreateIWorkbook(); // Get first worksheet which is created by default intrusive_ptrworksheet = 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_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 file workbook->Save(outputChartTypePyramid);
带有图表的Excel工作簿
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn