0

I would like to export tables for Excel with formatting from RStudio. Packages as openxlsx work perfect, but I cannot use them in my work situation. It is not possible to install Java or Rtools applications.

The only way to export tables with formatting for Excel seems to copy HTML output (from Markdown), or open the HTML file (from Markdown).

Are there smart starting points that I can use? Examples? CSS examples (for Markdown) that will work for excel?

The only thing that seems to work at the moment is to copy the HTML from the RStudio: Prview HTML. Other ways don't give good results for (my) formatting.

Things I would like to do is header (first row) color and two different colors for all the rest of the rows.

One of the problems is that Excel can only work with one background color.


Update:

So I have to assume that openxlsx will not work for me. The architect.css file from the prettydoc package is showing the same result. See the Markdown code. The output table in the RStudio preview is good. Also in browsers. Copying to Excel is only working from the RStudio preview.

When you open the HTML-file in Excel, Excel is only showing one background color. White in this case.

What I am looking for (I think) is more basic CSS, with a focus on table style. (with two background colors as by :nth-child(n)).

<style>
/*! normalize.css v3.0.2 | MIT License | git.io/normalize */

/**
 * 1. Set default font family to sans-serif.
 * 2. Prevent iOS text size adjust after orientation change, without disabling
 *    user zoom.
 */

@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 400;
  src: local('Open Sans'), local('OpenSans'), url('fonts/open-sans-400.woff') format('woff');
}
@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 700;
  src: local('Open Sans Bold'), local('OpenSans-Bold'), url('fonts/open-sans-700.woff') format('woff');
}

html {
  font-family: sans-serif; /* 1 */
  -webkit-text-size-adjust: 100%; /* 2 */
  -ms-text-size-adjust: 100%; /* 2 */
}

/**
 * Remove default margin.
 */

body {
  margin: 0;
}

/* HTML5 display definitions
   ========================================================================== */

/**
 * Correct `block` display not defined for any HTML5 element in IE 8/9.
 * Correct `block` display not defined for `details` or `summary` in IE 10/11
 * and Firefox.
 * Correct `block` display not defined for `main` in IE 11.
 */

article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
main,
menu,
nav,
section,
summary {
  display: block;
}

/**
 * 1. Correct `inline-block` display not defined in IE 8/9.
 * 2. Normalize vertical alignment of `progress` in Chrome, Firefox, and Opera.
 */

audio,
canvas,
progress,
video {
  display: inline-block; /* 1 */
  vertical-align: baseline; /* 2 */
}

/**
 * Prevent modern browsers from displaying `audio` without controls.
 * Remove excess height in iOS 5 devices.
 */

audio:not([controls]) {
  display: none;
  height: 0;
}

/**
 * Address `[hidden]` styling not present in IE 8/9/10.
 * Hide the `template` element in IE 8/9/11, Safari, and Firefox < 22.
 */

[hidden],
template {
  display: none;
}

/* Links
   ========================================================================== */

/**
 * Remove the gray background color from active links in IE 10.
 */

a {
  background-color: transparent;
}

/**
 * Improve readability when focused and also mouse hovered in all browsers.
 */

a:active,
a:hover {
  outline: 0;
}

/* Text-level semantics
   ========================================================================== */

/**
 * Address styling not present in IE 8/9/10/11, Safari, and Chrome.
 */

abbr[title] {
  border-bottom: 1px dotted;
}

/**
 * Address style set to `bolder` in Firefox 4+, Safari, and Chrome.
 */

b,
strong {
  font-weight: bold;
}

/**
 * Address styling not present in Safari and Chrome.
 */

dfn {
  font-style: italic;
}

/**
 * Address variable `h1` font-size and margin within `section` and `article`
 * contexts in Firefox 4+, Safari, and Chrome.
 */

h1 {
  margin: 0.67em 0;
  font-size: 2em;
}

/**
 * Address styling not present in IE 8/9.
 */

mark {
  color: #000;
  background: #ff0;
}

/**
 * Address inconsistent and variable font size in all browsers.
 */

small {
  font-size: 80%;
}

/**
 * Prevent `sub` and `sup` affecting `line-height` in all browsers.
 */

sub,
sup {
  position: relative;
  font-size: 75%;
  line-height: 0;
  vertical-align: baseline;
}

sup {
  top: -0.5em;
}

sub {
  bottom: -0.25em;
}

/* Embedded content
   ========================================================================== */

/**
 * Remove border when inside `a` element in IE 8/9/10.
 */

img {
  border: 0;
}

/**
 * Correct overflow not hidden in IE 9/10/11.
 */

svg:not(:root) {
  overflow: hidden;
}

/* Grouping content
   ========================================================================== */

/**
 * Address margin not present in IE 8/9 and Safari.
 */

figure {
  margin: 1em 40px;
}

/**
 * Address differences between Firefox and other browsers.
 */

hr {
  height: 0;
  -moz-box-sizing: content-box;
  box-sizing: content-box;
}

/**
 * Contain overflow in all browsers.
 */

pre {
  overflow: auto;
}

/**
 * Address odd `em`-unit font size rendering in all browsers.
 */

