c++ libxl 使用公式计算平均值、方差、标准差、求和,跨sheet求和
时间: 2023-07-10 07:35:52 浏览: 71
你可以使用 libxl 库中的 Formula 类来计算公式。具体来说,对于单个 sheet 中的数据,你可以使用以下公式:
- 平均值:AVERAGE(range)
- 方差:VAR(range)
- 标准差:STDEV(range)
- 求和:SUM(range)
其中,range 是要计算的数据范围,比如 "A1:A10"。
如果你想在多个 sheet 中计算公式,可以使用类似以下的公式:
- 跨 sheet 求和:SUM(sheet1!range1, sheet2!range2, ...)
其中,sheet1、sheet2 等是 sheet 的名称,range1、range2 等是要计算的数据范围。
举个例子,假设你要在 sheet1 的 A 列中计算平均值、方差、标准差和求和,可以使用以下代码:
```c++
Book* book = xlCreateXMLBook();
Sheet* sheet = book->addSheet("sheet1");
sheet->writeNum(1, 1, 1);
sheet->writeNum(2, 1, 2);
sheet->writeNum(3, 1, 3);
sheet->writeFormula(4, 1, "AVERAGE(A1:A3)");
sheet->writeFormula(5, 1, "VAR(A1:A3)");
sheet->writeFormula(6, 1, "STDEV(A1:A3)");
sheet->writeFormula(7, 1, "SUM(A1:A3)");
book->save("example.xlsx");
```
如果你想在 sheet1 和 sheet2 中计算求和,可以使用以下代码:
```c++
Book* book = xlCreateXMLBook();
Sheet* sheet1 = book->addSheet("sheet1");
Sheet* sheet2 = book->addSheet("sheet2");
sheet1->writeNum(1, 1, 1);
sheet1->writeNum(2, 1, 2);
sheet2->writeNum(1, 1, 3);
sheet2->writeNum(2, 1, 4);
sheet1->writeFormula(3, 1, "SUM(sheet1!A1:A2, sheet2!A1:A2)");
book->save("example.xlsx");
```
这样就可以在 sheet1 中计算 sheet1 和 sheet2 中 A 列的求和了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)