问题描述
单位要求日报必须用pdf格式的作为附件。之前都是直接利用openxlsx包生成.xlsx文件的,这个.xlsx文件里除了标题、表头、数据外还有其他的一些要素,比如数据单位、报告期、制表、复核、说明等一系列“乱七八糟”的东西。所以想在原有代码基础上再增加一部分代码将生成的.xlsx文件通过虚拟打印机打印成.pdf格式的文件。看到一篇用Java来实现的,JAVA 调用打印机输出PDF文件,看起来比较复杂,用R实现起来估计也不简单。
示例代码
##########################################################################
# 定义报告期
report_period = c("2020-04-08")
##########################################################################
# 计算报告期年、月、日
report_year = as.numeric(substr(report_period, 1, 4))
report_month = as.numeric(substr(report_period, 6, 7))
report_day = as.numeric(substr(report_period, 9, 10))
##########################################################################
daily = data.frame(
x1 = 1:21,
x2 = 1:21,
x3 = 1:21,
x4 = 1:21,
x5 = 1:21,
x6 = 1:21,
x7 = 1:21,
x8 = 1:21
)
##########################################################################
# 数据导入到Excel文件中
## 新建一个工作簿wb、新建一个工作表daily
library(openxlsx)
library(lubridate)
wb = createWorkbook()
addWorksheet(wb, "daily", gridLines = FALSE)
## 设置全局列宽、行高、冻结活动单元格
setColWidths(
wb,
"daily",
cols = 1:8,
widths = c(26, rep(15.5, 5), 12, 18)
)
setRowHeights(
wb,
"daily",
rows = 1:(nrow(daily) + 4),
heights = c(20, 35, rep(20, nrow(daily) + 2))
)
## 打印设置
pageSetup(
wb,
"daily",
orientation = "landscape",
scale = 97,
left = 0.7,
right = 0.7,
top = 0.75,
bottom = 0.75,
header = 0.3,
footer = 0.3,
fitToWidth = TRUE,
fitToHeight = TRUE,
paperSize = 9,
printTitleRows = NULL,
printTitleCols = NULL
)
## 保密提示
secret = c("★内部资料、严格保密")
style_secret = createStyle(
halign = "right",
valign = "center",
wrapText = TRUE,
fontColour = "red",
fontSize = 11,
fontName = "Arial"
)
mergeCells(
wb, "daily",
rows = 1:1,
cols = 1:8
)
addStyle(
wb,
"daily",
style = style_secret,
rows = 1:1,
cols = 1:8
)
writeData(wb, "daily", secret,
startRow = 1)
## 大标题设置
title = paste0(
year(report_period), "年" ,
month(report_period), "月",
day(report_period),
"日经营数据日报"
)
style_title = createStyle(
halign = "center",
valign = "center",
wrapText = TRUE,
textDecoration = c("bold"),
fontColour = "black",
fontSize = 20,
fontName = "Arial"
)
mergeCells(
wb, "daily",
rows = 2:2,
cols = 1:8
)
addStyle(
wb,
"daily",
style = style_title,
rows = 2:2,
cols = 1:8
)
writeData(wb, "daily", title,
startRow = 2)
## 报告期设置
date = paste0("报告日期:", Sys.Date())
style_date = createStyle(
halign = "right",
valign = "center",
wrapText = TRUE,
fontColour = "black",
fontSize = 11,
fontName = "Arial")
mergeCells(wb, "daily",
rows = 3:3, cols = 6:7)
addStyle(wb, "daily", style = style_date,
rows = 3:3, cols = 6:7)
writeData(wb, "daily", date,
startRow = 3, startCol = 6)
## 数据单位设置
unit = "单位:亿元、%"
style_unit = createStyle(
halign = "center",
valign = "center",
wrapText = TRUE,
fontColour = "black",
fontSize = 11,
fontName = "Arial")
mergeCells(wb, "daily",
rows = 3:3, cols = 8:8)
addStyle(
wb,
"daily",
style = style_unit,
rows = 3:3,
cols = 8:8
)
writeData(wb, "daily", unit,
startRow = 3, startCol = 8)
## 表头部分设置
style_header = createStyle(
textDecoration = "Bold",
halign = "center",
valign = "center",
wrapText = TRUE,
border = "TopBottomLeftRight",
borderColour = "black",
fontColour = "white",
fgFill = "#4F81BD",
fontSize = 11,
fontName = "Arial")
## 数据部分设置
style_data = createStyle(
valign = "center",
border = "TopBottomLeftRight",
borderColour = "black",
fontSize = 11,
fontName = "Arial",
numFmt = "0.00" )
addStyle(
wb,
"daily",
style = style_data,
rows = 5:(nrow(daily) + 4),
cols = 1:8,
gridExpand = T
)
writeData(
wb,
"daily",
daily,
headerStyle = style_header,
startRow = 4
)
## 保存工作簿
saveWorkbook(wb, overwrite = TRUE, "daily.xlsx")
#########################################################################
一些思考
考虑到通过.xlsx格式打印成.pdf格式用R实现由点绕,且不一定容易。我现在考虑直接输出为.pdf格式,而不是数据-->.xlsx-->.pdf。但是那些“乱七八糟”的数据单位、报告期、制表、复核、说明等要素该如何放进去呢?位置又该怎么精准控制呢?