code,
kbd,
pre,
samp {
  font-family: monospace, monospace;
  font-size: 1em;
}

/* Tables
   ========================================================================== */

/**
 * Remove most spacing between table cells.
 */

table {
  border-spacing: 0;
  border-collapse: collapse;
  width: 50%;
  margin: 20px auto;
}

th, td {
  border-bottom: 1px solid #bbbbbb;
  text-align: left;
  padding: 10px;
}

th {
  background-color: #63a0e1;
  color: white;
}

tr:nth-child(odd) {
  background-color: #eeeeee;
}

tr:nth-child(even) {
  background-color: #ffffff;
}

/* LAYOUT STYLES */
body {
  font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  font-size: 16px;
  font-weight: 400;
  line-height: 1.5;
  color: #666;
  background: #fafafa url(images/body-bg.jpg) 0 0 repeat;
}

p {
  margin-top: 0;
}

a {
  color: #2879d0;
}
a:hover {
  color: #2268b2;
}

header {
  padding-top: 40px;
  padding-bottom: 20px;
  background: #2e7bcf url(images/header-bg.jpg) 0 0 repeat-x;
  border-bottom: solid 1px #275da1;
  text-align: center;
}

header h1 {
  margin-top: 0;
  margin-bottom: 0.5em;
  font-size: 2em;
  font-weight: bold;
  line-height: 1;
  color: #fff;
  letter-spacing: -1px;
}

header h2 {
  margin-top: 0;
  margin-bottom: 1em;
  font-size: 1.5em;
  font-weight: normal;
  line-height: 1.3;
  color: #9ddcff;
  letter-spacing: 0;
}

header h3 {
  margin-top: 0;
  margin-bottom: 1em;
  font-size: 1.2em;
  font-weight: normal;
  line-height: 1.2;
  color: #9ddcff;
  letter-spacing: 0;
}

.inner, .toc {
  position: relative;
  width: 840px;
  font-size: 1.1em;
  margin: 0 auto;
}

.toc {
  padding-top: 1em;
  padding-bottom: 0;
}

.toc ul {
  margin-bottom: 0;
}

#content-wrapper {
  padding-top: 30px;
  border-top: solid 1px #fff;
}

#main-content img {
  max-width: 100%;
}

code {
  padding: 0 3px;
  background-color: #f2f8fc;
  border: solid 1px #dbe7f3;
}

pre {
  padding: 20px;
  overflow: auto;
  text-shadow: none;
  background: #fff;
  border: solid 1px #f2f2f2;
  font-size: 0.9em;
}
pre code {
  padding: 0;
  color: #2879d0;
  background-color: #fff;
  border: none;
}

ul, ol, dl {
  margin-bottom: 20px;
}


/* COMMON STYLES */

hr {
  height: 1px;
  margin-top: 1em;
  margin-bottom: 1em;
  border: 0;
  background: #aaa;
  background-image: linear-gradient(to right, #eee, #aaa, #eee);
}

form {
  padding: 20px;
  background: #f2f2f2;

}


/* GENERAL ELEMENT TYPE STYLES */

#main-content h1 {
  margin-top: 0;
  margin-bottom: 0;
  font-size: 2em;
  font-weight: bold;
  color: #474747;
  letter-spacing: -1px;
}

#main-content h1:before {
  padding-right: 0.3em;
  margin-left: -0.8em;
  color: #9ddcff;
  content: "/";
}

#main-content h2 {
  margin-bottom: 8px;
  font-size: 1.5em;
  font-weight: bold;
  color: #474747;
}
#main-content h2:before {
  padding-right: 0.3em;
  margin-left: -1.2em;
  content: "//";
  color: #9ddcff;
}

#main-content h3 {
  margin-top: 24px;
  margin-bottom: 8px;
  font-size: 1.2em;
  font-weight: bold;
  color: #474747;
}

#main-content h3:before {
  padding-right: 0.3em;
  margin-left: -1.7em;
  content: "///";
  color: #9ddcff;
}

#main-content h4 {
  margin-bottom: 8px;
  font-size: 1.1em;
  font-weight: bold;
  color: #474747;
}

h4:before {
  padding-right: 0.3em;
  margin-left: -2em;
  content: "////";
  color: #9ddcff;
}

#main-content h5 {
  margin-bottom: 8px;
  font-size: 1em;
  color: #474747;
}
h5:before {
  padding-right: 0.3em;
  margin-left: -2.4em;
  content: "/////";
  color: #9ddcff;
}

#main-content h6 {
  margin-bottom: 8px;
  font-size: 0.9em;
  color: #474747;
}
h6:before {
  padding-right: 0.3em;
  margin-left: -3em;
  content: "//////";
  color: #9ddcff;
}

p {
  margin-bottom: 20px;
}

a {
  text-decoration: none;
}

p a {
  font-weight: 400;
}

blockquote {
  padding: 0 0 0 30px;
  margin-bottom: 20px;
  font-size: 1.1em;
  border-left: 10px solid #e9e9e9;
}

ul, ol {
  padding-left: 30px;
}

dl dd {
  font-style: italic;
  font-weight: 100;
}

