library(openxlxs)
library(purrr)
library(magrittr)
creatwb <- function(df) {
wb <<- createWorkbook()
addWorksheet(wb, sheetName = 'Sheet1')
writeDataTable(wb, 1, df, tableStyle = "TableStyleMedium2")
mainStyle <-
createStyle(
fontName = "微软雅黑",
fontSize = 12,
fontColour = 'black',
numFmt = "GENERAL",
border = 'TopBottomLeftRight',
borderColour = getOption("openxlsx.borderColour", "black"),
borderStyle = getOption("openxlsx.borderStyle", "thin"),
bgFill = NULL,
fgFill = NULL,
halign = 'center',
valign = 'center',
textDecoration = NULL,
wrapText = FALSE,
textRotation = NULL,
indent = NULL
)
addStyle(wb, 1, mainStyle, rows = 0:nrow(df) + 1, cols = 1:ncol(df), gridExpand = TRUE)
showGridLines(wb, sheet = 1, showGridLines = FALSE)
setColWidths(
wb,
sheet = 1,
cols = 1:ncol(df),
widths = pmin(
pmax(
df %>% colnames() %>% str_count(),
df %>% map_df(str_count) %>% map_dbl(function(x)
max(x, na.rm = T))
),
rep(25, length = ncol(df))
) + 6
)
}