编辑xlsx的作者

时间:2015-10-02 09:24:52

标签: r xlsx

有没有办法使用body{ padding:2em; } #subheader h1{ font-size:1.5em; margin-top:0; } #subheader h2{font-size:1.2em;} #subheader { position: relative; max-width:300px; min-height:1.5em; padding: 20px; background: #FFFFFF; border: #dedede solid 2px; -webkit-border-radius: 20px; -moz-border-radius: 20px; border-radius: 20px; } #subheader:after { content: ""; position: absolute; bottom: -19px; height:13px; widht:12px; left: 10%; border-style: solid; border-width: 20px 13px 0; border-color: #FFFFFF transparent; display: block; width: 0; z-index: 1; } #subheader:before { content: ""; position: absolute; bottom: -22.5px; left: calc(10.5% - 3px) ; border-style: solid; border-width: 23px 15px 0px; border-color: #dedede transparent; display: block; width: 0; z-index: 0;} 包从RStudio编辑.xlsx文件的作者?

我创建简单的xlsx工作簿

xlsx

在Excel中这个xlsx的作者中,我看到" Apache POI",我该如何编辑这个字段?

data_1=data.frame(1,1,2) require(xlsx) wb <- createWorkbook() sheet <- createSheet(wb,"TEST") addDataFrame(as.data.frame(data_1), sheet=sheet, startRow=1, startColumn=1, row.names=FALSE ) saveWorkbook(wb,file = "TEST.xlsx") 的文档中找不到这样的内容。

谢谢!

2 个答案:

答案 0 :(得分:4)

library(XML)

# source workbook ---------------------------------------------------------

xl <- "~/Documents/wb.xlsx"

# make a copy using .zip extension ----------------------------------------

tmp <- tempfile(fileext=".zip")
xl <- path.expand(xl)
ok <- file.copy(xl, tmp)

# unzip it and get file list ----------------------------------------------

tmpdir <- tempfile()
fils <- unzip(tmp, exdir=tmpdir)

# get the doc properties file (one of them anyway) ------------------------

props_file <- grep("docProps/core.xml", fils, value=TRUE)

# open it -----------------------------------------------------------------

props <- xmlTreeParse(props_file, useInternalNodes=TRUE)

# view some info ----------------------------------------------------------

for (tag in c("dc:title", "dc:subject", "dc:creator", "cp:keywords",
              "dc:description", "cp:lastModifiedBy", "cp:category")) {
  print(xmlValue(props[[sprintf("//%s", tag)]]))
}

# modify some info --------------------------------------------------------

# you can do this for any of those properties
creator <- getNodeSet(props, "//dc:creator")[[1]]
xmlValue(creator) <- "Mickey Mouse"

# save out the modified file ----------------------------------------------

saveXML(props, props_file)

# re-zip the archive ------------------------------------------------------

unlink(tmp)
cur <- getwd()
setwd(tmpdir)
zip(tmp, basename(tmpdir))
setwd(cur)

# move new xlsx to source xlsir -------------------------------------------

file.copy(tmp,
          paste0(file.path(dirname(path.expand(xl)), basename(tmpdir)), ".xlsx"),
          overwrite=FALSE) # FALSE for testing only

通过制作具有某些文档属性的虚拟笔记本来测试。

以下是该代码中的两个函数,一个用于查看属性,另一个用于通过命名向量设置它们:

#' Set Excel (xlsx) document properties
#'
#' pass in a named vector. Names should be in this set:
#'
#'   "dc:title", "dc:subject", "dc:creator", "cp:keywords",
#'   "dc:description", "cp:category"
#'
#' @param xl path to excel xlsx file
#' @param file_props document properties to set (named vector)
#' @return new filename created (this doesn't overwrite the existing since
#'         there's not enough error checking)
#' @examples
#' set_properties("path/tp/some.xlsx",
#'                c(`dc:title`="Cool Title",
#'                  `dc:subject`="Cool Subject",
#'                  `dc:creator`="Cool Creator"))
set_properties <- function(xl, file_props) {

  require(XML)

  # make a copy using .zip extension ----------------------------------------

  tmp <- tempfile(fileext=".zip")
  xl <- path.expand(xl)
  ok <- file.copy(xl, tmp)

  # unzip it and get file list ----------------------------------------------

  tmpdir <- tempfile()
  fils <- unzip(tmp, exdir=tmpdir)

  # get the doc properties file (one of them anyway) ------------------------

  props_file <- grep("docProps/core.xml", fils, value=TRUE)

  # open it -----------------------------------------------------------------

  props <- xmlTreeParse(props_file, useInternalNodes=TRUE)

  # you can do this for any of those properties
  for (tag in names(file_props)) {
    # message(sprintf("//%s", tag))
    # message(file_props[tag])
    tval <- getNodeSet(props, sprintf("//%s", tag))[[1]]
    xmlValue(tval) <- file_props[tag]
  }

  # save out the modified file ----------------------------------------------

  saveXML(props, props_file)

  # re-zip the archive ------------------------------------------------------

  unlink(tmp)
  cur <- getwd()
  setwd(tmpdir)
  zip(tmp, "./")
  setwd(cur)

  # move new xlsx to source xlsir -------------------------------------------
  new_fil <- paste0(file.path(dirname(path.expand(xl)), basename(tmpdir)), ".xlsx")
  file.copy(tmp, new_fil, overwrite=TRUE)

  new_fil
}

#' Display Excel (xlsx) document properties
#'
#' @param xl path to excel xlsx file
#' @examples
#' print_properties("path/to/some.xlsx")
print_properties <- function(xl, props) {
  require(XML)

  # make a copy using .zip extension ----------------------------------------

  tmp <- tempfile(fileext=".zip")
  xl <- path.expand(xl)
  ok <- file.copy(xl, tmp)

  # unzip it and get file list ----------------------------------------------

  tmpdir <- tempfile()
  fils <- unzip(tmp, exdir=tmpdir)

  # get the doc properties file (one of them anyway) ------------------------

  props_file <- grep("docProps/core.xml", fils, value=TRUE)

  # open it -----------------------------------------------------------------

  props <- xmlTreeParse(props_file, useInternalNodes=TRUE)
  for (tag in c("dc:title", "dc:subject", "dc:creator", "cp:keywords",
                "dc:description", "cp:category")) {
    cat(sprintf("%16s", tag), ": ", xmlValue(props[[sprintf("//%s", tag)]]), sep="", "\n")
  }

  unlink(tmp)
  unlink(tmpdir)

}

答案 1 :(得分:1)

最简单的方法是使用库openxlsx而不是xlsx。默认情况下,openxlsx保存文件而不指定作者,但是您可以设置一个文件:

require(openxlsx)
write.xlsx(my_data_frame, "my_filename.xlsx", creator="Your Name")