Экспорт Excel дружественных таблиц через HTML/CSS
Я хотел бы экспортировать таблицы для Excel с форматированием из RStudio. Пакеты как openxlsx работают отлично, но я не могу использовать их в своей рабочей ситуации. Невозможно установить приложения Java или Rtools.
Единственный способ экспортировать таблицы с форматированием для Excel - это скопировать вывод HTML (из Markdown) или открыть файл HTML (из Markdown).
Есть ли умные отправные точки, которые я могу использовать? Примеры? Примеры CSS (для Markdown), которые будут работать для Excel?
Единственное, что сейчас работает, - это скопировать HTML из RStudio: Prview HTML. Другие способы не дают хороших результатов для (моего) форматирования.
То, что я хотел бы сделать, это цвет заголовка (первая строка) и два разных цвета для всех остальных строк.
Одна из проблем заключается в том, что Excel может работать только с одним цветом фона.
Обновить:
Поэтому я должен предположить, что openxlsx не будет работать для меня. Файл architect.css из пакета prettydoc показывает тот же результат. Смотрите код уценки. Выходная таблица в превью RStudio хорошая. Также в браузерах. Копирование в Excel работает только из предварительного просмотра RStudio.
Когда вы открываете HTML-файл в Excel, Excel показывает только один цвет фона. Белый в этом случае.
То, что я ищу (я думаю), является более простым CSS, с упором на стиль таблицы. (с двумя цветами фона как: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)
```
1 ответ
Извините, я не хочу хвастаться или что-то. Информацию об этом трудно найти в Интернете, для кого-то из моих знакомых (я думаю).
Но после того, как погуглил еще немного, я должен признать, что "два разных цвета для всех остальных строк" невозможны при конвертации из HTML/CSS в Excel.
Если это так, то Excel может работать только с селекторами CSS2.
А с помощью селекторов CSS2 вы не можете выбирать все остальные строки.