翻譯|使用教程|編輯:李顯亮|2020-05-18 10:02:17.277|閱讀 848 次
概述: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@fc6vip.cn