问题
I have workbooks with several (70+) worksheets. Each worksheet is a station name. Each worksheet has different number of comment line starting with # which I want to eliminate, then read the header from when the comments end and assign them to the column names. I was trying to use RODBC which works on a 32-bit machine/R/EXCEL but is not working on a 64-bit system. Below is what I have using RODBC. How can I do the same using XLconnect or something else for a 64-bit system?
library(RODBC)
### Connect to Excel Files
# Input
excel.input <- odbcConnectExcel2007("Diurnal Data 2013.xlsx")
# Output
excel.output <- odbcConnectExcel("Diurnal Data 2013 edit.xls", readOnly=FALSE)
### Pull and Format Data
#extract and keep Stations sheet
Stations <-sqlFetch(excel.input, "Stations", na.strings=c("","-"))
sqlSave(excel.output,Stations, rownames = FALSE)
##Loop through data pull and formatting for each site id
sites <- gsub("[[:punct:]]","",sqlTables(excel.input)[,"TABLE_NAME"])
len <- length(sites)
for(i in 2:len) { # Omit the 1st worksheet
sheet <- sites[i]
data <- sqlFetch(excel.input, sheet, na.strings=c("","-")) ##retrieve data from each worksheet ith sheet
data1 <- subset(data, !grepl("^#", data[,1])) ## Removes rows starting with #(hashtag) on ith sheet
# Rename to appropriate column headers
A <- t(data1[1,])
names(data1)<- A[,1]
# Remove unwanted rows
data2 <- data1[c(-1,-2),]
head(data2)
# Write new ith worksheet into output file.
sqlSave(excel.output,data2, tablename=sheet, rownames = FALSE)
# End loop after running through all sheets.
}
###Close connections to Excel files before opening
odbcCloseAll()
来源:https://stackoverflow.com/questions/23413583/read-data-from-several-worksheets-from-workbook-and-eliminate-comments