简体   繁体   English

合并不同格式的csv文件,并用不同的表格制作成一个excel

[英]Combine csv files of different formats and make into one excel with different sheets

I have four csv files with different formats and variables, combining these 4 CSV files into one excel file using below code 我有四个具有不同格式和变量的csv文件,使用以下代码将这4个CSV文件合并为一个excel文件

library(rJava)
library(xlsx)
rm(list = ls())

# getting the path of all reports (they are in csv format)
files <- list.files(pattern = "\\.csv$")

# creating work book
wb <- createWorkbook()

# going through each csv file
for (item in files)
{
    # create a sheet in the workbook
    sheet <- createSheet(wb, sheetName=strsplit(item,"[.]")[[1]][1])

    # add the data to the new sheet
    addDataFrame(read.csv(item), sheet,row.names=FALSE)
}


# saving the workbook
saveWorkbook(wb, "crosstabs of data.xlsx")

In csv file one sheet the variable name is source / Medium But it is appeared in output excel file as Source...Medium, % New Sessions variable is appeared as X..New.Sessions and all variables delimited space occupied with . 在csv文件中,一个变量名称为source / Medium,但在输出excel文件中显示为Source ... Medium,%New Sessions变量显示为X..New.Sessions,所有变量都用分隔。 in output excel file How to overcome this i need what ever the variable names in CSV files same as in output Excel file 在输出excel文件中如何解决此问题,我需要CSV文件中与输出Excel文件中相同的变量名

This problem is due to read.csv changing names of headers. 此问题是由于read.csv更改标头名称引起的。 Column headers like gi/joe will be converted in gi.joe if we do read.csv with header=T . 如果我们使用header=T进行read.csv转换,则gi/joe类的列标题将在gi.joe进行转换。 So one need to convert just the header names again using: 因此,需要使用以下方法再次仅转换标头名称:

names(df) <- gsub("\\.","/",names(df))

OR if acceptable do simply (read headers as data): 或者,如果可以接受,则简单地进行操作(将标头读取为数据):

addDataFrame(read.csv(item,header=F), sheet,row.names=FALSE)

On a separate note looks like names like gi/joe are not allowed as excel sheet names. 在单独的注释上看起来像gi/joe类的名称不允许作为excel工作表名称。 Now to validate limitation in excel end open excel and try to name a sheet hi/5 . 现在要验证excel中的限制,请打开excel并尝试将其命名为hi/5 One should get error saying The sheet name contains invalid characters: : \\ / ? * [ ]. 会出现一个错误,说工作The sheet name contains invalid characters: : \\ / ? * [ ]. The sheet name contains invalid characters: : \\ / ? * [ ]. [I am testing this on mac excel 15.19.1] [我正在Mac excel 15.19.1上对此进行测试]

暂无
暂无

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

相关问题 将不同的csv文件另存为具有多个工作表的一个excel工作簿 - Save different csv files as one excel workbook with multiple sheets 在 R 的一个工作簿中将多个 excel 文件组合成不同的 excel 工作表 - Combing multiple excel files into different excel sheets in one workbook in R 将csv文件合并为1个不同列的文件 - Combine csv files into 1 with different columns 将一些csv文件合并为一个不同数量的列 - Combine some csv files into one - different number of columns 从 R 中不同格式的多个 excel 文件中读取日期 - read dates from multiple excel files with different formats in R 如何通过使用R将每个文件的数据添加为附加行来将不同的.csv文件组合为一个完整文件? - How to combine different .csv files to one complete file by adding the data of every file as an additional row using R? 从R中不同文件夹中的Excel文件导入图纸 - Import sheets from Excel files located in different folder in R 在r中加载不同时间格式的csv文件 - load csv file with different time formats in r 在R中以不同的格式导入带有日期和时间列的csv文件:有没有办法对其进行标准化? - Importing csv files with date and time column in different formats in R: is there a way to standardize it? 如何使用MS Access将具有不同年份的相似信息的两个Excel工作表结合在一起? - How to combine two excel-sheets with similar information for different years using MS access?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM