via:
http://texblog.org/2012/05/30/generate-latex-tables-from-csv-files-excel/
Generate LaTeX tables from CSV files (Excel):
Besides various online services and scripts, there are several LaTeX packages that generate tables directly from CSV (comma separated value) files. The advantage is everything is in the tex-file in one place and one doesn’t have to switch back and forth between website/script and LaTeX when changes are made. The drawback clearly is their limited flexibility or high complexity for sophisticated tables.
Note, I used the following few lines of code to generate a simple CSV file using LaTeX. All examples assume the existence of the file “scientists.csv”. Just copy the code into a tex-file and typeset.
\documentclass{minimal}
\begin{filecontents*}{scientists.csv}
name,surname,age
Albert,Einstein,133
Marie,Curie,145
Thomas,Edison,165
\end{filecontents*}
The star suppresses additional information on the file creation from being added.
Let’s start with a simple package.
Package csvsimple
Here is a very basic example. Elements of the first row are considered column titles. To separate them from the table content, a horizontal line is automatically added in between.
\documentclass{article}
\usepackage{csvsimple}
\begin{document}
\csvautotabular{scientists.csv}
\end{document}
The command
csvreader
provides a better control of the column style, titles and content, but makes things slightly more complicated.
Here is an example:
\documentclass{article}
\usepackage{csvsimple}
\begin{document}
\csvreader[tabular=|l|l|c|,
table head=\hline & Name & Age\\\hline,
late after line=\\\hline]%
{scientists.csv}{name=\name,surname=\surename,age=\age}%
{\thecsvrow & \firstname~\name & \age}%
\end{document}
The first part of the optional argument controls the alignment of the content per column (
tabular
). The second sets the column titles and adds a horizontal line before and after them (
table head
). Finally, another horizontal line is added to the end of the table (
late after line
). Furthermore, “commands” are defined for every column using the column title from the CSV file (name, surname and age). These commands allow reordering and combining column content. The command
thecsvrow
is a row counter and therefore an easy way to enumerate the rows.
Here is the
complete package documentation.
Package pgfplotstable
A more flexible package is pgfplotstable (
package documentation). It allows generating tables from different data files types. However, we will only consider CSV files here.
Again, an optional argument serves to customize what the table looks like, using key-value-pairs. Here is a minimal example with the file generated earlier. A simple example without anything special is quite complicated:
\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstabletypeset[
col sep=comma,
string type,
columns/name/.style={column name=Name, column type={|l}},
columns/surname/.style={column name=Surname, column type={|l}},
columns/age/.style={column name=Age, column type={|c|}},
every head row/.style={before row=\hline,after row=\hline},
every last row/.style={after row=\hline},
]{scientists.csv}
\end{document}
One of the things that makes this package interesting is, it supports
multicolumn
.
\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstabletypeset[
col sep=comma,
string type,
every head row/.style={%
before row={\hline
\multicolumn{2}{c}{Full Name} & \\
},
after row=\hline
},
every last row/.style={after row=\hline},
columns/name/.style={column name=Name, column type=l},
columns/surname/.style={column name=Surname, column type=l},
columns/age/.style={column name=Age, column type=c},
]{scientists.csv}
\end{document}
Furthermore, the package allows generation of
multi-page tables with longtable (
package documentation) with
pgfplotstable
(see the
package documentation for more details).
The
csvsimple as well as the
pgfplotstable package documentations are both comprehensive and very nicely formatted. It seems, the authors put in quite a bit of effort writing them. Check it out!
Further packages and other approaches
Other packages that I will not discuss here include: