1. library(openxlxs)
    2. library(purrr)
    3. library(magrittr)
    4. creatwb <- function(df) {
    5. wb <<- createWorkbook()
    6. addWorksheet(wb, sheetName = 'Sheet1')
    7. writeDataTable(wb, 1, df, tableStyle = "TableStyleMedium2")
    8. mainStyle <-
    9. createStyle(
    10. fontName = "微软雅黑",
    11. fontSize = 12,
    12. fontColour = 'black',
    13. numFmt = "GENERAL",
    14. border = 'TopBottomLeftRight',
    15. borderColour = getOption("openxlsx.borderColour", "black"),
    16. borderStyle = getOption("openxlsx.borderStyle", "thin"),
    17. bgFill = NULL,
    18. fgFill = NULL,
    19. halign = 'center',
    20. valign = 'center',
    21. textDecoration = NULL,
    22. wrapText = FALSE,
    23. textRotation = NULL,
    24. indent = NULL
    25. )
    26. addStyle(wb, 1, mainStyle, rows = 0:nrow(df) + 1, cols = 1:ncol(df), gridExpand = TRUE)
    27. showGridLines(wb, sheet = 1, showGridLines = FALSE)
    28. setColWidths(
    29. wb,
    30. sheet = 1,
    31. cols = 1:ncol(df),
    32. widths = pmin(
    33. pmax(
    34. df %>% colnames() %>% str_count(),
    35. df %>% map_df(str_count) %>% map_dbl(function(x)
    36. max(x, na.rm = T))
    37. ),
    38. rep(25, length = ncol(df))
    39. ) + 6
    40. )
    41. }