使用R将Excel(.xlsx)工作表打印/保存为PDF [英] Print/save Excel (.xlsx) sheet to PDF using R

查看:186
本文介绍了使用R将Excel(.xlsx)工作表打印/保存为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在操作后将Excel文件打印到pdf文件.对于操作,我使用了效果很好的.xlsx软件包.有功能printSetup,但是我找不到启动打印的功能.有解决方案吗?

I want to print an Excel file to a pdf file after manipulating it. For the manipulation I used the .xlsx package which works fine. There is a function printSetup but I cannot find a function to start the printing. Is there a solution for this?

library(xlsx)
file <- "test.xlsx"
wb <- loadWorkbook(file)  
sheets <- getSheets(wb)       # get all sheets
sheet <- sheets[[1]]          # get first sheet
# HERE: MAGIC TO SAVE THIS SHEET TO PDF

这可能是通过RDCOMClient包使用 DCOM 的解决方案,尽管我更喜欢当我在MacOS上工作时,一个独立于平台的解决方案(例如,使用xlsx).有什么想法吗?

It may be a solution using DCOM through the RDCOMClient package, though I would prefer a plattform independent solution (e.g. using xlsx) as I work on MacOS. Any ideas?

推荐答案

使用libreoffice这样的开源和跨平台方法是这样的:

An open source and cross platform way to do this would be with libreoffice as so:

library("XLConnect")
x <- rnorm(1:100)
y <- x ^ 2
writeWorksheetToFile("test.xlsx", data.frame(x = x, y = y), "Data")
tmpDir <- file.path(tempdir(), "LOConv")
system2("libreoffice", c(paste0("-env:UserInstallation=file://", tmpDir), "--headless", "--convert-to pdf",
    "--outdir", getwd(), file.path(getwd(),"test.xlsx")))

理想情况下,您将删除tmpDir引用的文件夹,但这将是特定于平台的.

Ideally you'd then remove the folder referenced by tmpDir but that would be platform specific.

请注意,这假定libreoffice在您的路径中.如果不是,则需要更改命令以包含libreoffice可执行文件的完整路径.

Note this assumes libreoffice is in your path. If it isn't, then the command would need to be altered to include the full path to the libreoffice executable.

env位的原因是,无头libreoffice仅在尚未在GUI模式下运行时才会执行其他操作.参见 http://ask有关更多信息,请访问.libreoffice.org/en/question/1686/how-to-not-connect-to-a-running-instance/.

The reason for the env bit is that headless libreoffice will only do anything otherwise if it isn't already running in GUI mode. See http://ask.libreoffice.org/en/question/1686/how-to-not-connect-to-a-running-instance/ for more info.

这篇关于使用R将Excel(.xlsx)工作表打印/保存为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