[Tex/LaTex] Using long table to split a table across multiple pages

excel2latexlongtabletablestexmaker

I am using the excel to latex add in to generate tables from excel, however, when the table is too long it is not split automatically between 2 (or more pages). Is there any easy way to do this? I have found the longtable package, however, I am not managing to use it with the following code produced by LateX

% Table generated by Excel2LaTeX from sheet 'Sheet1'
\begin{table}[htbp]
  \centering
  \caption{Trial}
    \begin{tabular}{|r|r|r|r|r}
\cline{1-4}    Hello & How   & Are   & You   &  \bigstrut\\
\cline{1-4}    \multicolumn{1}{|c|}{\multirow{2}[4]{*}{1}} & 2     & \multicolumn{1}{c|}{\multirow{2}[4]{*}{4}} & \multicolumn{1}{c|}{\multirow{2}[4]{*}{5}} &  \bigstrut\\
\cline{2-2}    \multicolumn{1}{|c|}{} & 3     & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} &  \bigstrut\\
\cline{1-4}    6     & 7     & 8     & 9     &  \bigstrut\\
\cline{1-4}    \multicolumn{1}{r}{} & \multicolumn{1}{r}{} & \multicolumn{1}{r}{} & \multicolumn{1}{r}{} &  \bigstrut[t]\\
    \multicolumn{1}{r}{} & \multicolumn{1}{r}{} & \multicolumn{1}{r}{} & \multicolumn{1}{r}{} &  \\
    \end{tabular}%
  \label{tab:addlabel}%
\end{table}%

I am using a simple example to not get confused with the code here (this example does not span over 2 pages) but if it did, what changes are necessary?

enter image description here

Best Answer

As you've noted, in order to make LaTeX split a long table across pages, it's necessary to use a longtable environment instead of nested table and tabular environments.

The following example first shows the table produced by your code (with the code simplified here and there), followed by the same table produced using a longtable environment. The four key differences are:

  • The longtable environment takes as its argument the material that would be specified as the argument of the tabular environment. Do not try to nest a longtable inside a table.

  • Insert \\ (line break directive) after \caption.

  • Since the table's headers and footers may occur repeatedly, specify the repeated material explicitly. Header and footer material is delimited with instructions named \endfirsthead, \endhead, \endfoot, and \endlastfoot.

  • No need to provide a \centering instruction; the longtable is centered by default.

enter image description here

\documentclass{article}
\usepackage{multirow,longtable,bigstrut,caption}
\begin{document}

\begin{table}[h!]
\centering
\caption{A Trial} \label{tab:addlabel}
\begin{tabular}{|r|r|r|r|}
\hline 
Hello & How & Are & You \bigstrut\\
\hline 
\multicolumn{1}{|c|}{\multirow{2}[4]{*}{1}} & 2 
& \multicolumn{1}{ c|}{\multirow{2}[4]{*}{4}} 
& \multicolumn{1}{ c|}{\multirow{2}[4]{*}{5}} \bigstrut\\
\cline{2-2} 
\multicolumn{1}{|c|}{} & 3 & & \bigstrut\\
\hline 
6 & 7 & 8 & 9 \bigstrut\\
\hline 
\end{tabular}
\end{table}

\begin{longtable}{|r|r|r|r|}
\caption{Another Trial} \label{tab:addanotherlabel}\\
   \hline 
   Hello & How & Are & You \bigstrut\\
   \hline
   \endhead  % delimiter for header
   \hline
   \endfoot  % delimiter for footer
\multicolumn{1}{|c|}{\multirow{2}[4]{*}{1}} & 2 
& \multicolumn{1}{ c|}{\multirow{2}[4]{*}{4}} 
& \multicolumn{1}{ c|}{\multirow{2}[4]{*}{5}} \bigstrut\\
\cline{2-2} 
\multicolumn{1}{|c|}{} & 3 & & \bigstrut\\
\hline 
6 & 7 & 8 & 9 \bigstrut \\
\end{longtable}

\end{document}