openxlsx 将单元格格式化为带逗号和小数位的数字
openxlsx format cells as number with comma and decimal places
如何使用 openxlsx 创建一个 excel 文件,其中数字格式包括逗号千位分隔符和 2 个小数位?我试过下面的代码,但在格式上没有运气。
# Create Customer Dataset
cust <- data.table(Customer = c("Sue", "Ben", "Jason", "Cody"), Sales =
c(5654.3456, 29384.4, 729, .4093))
# Start Workbook
wb <- createWorkbook()
# Set Sheet Name
sheet = "Customers Report"
# Initiate worksheet within workbook
addWorksheet(wb = wb, sheet = sheet)
# Add Formatting to Spreadsheet
addStyle(wb = wb, sheet = sheet, style = createStyle(numFmt = "NUMBER"), rows = 2:6, cols = 2)
addStyle(wb = wb, sheet = sheet, style = createStyle(numFmt = "COMMA"), rows = 2:6, cols = 2, stack = TRUE)
# Write Customer Dataset to Spreadsheet
writeData(wb = wb, sheet = sheet, x = cust, headerStyle =
createStyle(textDecoration = "bold"))
# Write Workbook to File
saveWorkbook(wb = wb, file = "~/Desktop/Customer_Report.xlsx", overwrite = TRUE)
您可以在添加千位格式之前设置 2 位小数的默认格式。
wb = createWorkbook()
options("openxlsx.numFmt" = "0.00") # 2 decimal cases formating
styleT <- createStyle(numFmt = "#,##0.00") # create thousands format
addStyle(wb, sheetName, styleT,
rows = 'yourrows',cols = 'yourcols',
gridExpand = T, stack = T) # add thousands format to designated cols and rows
这将确保千位格式发生在已经只有 2 位小数的值上。
我在写问题时找到了答案,但我想我会继续 post 以防其他人有同样的问题。我通过 github openxlsx issue #75 找到了这个答案。请看下面的代码块:
# Add Formatting to Spreadsheet
addStyle(wb = wb, sheet = sheet, style = createStyle(numFmt = "#,##0.00"), rows = 2:6, cols = 2)
我发现当您希望那里有一个数字时使用“0”,而不管它是否为 0,并且您使用“#”来表示潜在数字的占位符。例如,如果数字是如上所示的 .4093,那么它将被格式化为 0.41,如果数字是如上所示的 29384.4,那么它将被格式化为 29,384.40。
如何使用 openxlsx 创建一个 excel 文件,其中数字格式包括逗号千位分隔符和 2 个小数位?我试过下面的代码,但在格式上没有运气。
# Create Customer Dataset
cust <- data.table(Customer = c("Sue", "Ben", "Jason", "Cody"), Sales =
c(5654.3456, 29384.4, 729, .4093))
# Start Workbook
wb <- createWorkbook()
# Set Sheet Name
sheet = "Customers Report"
# Initiate worksheet within workbook
addWorksheet(wb = wb, sheet = sheet)
# Add Formatting to Spreadsheet
addStyle(wb = wb, sheet = sheet, style = createStyle(numFmt = "NUMBER"), rows = 2:6, cols = 2)
addStyle(wb = wb, sheet = sheet, style = createStyle(numFmt = "COMMA"), rows = 2:6, cols = 2, stack = TRUE)
# Write Customer Dataset to Spreadsheet
writeData(wb = wb, sheet = sheet, x = cust, headerStyle =
createStyle(textDecoration = "bold"))
# Write Workbook to File
saveWorkbook(wb = wb, file = "~/Desktop/Customer_Report.xlsx", overwrite = TRUE)
您可以在添加千位格式之前设置 2 位小数的默认格式。
wb = createWorkbook()
options("openxlsx.numFmt" = "0.00") # 2 decimal cases formating
styleT <- createStyle(numFmt = "#,##0.00") # create thousands format
addStyle(wb, sheetName, styleT,
rows = 'yourrows',cols = 'yourcols',
gridExpand = T, stack = T) # add thousands format to designated cols and rows
这将确保千位格式发生在已经只有 2 位小数的值上。
我在写问题时找到了答案,但我想我会继续 post 以防其他人有同样的问题。我通过 github openxlsx issue #75 找到了这个答案。请看下面的代码块:
# Add Formatting to Spreadsheet
addStyle(wb = wb, sheet = sheet, style = createStyle(numFmt = "#,##0.00"), rows = 2:6, cols = 2)
我发现当您希望那里有一个数字时使用“0”,而不管它是否为 0,并且您使用“#”来表示潜在数字的占位符。例如,如果数字是如上所示的 .4093,那么它将被格式化为 0.41,如果数字是如上所示的 29384.4,那么它将被格式化为 29,384.40。