简体   繁体   English

如何从R中将Excel工作表范围导出到图片

[英]How to export an Excel sheet range to a picture, from within R

We are trying to automate the creation of some picture files within an R Script. 我们正在尝试在R脚本中自动创建一些图片文件。

We have the Excel files looking the way that we want them to, but now need to make a JPG or PNG picture-copy of those excel tables for easier web posting. 我们的Excel文件看起来就像我们想要的那样,但是现在需要制作这些excel表的JPG或PNG图片副本,以便更容易发布网页。

We've been using the library(xlsx) package for most of our interactions between R and Excel, and it looks like we should be able to send specific java commands through something like ?.jmethods but it's unclear how we would pass as many lines as we need to. 我们一直在使用library(xlsx)包进行R和Excel之间的大多数交互,看起来我们应该能够通过类似?.jmethods类的方式发送特定的java命令但是我们不知道如何传递尽可能多的行我们需要。

In an R session, here's a minimal reproducible example... R会话中,这是一个可重复性最小的例子......

Here's an example Excel file with a range to print 这是一个带有要打印范围的Excel文件示例

library(xlsx)
file <- system.file("tests", "test_import.xlsx", package = "xlsx")
file

And here's the Excel macro that exports the Excel range to a picture file 这是Excel宏将Excel范围导出到图片文件

Sub Tester()

    Worksheets("deletedFields").Range("A8:J36").CopyPicture xlScreen, xlBitmap

    Application.DisplayAlerts = False
    Set oCht = Charts.Add
    With oCht
        .Paste
        .Export Filename:="C:\temp\SavedRange.jpg", Filtername:="JPG"
        .Delete
    End With


End Sub

Any help automating this would be much appreciated! 任何有助于自动化的帮助将非常感谢!

Consider having R do exactly as VBA does in your macro: making a COM interface to the Excel object library. 考虑让R完全按照VBA在宏中执行操作:为Excel对象库创建COM接口。 You can do so with the RDCOMClient package, retaining nearly same code as macro in the R syntax. 您可以使用RDCOMClient包执行此RDCOMClient ,在R语法中保留与宏几乎相同的代码。

library(RDCOMClient)

xlApp <- COMCreate("Excel.Application")
xlWbk <- xlApp$Workbooks()$Open("C:\\Path\\To\\test_import.xlsx")
xlScreen = 1
xlBitmap = 2

xlWbk$Worksheets("deletedFields")$Range("A8:J36")$CopyPicture(xlScreen, xlBitmap)

xlApp[['DisplayAlerts']] <- FALSE

oCht <- xlApp[['Charts']]$Add()
oCht$Paste()
oCht$Export("C:\\Temp\\SavedRange.jpg", "JPG")
oCht$Delete()

# CLOSE WORKBOOK AND APP
xlWbk$Close(FALSE)
xlApp$Quit()

# RELEASE RESOURCES
oCht <- xlWbk <- xlApp <- NULL    
rm(oCht, xlWbk, xlApp)
gc()

Output (random data/chart) 输出 (随机数据/图表)

数据和图表输出图像

You can do this with vbs. 你可以用vbs做到这一点。 Most vbs is identical to vba so you can write out your dynamic vbs script which includes your macro as text and then call it with shell. 大多数vbs与vba相同,因此您可以编写动态vbs脚本,其中包含宏作为文本,然后使用shell调用它。

Here is a working example: 这是一个工作示例:

fileConn<-file("c:/rworking/test/test.vbs")
writeLines(c("Dim xlApp, xlBook, xlSht",
    "Dim filename",
    "filename = \"c:\\Rworking\\test\\test_import.xlsx\"",
    "Set xlApp = CreateObject(\"Excel.Application\")",
    "xlApp.Visible = True",
    "set xlBook = xlApp.WorkBooks.Open(filename)",
    "set xlSht = xlApp.Worksheets(1)",
    "set rng = xlSht.Range(\"A8:J36\")",
    "rng.CopyPicture",
    "Set oCht = xlApp.Charts",
    "oCht.Add() ",
    "Set oCht = oCht(1)",
    "oCht.paste",
    "oCht.Export \"C:\\rworking\\test\\Test.jpg\", \"JPG\""), 
    fileConn)

close(fileConn)

shell.exec("c:/rworking/test/test.vbs")

Hmm, not sure about posting, maybe it got redundant through Ian´s post. 嗯,不确定发布,也许它通过伊恩的帖子多余。 Its a bit more generic, but I can also remove it. 它更通用,但我也可以删除它。

library(xlsx)

OutputPicFileName <- "Chart.jpg"
ScriptFileName <- "Chart.vbs"
xclFileName <- "test_import.xlsx"
xclRng <- "A8:J36"
file <- system.file("tests", xclFileName, package = "xlsx")
fileDirec <- unlist(strsplit(file, xclFileName))

CreateChart <- function(fileDirec, OutputPicFileName, ScriptFileName, xclRng){
  setwd(fileDirec)
  filePath <- file(paste0(fileDirec, ScriptFileName))
  writeLines(
    c(
      "Dim App, WBook, Sht, Rng, FileName, ChartObj, Chart",
       paste0("FileName = \"", gsub("/", "\\\\", fileDirec), xclFileName ,"\""),
       "Set App = CreateObject(\"Excel.Application\")",
       "Set WBook = App.WorkBooks.Open(FileName)",
       "Set Sht = App.Worksheets(1)",
       paste0("Set Rng = Sht.Range(\"", xclRng,"\")"),
       "Rng.CopyPicture",
       "Set ChartObj = App.Charts",
       "Set Chart = ChartObj.Add() ",
       "Chart.paste",
       paste0("Chart.Export \"", gsub("/", "\\\\", fileDirec) , OutputPicFileName ,"\", \"JPG\"")
    ), 
    filePath
  )
  close(filePath)
  shell.exec(ScriptFileName)
}

CreateChart(fileDirec, OutputPicFileName, ScriptFileName, xclRng)

# Result in: fileDirec

Chart.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM