首页 > 解决方案 > 将字母数字数据导出到 Excel

问题描述

将数据框导出到 Excel 后,我遇到了格式化问题。我计算数据,然后使用掩码公式,如果样本太低,则隐藏一些数据(使用 -- 符号):

ValStat <- function(d,a,i,df){
o <- count(d)
per25 <- round(quantile(d$b, probs = 0.25,type = 6),digits = 0)
per50 <- round(quantile(d$b, probs = 0.5,type = 6),digits = 0)
per75 <- round(quantile(d$b, probs = 0.75,type = 6),digits = 0)

df1 <- InitDf(2)
df <- rbind(df, df1, stringsAsFactors = FALSE)
if(o<22){
df <- FillDf(df,2,i,a,o,"--","--","--")
}
else{
df <- FillDf(df,2,i,a,o,per25,per50,per75)
}
return(df)
}

但是我导出到 excel 的内容如下所示: https ://i.stack.imgur.com/r8zhP.png

所以“--”左对齐(作为文本),数字右对齐。看起来 excel 忽略了我在 R 代码中使用的输出格式:

CellStyle(wb, dataFormat=NULL, alignment=NULL, border=NULL, fill=NULL, font=NULL)
TITLE_STYLE <- CellStyle(wb)+ Font(wb,  heightInPoints=16,  isBold=TRUE, underline=1)
SUB_TITLE_STYLE <- CellStyle(wb) + Font(wb,  heightInPoints=11, isItalic=FALSE, isBold=TRUE)
TABLE_COLNAMES_STYLE <- CellStyle(wb) + Font(wb, isBold=TRUE) +Alignment(wrapText=TRUE, horizontal="ALIGN_CENTER") + 
Border(color="black", position=c("TOP", "BOTTOM"), pen=c("BORDER_THIN", "BORDER_THICK"))
COL_STYLE <- CellStyle(wb) + Alignment(wrapText=FALSE, horizontal="ALIGN_RIGHT")

有没有办法让“--”和数字以相同的方式格式化?向右会很棒。

标签: formattingformatrstudioxlsxalphanumeric

解决方案


推荐阅读