0
  7   ˆl–Ðz¾”*a,je@à¸ÓêbÑ¿�þZo¬Ü­<ûíe ?Â„�	¿ÁÀ¿  $   H403239_•I|¥‰ÓMjqØ‚¦ ë<óo¬ÂÁ ?÷     ---
title: "Introduction"
author: "Alexander Walker, Philipp Schauberger"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

## Basic Examples


### write.xlsx

The simplest way to write to a workbook is write.xlsx().
By default, write.xlsx calls writeData.  If asTable is TRUE write.xlsx will write x as
an Excel table.

```{r include=TRUE,tidy=TRUE, eval = FALSE ,highlight=TRUE}
## write to working directory
library(openxlsx)
write.xlsx(iris, file = "writeXLSX1.xlsx")
write.xlsx(iris, file = "writeXLSXTable1.xlsx", asTable = TRUE)
```

### write list of data.frames to xlsx-file

```{r  include=TRUE,tidy=TRUE, eval = FALSE ,highlight=TRUE}
## write a list of data.frames to individual worksheets using list names as worksheet names
l <- list("IRIS" = iris, "MTCARS" = mtcars)
write.xlsx(l, file = "writeXLSX2.xlsx")
write.xlsx(l, file = "writeXLSXTable2.xlsx", asTable = TRUE)
```


### write.xlsx also accepts styling parameters


#### The simplest way is to set default options and set column class

```{r include=TRUE,tidy=TRUE, eval = FALSE ,highlight=TRUE}
options("openxlsx.borderColour" = "#4F80BD")
options("openxlsx.borderStyle" = "thin")
options("openxlsx.dateFormat" = "mm/dd/yyyy")
options("openxlsx.datetimeFormat" = "yyyy-mm-dd hh:mm:ss")
options("openxlsx.numFmt" = NULL) ## For default style rounding of numeric columns

df <- data.frame("Date" = Sys.Date()-0:19, "LogicalT" = TRUE,
                 "Time" = Sys.time()-0:19*60*60,
                 "Cash" = paste("$",1:20), "Cash2" = 31:50,
                 "hLink" = "https://CRAN.R-project.org/",
                 "Percentage" = seq(0, 1, length.out=20),
                 "TinyNumbers" = runif(20) / 1E9,  stringsAsFactors = FALSE)

class(df$Cash) <- "currency"
class(df$Cash2) <- "accounting"
class(df$hLink) <- "hyperlink"
class(df$Percentage) <- "percentage"
class(df$TinyNumbers) <- "scientific"

write.xlsx(df, "writeXLSX3.xlsx")
write.xlsx(df, file = "writeXLSXTable3.xlsx", asTable = TRUE)


```


## Workbook styles

### define a style for column headers
```{r include=TRUE,tidy=TRUE, eval = FALSE ,highlight=TRUE}
hs <- createStyle(fontColour = "#ffffff", fgFill = "#4F80BD",
                  halign = "center", valign = "center", textDecoration = "Bold",
                  border = "TopBottomLeftRight", textRotation = 45)

write.xlsx(iris, file = "writeXLSX4.xlsx", borders = "rows", headerStyle = hs)
write.xlsx(iris, file = "writeXLSX5.xlsx", borders = "columns", headerStyle = hs)

write.xlsx(iris, "writeXLSXTable4.xlsx", asTable = TRUE,
headerStyle = createStyle(textRotation = 45))


```

### When writing a list, the stylings will apply to all list elements
```{r include=TRUE,tidy=TRUE, eval = FALSE ,highlight=TRUE}
l <- list("IRIS" = iris, "colClasses" = df)
write.xlsx(l, file = "writeXLSX6.xlsx", borders = "columns", headerStyle = hs)
write.xlsx(l, file = "writeXLSXTable5.xlsx", asTable = TRUE, tableStyle = "TableStyleLight2")

openXL("writeXLSX6.xlsx")
openXL("writeXLSXTable5.xlsx")
```


### write.xlsx returns the workbook object for further editing
```{r include=TRUE,tidy=TRUE, eval = FALSE ,highlight=TRUE}
wb <- write.xlsx(iris, "writeXLSX6.xlsx")
setColWidths(wb, sheet = 1, cols = 1:5, widths = 20)
saveWorkbook(wb, "writeXLSX6.xlsx", overwrite = TRUE)
```



## Workbook creation walk-through

### create workbook and set default border Colour and style



```{r include=TRUE,tidy=TRUE, eval = FALSE ,highlight=TRUE}
require(ggplot2)
wb <- createWorkbook()
options("openxlsx.borderColour" = "#4F80BD")
options("openxlsx.borderStyle" = "thin")
modifyBaseFont(wb, fontSize = 10, fontName = "Arial Narrow")
```


### Add Sheets 


```{r include=TRUE,tidy=TRUE, eval = FALSE ,highlight=TRUE}
addWorksheet(wb, sheetNam