创建和操作 Excel 表格

可能的使用场景

Aspose.Cells 允许您创建和操作新的或现有的列表对象或表格。您可以使用列表对象或表格的各种方法,例如标题行样式、列条纹、样式类型、显示小计等,还可以使用表格的各个列并设置它们的名称和总计计算函数,可以是 Min 、最大值、计数、平均值、求和等。

创建和操作 Excel 表格

下面的示例代码加载示例 excel 文件然后在 A1:H10 范围内创建一个列表对象或表,然后它利用其各种方法并设置显示小计。然后将第3、4、5列的总函数分别设置为Min、Max和Count,写出输出excel文件.下面的截图展示了示例代码对示例 excel 文件执行后。

待办事项:图片_替代_文本

示例代码

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Source directory path
StringPtr dirPath = new String("..\\Data\\TechnicalArticles\\");
//Output directory path
StringPtr outPath = new String("..\\Data\\Output\\");
//Path of input excel file
StringPtr sampleCreateAndManipulateExcelTable = dirPath->StringAppend(new String("sampleCreateAndManipulateExcelTable.xlsx"));
//Path of output excel file
StringPtr outputCreateAndManipulateExcelTable = outPath->StringAppend(new String("outputCreateAndManipulateExcelTable.xlsx"));
//Load the sample excel file
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(sampleCreateAndManipulateExcelTable);
//Access first worksheet
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0);
//Add table i.e. list object
int idx = ws->GetIListObjects()->Add(new String("A1"), new String("H10"), true);
//Access the newly added list object
intrusive_ptr<IListObject> lo = ws->GetIListObjects()->GetObjectByIndex(idx);
//Use its display methods
lo->SetShowHeaderRow(true);
lo->SetShowTableStyleColumnStripes(true);
lo->SetShowTotals(true);
//Set its style
lo->SetTableStyleType(TableStyleType_TableStyleLight12);
//Set total functions of 3rd, 4th and 5th columns
lo->GetIListColumns()->GetObjectByIndex(2)->SetTotalsCalculation(TotalsCalculation_Min);
lo->GetIListColumns()->GetObjectByIndex(3)->SetTotalsCalculation(TotalsCalculation_Max);
lo->GetIListColumns()->GetObjectByIndex(4)->SetTotalsCalculation(TotalsCalculation_Count);
//Save the output excel file
wb->Save(outputCreateAndManipulateExcelTable);