创建和操作Excel表格

可能的使用场景

Aspose.Cells允许您创建和操作新的或现有的列表对象或表格。您可以利用列表对象或表格的各种方法,例如标题行样式、列条纹、样式类型、显示小计等,也可以处理表格的各列,并设置它们的名称和总计计算函数,可以是Min、Max、Count、Average、Sum等。

创建和操作Excel表格

以下示例代码加载了示例Excel文件,然后在A1:H10范围内创建了一个列表对象或表格,然后利用它的各种方法并设置显示小计。然后将第三列、第四列和第五列的总计函数分别设置为Min、Max和Count,并将结果写入输出Excel文件。以下截图显示了执行后示例代码对示例Excel文件的影响。

todo:image_alt_text

示例代码

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
Aspose::Cells::Startup();
//Source directory path
U16String dirPath(u"..\\Data\\TechnicalArticles\\");
//Output directory path
U16String outPath(u"..\\Data\\Output\\");
//Path of input excel file
U16String sampleCreateAndManipulateExcelTable = dirPath + u"sampleCreateAndManipulateExcelTable.xlsx";
//Path of output excel file
U16String outputCreateAndManipulateExcelTable = outPath + u"outputCreateAndManipulateExcelTable.xlsx";
//Load the sample excel file
Workbook wb(sampleCreateAndManipulateExcelTable);
//Access first worksheet
Worksheet ws = wb.GetWorksheets().Get(0);
//Add table i.e. list object
int idx = ws.GetListObjects().Add(u"A1", u"H10", true);
//Access the newly added list object
ListObject lo = ws.GetListObjects().Get(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.GetListColumns().Get(2).SetTotalsCalculation(TotalsCalculation::Min);
lo.GetListColumns().Get(3).SetTotalsCalculation(TotalsCalculation::Max);
lo.GetListColumns().Get(4).SetTotalsCalculation(TotalsCalculation::Count);
//Save the output excel file
wb.Save(outputCreateAndManipulateExcelTable);
Aspose::Cells::Cleanup();