将文字附加到Excel文件

时间:2018-07-17 16:29:01

标签: r

我有一个已经写入代码的Excel文件。我想添加另一行代码。假设我想在已经存在的数据下添加“ Hello”。我尝试过:

write.xlsx("Hello", "text.xlsx", sheetName = "Sheet1", append = TRUE)

出现错误:

Error in .jcall(wb, "Lorg/apache/poi/ss/usermodel/Sheet;", "createSheet",  : 
  java.lang.IllegalArgumentException: The workbook already contains a sheet of this name

我想知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

根据您想要的复杂程度,这是一种将文本加载到excel中的简单方法

path = r'ENTER FILE PATH AND DESIRED FOLDER NAME HERE FOLLOWED BY .csv' #plug in  the     
folder path you want to save this as ending it with the filename.csv of your choice
with open(path, 'w', newline='', encoding='utf-8') as resultfile:
wr = csv.writer(resultfile, dialect='excel')
wr.writerow([x[0] for x in cursor.description])

for record in records:
  wr.writerow(record)