简体   繁体   English

如何使用 R 的 xlsx package 对齐 XLSX 文件的单元格?

[英]How to align the cells of an XLSX file using R's xlsx package?

When creating an XLSX file using R's xlsx package, by default, columns with strings are justified to the left by default, and columns with integers are justified to the right (columns with a mix of integers and strings are also justified to the left).使用 R 的 xlsx package 创建 XLSX 文件时,默认情况下,带有字符串的列默认左对齐,带有整数的列向右对齐(整数和字符串混合的列也向左对齐)。 Ultimately, I want to standardize all columns by aligning them all to the left, but I'm having trouble doing so using xlsx.最终,我想通过将所有列都向左对齐来标准化所有列,但是我在使用 xlsx 时遇到了麻烦。 Using the below example, how can I align all cells to the left?使用下面的示例,如何将所有单元格左对齐?

library(xlsx)

# Creating dataframe.
df <- data.frame(c(1, 2, 3),
                 c("one", "two", "three"),
                 c("1", "2", "3"))

# Creating a workbook using the XLSX package.
wb <- xlsx::createWorkbook(type = "xlsx")

# Creating a sheet inside the workbook.
sheet <- xlsx::createSheet(wb, sheetName = "Sheet0")

# Adding the full dataset into the sheet.
xlsx::addDataFrame(df, sheet, startRow = 1, startCol = 1, row.names = FALSE, col.names = FALSE)

# Saving the workbook.
xlsx::saveWorkbook(wb, "df.xlsx")

I've solved the above question with the solution seen below: 我用下面的解决方案解决了上述问题:

library(xlsx)

# Creating dataframe.
df <- data.frame(c(1, 2, 3),
                 c("one", "two", "three"),
                 c("1", "2", "3"))

# Creating a workbook using the XLSX package.
wb <- xlsx::createWorkbook(type = "xlsx")

# Creating a sheet inside the workbook.
sheet <- xlsx::createSheet(wb, sheetName = "Sheet0")

# Adding the full dataset into the sheet.
xlsx::addDataFrame(df, sheet, startRow = 1, startCol = 1, row.names = FALSE, col.names = FALSE)

# Creating cell style needed to left-justify text.
cs <- CellStyle(wb) + Alignment(horizontal = "ALIGN_LEFT")

# Selecting rows to apply cell style to.
all.rows <- getRows(sheet, rowIndex = 1:nrow(df))

# Selecting cells within selected rows to apply cell style to.
all.cells <- getCells(all.rows)

# Applying cell style to selected cells.
invisible(lapply(all.cells, setCellStyle, cs))

# Saving the workbook.
xlsx::saveWorkbook(wb, "df.xlsx")

The solution involved the creation of a cell style which I stored in cs . 解决方案涉及创建我存储在cs中的单元格样式。 Next, I selected each row and each cell each contained and applied the cell style to them using lapply() . 接下来,我选择了每个行和每个包含的单元格,并使用lapply()将单元格样式应用于它们。

Note for future readers: the solution described above eats a lot of memory. I had a 10Mb data.frame and the corresponding all.cells object turned out to be 1.1Gb.未来读者注意:上面描述的解决方案吃了很多 memory。我有一个 10Mb data.frame 和相应的all.cells object 结果是 1.1Gb。

If the only purpose is to have left-alignment of the cell, this can easier done like this:如果唯一的目的是使单元格左对齐,则可以像这样更容易地完成:

openxlsx::write.xlsx(
    x = df,
    file = file,
    startRow = 1, 
    startColumn = 1, 
    rowNames = FALSE, 
    colNames = TRUE
  )

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

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