【发布时间】:2018-11-07 01:00:26
【问题描述】:
我正在编写一个读取 CSV 文件并生成两个 XLSX 文件的代码,具体取决于站点是否需要更新以及位置在哪里。例如,我会有示例数据,例如:
我现在遇到的问题是,我想应用 Excel 表格样式“橄榄绿,表格样式中号 18”。我有一个必须传递工作簿的选项,但由于我不是直接创建工作簿而是读取 CSV 文件,我将如何继续执行此操作?
现在,我只是使用write.xlsx() 将我的数据导出到 Excel,但我想对表格应用格式。
示例代码:
#Determine the input and output parameters
input_file <- choose.files()
output_eu <- "eu.xlsx"
output_noteu <- "noteu.xlsx"
#list of EU countries
eu <- c("Andorra","Austria","Belarus","Belgium","Bosnia and Herzegovina","Bulgaria","Croatia","Czech Republic","Denmark","Estonia","Finland","France","Germany","Greece","Hungary","Iceland","Ireland","Italy","Latvia","Liechtenstein","Lithuania","Luxembourg","Malta","Moldova","Monaco","Montenegro","Netherlands","Norway","Poland","Portugal","Romania","Russia","San Marino","Serbia","Slovakia","Slovenia","Spain","Sweden","Switzerland","Ukraine","United Kingdom")
#reading the csv table
d <- read.table(input_file, sep = ";", header = TRUE, check.names = FALSE)
# get all cases where there is some text in the Update field
updates <- d[d$Update != "", ]
#within updates are there countries in EU
i <- updates$Country %in% eu
eu_up <- updates[i,]
noteu_up <- updates[!i,]
#Creating the excel files
library(openxlsx)
write.xlsx(eu_up, output_eu)
write.xlsx(noteu_up, output_noteu)
Update1:添加了示例代码以显示我使用 1 个 CSV 文件进入两个 excel 文件。
更新代码:
#Determine the input and output parameters
input_file <- choose.files()
output_eu <- "eu.xlsx"
output_noteu <- "noteu.xlsx"
#list of EU countries
eu <- c("Andorra","Austria","Belarus","Belgium","Bosnia and Herzegovina","Bulgaria","Croatia","Czech Republic","Denmark","Estonia","Finland","France","Germany","Greece","Hungary","Iceland","Ireland","Italy","Latvia","Liechtenstein","Lithuania","Luxembourg","Malta","Moldova","Monaco","Montenegro","Netherlands","Norway","Poland","Portugal","Romania","Russia","San Marino","Serbia","Slovakia","Slovenia","Spain","Sweden","Switzerland","Ukraine","United Kingdom")
#reading the csv table
d <- read.table(input_file, sep = ";", header = TRUE, check.names = FALSE)
# get all cases where there is some text in the Update field
updates <- d[d$Update != "", ]
#within updates are there countries in EU
i <- updates$Country %in% eu
eu_up <- updates[i,]
noteu_up <- updates[!i,]
#importing openxlsx library
library(openxlsx)
#create the workbook for each one
wb_eu <- createWorkbook()
wb_xeu <-createWorkbook()
#adding the data to each corresponding workbook
addWorksheet(wb_eu, "European Sites")
addWorksheet(wb_xeu, "Non-European Sites")
#write our tables into each
writeDataTable(wb_eu, 1, eu_up, startRow=1, startCol=1, tableStyle="TableStyleLight11")
writeDataTable(wb_xeu, 1, noteu_up, startRow=1, startCol=1, tableStyle="TableStyleLight11")
#setting our column widths
setColWidths(wb_eu, 1, cols=1:26, widths = "auto")
setColWidths(wb_xeu, 1, cols=1:26, widths = "auto")
#saving our workbooks
saveWorkbook(wb_eu, "European Sites updated.xlsx", overwrite = TRUE)
saveWorkbook(wb_xeu, "Non-European Sites updated.xlsx", overwrite = TRUE)
【问题讨论】:
-
您好,请避免上传您的数据图片,而是上传一小部分数据样本。此外,我们需要查看您的代码才能对其进行测试。尝试使用包含少量数据样本的可重现示例来编辑此问题。
-
阅读社区指南并请发布可重现的示例...你的问题很好,但你问的方式很重要。所以请避免在问题中附加或发布图片
-
有几个选项。几年前,我决定使用
xlsx包来创建格式良好的Excel 文件输出。一个替代方案可能是openxlsx包,它可能在此期间已经成熟。