/* MISC */
.clearfix:after {
  display: block;
  height: 0;
  clear: both;
  visibility: hidden;
  content: '.';
}

.clearfix {display: inline-block;}
* html .clearfix {height: 1%;}
.clearfix {display: block;}

/* #Media Queries
================================================== */

/* All Mobile Sizes (devices and browser) */
@media only screen and (max-width: 850px) {
  .toc, .inner {
    width: 93%;
    font-size: 1em;
  }
  header {
    padding: 10px 0;
  }
  header h1, header h2 {
    width: 100%;
  }
  header h1 {
    font-size: 1.75em;
  }
  header h2 {
    font-size: 1.2em;
  }
  header h3 {
    font-size: 1em;
  }
  #main-content h1:before,
  #main-content h2:before,
  #main-content h3:before,
  #main-content h4:before,
  #main-content h5:before,
  #main-content h6:before {
    padding-right: 0;
    margin-left: 0;
    content: none;
  }
}

</style>



```{r, echo=FALSE, results='asis'}
knitr::kable(head(iris, 10), full_width = F)
```
Peter P.
  • 51
  • 7
  • Can you provide a reproducible example of your current HTML copy/paste solution? – Mako212 Jul 18 '18 at 15:13
  • I am realising myself that this is looking like I want to go the easy way. But at the moment not. It's not my CSS code. It is somebody else's. Not being able to write CSS from scratch could be the problem. If I find the time I will try to edit the Knitr CSS, so I can provide an example – Peter P. Jul 18 '18 at 15:39
  • Well another thing is that `openxlsx` doesn't require Java, and the `Rtools` requirement was recently removed too. You might try installing the latest version from GitHub if you still get the error: `require(devtools); install_github("awalker89/openxlsx")` – Mako212 Jul 18 '18 at 15:52
  • ok, Mako, openxlsx would surely make it unnecessary to mess around with this workaround. I'll retry to install when I am back at work tomorrow. If not succesfull, I will post an example of a Markdown script, with CSS code. Thanks! – Peter P. Jul 18 '18 at 16:26
  • Normal reinstall (after deleting openxlsx) is still giving the message about Rtools. install_github("awalker89/openxlsx") does not work for me because of "Could not resolve host: raw.githubusercontent.com". I tried to manually download and install of a development version (openxlsx-master?), but this was not succesfull. Perhaps manual installation is still an option. If not openxlsx is seems not available for me. – Peter P. Jul 19 '18 at 07:56
  • Mako, could you please give a link to a source? "Rtools requirement was recently removed too" – Peter P. Jul 20 '18 at 07:38
  • This commit "remove Rtools dependency" https://github.com/awalker89/openxlsx/commit/c02cae8816980798a9afc353cf6d3f7a64410a15 – Mako212 Jul 20 '18 at 15:52
  • https://github.com/awalker89/openxlsx/issues/130 – Mako212 Jul 20 '18 at 15:59
  • Great, nice to see how these things go. You are right. The dependency should be gone with version 4.1.0. Perhaps there is a process on my computer that still triggers the (RTools) dependency error. May be a security thing? – Peter P. Jul 21 '18 at 07:19
  • Are you getting [this](https://github.com/awalker89/openxlsx/commit/c02cae8816980798a9afc353cf6d3f7a64410a15#diff-50c8312a494fdd522c1e739b72674289L111) error in line 111? If so, you might try deleting the `openxlsx` package entirely, and re-installing – Mako212 Jul 22 '18 at 06:05
  • The example above is about export Excel friendly HTML. The actual writing of the excl-file is giving the error. This line (new exmple):"saveWorkbook(wb, "writeEx.xlsx", overwrite = TRUE)" will give this error: "Error: zipping up workbook failed. Please make sure Rtools is installed or a zip application is available to R. Try installr::install.rtools() on Windows. If the "Rtools\bin" directory does not appear in Sys.getenv("PATH") please add it to the system PATH or set this within the R session with Sys.setenv("R_ZIPCMD" = "path/to/zip.exe")". This after manual deleting the openxlsx-folder . – Peter P. Jul 22 '18 at 06:43
  • Good news and bad news. Good news. I didn't notice my version wasn't the latest 4.1.0, but 4.0.17. Bad news. Install of the latest version is not successfull. R keeps installing the binary version and not the source version. Forcing a source install is returning an error. "Warning: running command 'make....had status 127". Perhaps, openxlslx 4.1.0 does not need RTools to function, but RTools is needed for its installation? – Peter P. Jul 22 '18 at 08:33
  • I appreciate your help Mako and I don't want to be unthankful, but I will start a new question about the install of 4.1.0. This topic should be about exporting excel-friendly HTML files. – Peter P. Jul 22 '18 at 08:47

1 Answers1

0

Sorry, I don't want to show off or something. Information about this is hard to find on the internet, for somebody with my knownledge (I think).

But after googling some more I have to accept that "two different colors for all the rest of the rows" is not possible in a conversion from HTML/CSS to Excel.

If this is true, then Excel can only deal with CSS2 selectors.

And with CSS2 selectors you can not select every other row.

Peter P.
  • 51
  • 7