R语言函数封装
保存数据框为xlsx工作薄中的工作表sheet
add_to_workbook
- 如果不存在该xlsx文件,那么新建xlsx文件,再写入工作表
- 如果存在该xlsx文件,那么根据工作表名写入新的工作表
不能写入同一个xlsx文件中的同名工作表
函数名add_to_workbook
调用方式
# 如果函数封装在另一个R文件,则需先引入包资源
source("./save_xlsx.R")
# 模拟数据框
data <- data.frame(Column1 = c("Value1", "Value2", "Value3"), Column2 = c(10, 20, 30))
add_to_workbook(data, "my_workbook.xlsx", "NewSheet")
三个参数:
data:数据框
workbook_name: 工作薄名(即.xlsx文件名),如不输入,默认为WorkBook+年月日时分秒.xlsx
worksheet_name: 工作表明, 如不输入,默认为sheet1
代码如下:
add_to_workbook <- function(data, workbook_name = paste0("WorkBook", "_",Sys.Date(), "_",format(Sys.time(), "%H%M%S"), ".xlsx"), worksheet_name = "sheet1" ){
if (!require("openxlsx")) {
library("openxlsx")
}
# else {
# cat(paste0(package_name, "已导入,无需再次导入。\n"))
# }
if (file.exists(workbook_name)) {
# 如果文件存在,加载工作簿并添加新工作表
existing_wb <- loadWorkbook(workbook_name)
if (worksheet_name %in% names(existing_wb)) {
cat(paste0("表格文件 ",workbook_name," 中已有同名工作表 ", worksheet_name, " !\n"))
return("请仔细检查代码!")
}
addWorksheet(existing_wb, worksheet_name)
writeData(existing_wb, worksheet_name, data)
# 设置字体和居中格式
style <- createStyle(fontColour = "black", fontName = "Arial",fontSize = 10, halign = "center", valign = "center")
addStyle(existing_wb, worksheet_name, style, rows = 1:(nrow(data)+1), cols = 1:(ncol(data)+1), gridExpand = TRUE)
# 设置首行加粗
bold_style <- createStyle(fontColour = "black", fontName = "Arial", fontSize = 12, halign = "center", valign = "center", textDecoration = "Bold")
addStyle(existing_wb, worksheet_name, bold_style, rows = 1, cols = 1:ncol(data), gridExpand = TRUE)
# 设置自适应行高和列宽
setColWidths(existing_wb, worksheet_name, cols = 1:2, widths = "8")
setColWidths(existing_wb, worksheet_name, cols = 3:ncol(data), widths = "16")
setRowHeights(existing_wb, worksheet_name, rows = 1:nrow(data), heights = "20")
# 保存修改后的工作簿
saveWorkbook(existing_wb, workbook_name, overwrite = TRUE)
cat(paste0("表格文件",workbook_name," 当前已经存在! 成功追加工作表 ", worksheet_name, " \n"))
} else {
# 如果文件不存在,创建新工作簿并添加工作表
wb <- createWorkbook()
addWorksheet(wb, worksheet_name)
writeData(wb, worksheet_name, data)
# 设置字体和居中格式
style <- createStyle(fontColour = "black", fontName = "Arial",fontSize = 10, halign = "center", valign = "center")
addStyle(wb, worksheet_name, style, rows = 1:(nrow(data)+1), cols = 1:(ncol(data)+1), gridExpand = TRUE)
# 设置首行加粗
bold_style <- createStyle(fontColour = "black", fontName = "Arial", fontSize = 12, halign = "center", valign = "center", textDecoration = "Bold")
addStyle(wb, worksheet_name, bold_style, rows = 1, cols = 1:ncol(data), gridExpand = TRUE)
# 设置自适应行高和列宽
setColWidths(wb, worksheet_name, cols = 1:2, widths = "8")
setColWidths(wb, worksheet_name, cols = 3:ncol(data), widths = "16")
setRowHeights(wb, worksheet_name, rows = 1:nrow(data), heights = "20")
# 保存新工作簿
saveWorkbook(wb, workbook_name, overwrite = FALSE)
cat(paste0("表格文件 ",workbook_name," 不存在! 成功新建工作薄并添加工作表 ", worksheet_name, " \n"))
}
}
# 模拟数据框
# data <- data.frame(Column1 = c("Value1", "Value2", "Value3"),
# Column2 = c(10, 20, 30))
# add_to_workbook(data, "my_workbook.xlsx", "NewSheet")