Skip to main content

How to create table in Latex from MS Excel files or other external databases

Latex can be used to create tables from external files such as MS-Excel files saved as comma separated values or similarly many other formats. Recently, I received a comment where a reader suggested that I should post a tutorial on how to create tables in Latex from external files. I searched around and found multiple ways to achieve this. I am going to talk about two of the most popular ways of doing it. First one is using pgfplotstable and the second one is using datatool package.

Specifiacally, we are going to learn the following in this tutorial:
  1. Using datatool package to load external files
  2. Usng MS-Excel files to create tables in Latex
  3. Using \DTLloaddb to set keys and use external files
  4. Using \DTLforeach to iterate the external file
  5. Using \pgfplotstable package
  6. Using \booktabs package
  7. Using pgfplotstabletypeset
  8. Styling each column of table
  9. Using style rules from booktabs package
The following video tutorial explains both methods of creating tables in Latex using external database files.



The code for both of the methods is given below for your reference:
The first method - suing datatool package
\documentclass{article}
\usepackage{datatool}
\DTLloaddb[keys={c1,c2,c3}]{ctext}{csvtext.csv}
\begin{document}
\begin{table}
\begin{tabular}{clr}
\textbf{Software} & \textbf{Manufacturer} & \textbf{Malware}
\DTLforeach{ctext}{
\cola=c1,\colb=c2,\colc=c3}{\\
\cola & \colb & \colc}
\end{tabular}
\end{table}
\end{document}
The second method - using pgfplotstable package
\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\begin{document}
\pgfplotstabletypeset[
col sep=comma,
columns/Software/.style={string type},
columns/num1/.style={string type},
columns/num2/.style={string type},
every head row/.style={
before row=\toprule,
after row =\midrule
},
every last row/.style={after row=\bottomrule}
]{csvtext.csv}
\end{document}

Comments

  1. I assume you can also format the table cells using pgfplotstable (e.x. ccc) and put them in table environment. Is this right?

    ReplyDelete
  2. Hello! Ur videos r quite interesting. I am facing the following errors. Kindly help

    ! Package pgfplots Error: Could not read table file 'csvtext.csv'. In case you
    intended to provide inline data: maybe TeX screwed up your end-of-lines? Try `r
    ow sep=crcr' and terminate your lines with `\\' (refer to the pgfplotstable man
    ual for details).

    See the pgfplots package documentation for explanation.
    Type H for immediate help.
    ...

    l.19 ]{csvtext.csv}

    ?

    ReplyDelete

Post a Comment

Comments on QuickLatex are strictly moderated. As much as I hate moderation, I must fight spam. Please rest assured that if your comment is relevant I will approve it and answer.

Popular posts from this blog

Latex Tutorial: How to Create Images, Diagrams, and Flow Charts Using Latex

This tutorial is an extension of previous video tutorial on how to use tikz for image creation in Latex . In this video I explain how to set up your document for flow chat design. This equally applies to any other image that uses similar structure. Specifically, in this video I talk about following things: How to create a flow chart How to setup styles for reusable components of an image How to draw arrows from one component to other How to change the styles of components How to change the arrowhead for Latex arrows How to align components left, right, top, and below of each other The code use:d in the video is here: \documentclass{article} \usepackage{tikz} \usetikzlibrary{shapes,shadows,arrows} \begin{document} \tikzstyle{decision} = [diamond, draw, fill=blue!50] \tikzstyle{line} = [draw, -stealth, thick] \tikzstyle{elli}=[draw, ellipse, fill=red!50,minimum height=8mm, text width=5em, text centered] \tikzstyle{block} = [draw, rectangle, fill=blue!50, text width...

LaTeX Video Tutorial: How to Create a Resume or CV (Curriculum Vitae) using LaTeX

One of the most frequent questions my colleagues ask me is how to create a Resume or Curriculum Vitae (CV), if you will, in LaTeX . I have a style file that was passed to me by a friend who found it on Internet. Since this looked good I used it and, thanks to the original contributor, I am going to share it with you all today. You will be able to download these files and create a professional Resume for yourself.  The files can be download from the linked locations: The Resume.tex file (main document) The Class file res.cls (for page setting) In the video tutorial , attached below, I have explained how to change the text for your needs. The structure of the Resume (or CV) is highly flexible and once you get hold of the basics it will be pretty straight forward for you to tweak it for your own use.  I have tried to add the basic sections that are present in most of the Resumes but you will most probably add more depending on your requirements. I am prett...

How to draw Reddit Alien in LaTeX using Tikz - Video tutorial and code

If you are not a Redditor you are a lucky person. It's a one way traffic scenario with no dead end. Once you subscribe to Reddit there is no way back and it is no good being there. I am expecting my colleagues to have an intervention for my Reddit addiction soon but till then I am all here to invest (read:waste) my precious time. Since we got that out of our way, let us get back to business. Today, we are going to see how to draw a cartoon figure using LaTeX. The motivation behind this post is /r/latex on Reddit as I wanted that subreddit to have its logo drawn in LaTeX. I did not know it already was in LaTeX but it seems my work is appreciated there so I am going to contribute my 2 cents to the community. Specifically, we will learn following things in this tutorial: How to work with Colors - defining RGB colors in LaTeX. How to use multiple layers to set order of document objects (send to back, bring forward like functionality). How to use Arcs in an effective way....