You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用openxlsx2设置数据透视表为表格布局且无小计?

使用openxlsx2生成Tabular布局且无小计的数据透视表

openxlsx2目前已经支持调整数据透视表的布局和小计显示,你只需在add_pivot_table函数中添加两个参数即可实现需求:

library(openxlsx2)

wb <- wb_workbook() %>% 
      wb_add_worksheet() %>%
      wb_add_data(x = mtcars, inline_strings = F)

wb$add_numfmt(dims = wb_dims(x = mtcars, cols = "disp"), numfmt = "$ #,###")

df <- wb_data(wb)

wb$add_pivot_table(
  df,
  rows = "cyl", cols = "gear",
  data = c("disp", "hp"),
  fun = c("sum", "count"),
  params = list(
    numfmt = c(formatCode = "$ ###", formatCode = "#")
  ),
  layout = "tabular",  # 设置为表格(Tabular)布局
  subtotal = list(rows = FALSE, cols = FALSE)  # 关闭行、列的小计
)

wb$open()

参数说明:

  • layout = "tabular":指定数据透视表采用表格形式展示,对应Excel中的Tabular布局选项。
  • subtotal = list(rows = FALSE, cols = FALSE):分别禁用行维度和列维度的小计显示,达到不展示Subtotals的效果。

内容的提问来源于stack exchange,提问作者emi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 18:46:16