使用 simplecsv 重新创建 Excel 表格的格式

使用 simplecsv 重新创建 Excel 表格的格式

我正在尝试用 LaTeX 重新创建批发价格表,以便可以从 CSV 文件自动更新数据。

我能够使用导入数据,\csvautolongtable但我对格式感到困惑。这将向客户展示并供内部使用,因此我希望它看起来至少与数据最初来自的 Excel 表格一样好(如下):

在此处输入图片描述

我的问题如下:

  1. 有没有办法使用 csvsimple 重新创建具有类似(理想情况下更好)格式的表格?特别是长线的换行、粗体价格和居中对齐的标题。

    换行尤其重要,因为否则我需要将字体设置为很小的尺寸,否则表格将无法在页面上显示。我使用 longtable 是因为其他一些数据会填满一页以上。

  2. 价格以英镑为单位,如何让 csvsimple 读取 CSV 文件中的 £ 符号?目前它忽略了这些符号。

这是我正在使用的 CSV(技术上用分号分隔)文件:

\begin{filecontents*}{olive_oil_data.csv}
Code;Product Name;Unit Size;Pack Size;Unit Price
1463;AGULLO OLIVE OIL (Valencia , good value and fruity using Picual, Arbequina, Cornicabra and Chancló olives) ;5 litre;3;£17.00
1489;CAL MAS EXTRA VIRGEN OIL (Valencia, artisan single estate using Blanqueta and Alfafara olives );750ml;12;£8.52
3001;GOMEOLIVA OLIVE OIL (Andalucia, smooth and fruity using Hojiblanca and Picual olives);5 litre;3;£20.25
3002;GOMEOLIVA OLIVE OIL (Andalucia, smooth and fruity using Hojiblanca and Picual olives);2 litre;6;£8.33
3003;GOMEOLIVA OLIVE OIL  (Andalucia, smooth and fruity using Hojiblanca and Picual olives );1 litre;15;£4.13
3008;MAIMONA OLIVE OIL GLASS (Extramadura,  intense green colour using Morisca olives);750ml;12;£3.86
3004;MAIMONA OLIVE OIL TIN (Extramadura, intense green colour using Morisca olives);5 litre;4;£23.91
3016;MAS D'EN GIL OLIVE OIL (Priorat, artisan delicate taste using Arberquina olives );500ml;6;£8.57
3011;MOLINO OLIVE OILGLASS (Andalucia, unfiltered fruity using Hojiblanca and Picual olives );750ml;6;£4.85
3009;MOLINO OLIVE OIL TIN (Andalucia unfiltered fruity, Hojiblanca  Picual olives);5 litre;3;£22.10
3019;ORGANIC OLIVE OIL (Andalucia, organic using Hojiblanca and Picual olives );5 litre;3;£26.93
3020;ORGANIC OLIVE OIL (Andalucia , organic using Hojiblanca and Picual olives);2 litre;6;£11.03
1331;CAPIRETE PICUAL OLIVE OIL (Jaen, single estate high expression oil);500ml;12;£5.77
3021;ORGANIC OLIVE OIL GLASS  (Andalucia, Andalucia, organic using Hojiblanca and Picual olives );1 litre;12;£6.21
\end{filecontents*}

我知道提供 MWE 是一种很好的做法,到目前为止我所得到的只有这些:

\documentclass[a4paper,11pt]{article}
\usepackage[top=1cm, bottom=1cm, left=1cm, right=1cm]{geometry}
\usepackage{longtable}
\usepackage{csvsimple}
\begin{document}
\section*{Olive Oil}
    \csvautolongtable[
    before reading=\footnotesize,
    late after line=\\\hline, 
    late after last line=, 
    after reading=\normalsize,
    separator=semicolon,
    respect and=true]
    {./olive_oil_data.csv}
  \end{longtable}
\end{document}

感谢您的帮助。

答案1

对于磅符号,必须选择某种文件编码。如果您在 Windows 世界中,您可能有 cp1252 编码的文件 (?)。

在我的回答中,我假设此编码并使用latin1inputenc如果使用UTF-8,则必须更改此设置。

以下代码尝试重建您的屏幕截图(但目前为黑白)。

在此处输入图片描述

\begin{filecontents*}{olive_oil_data.csv}
Code;Product Name;Unit Size;Pack Size;Unit Price
1463;AGULLO OLIVE OIL (Valencia , good value and fruity using Picual, Arbequina, Cornicabra and Chancló olives) ;5 litre;3;£17.00
1489;CAL MAS EXTRA VIRGEN OIL (Valencia, artisan single estate using Blanqueta and Alfafara olives );750ml;12;£8.52
3001;GOMEOLIVA OLIVE OIL (Andalucia, smooth and fruity using Hojiblanca and Picual olives);5 litre;3;£20.25
3002;GOMEOLIVA OLIVE OIL (Andalucia, smooth and fruity using Hojiblanca and Picual olives);2 litre;6;£8.33
3003;GOMEOLIVA OLIVE OIL  (Andalucia, smooth and fruity using Hojiblanca and Picual olives );1 litre;15;£4.13
3008;MAIMONA OLIVE OIL GLASS (Extramadura,  intense green colour using Morisca olives);750ml;12;£3.86
3004;MAIMONA OLIVE OIL TIN (Extramadura, intense green colour using Morisca olives);5 litre;4;£23.91
3016;MAS D'EN GIL OLIVE OIL (Priorat, artisan delicate taste using Arberquina olives );500ml;6;£8.57
3011;MOLINO OLIVE OILGLASS (Andalucia, unfiltered fruity using Hojiblanca and Picual olives );750ml;6;£4.85
3009;MOLINO OLIVE OIL TIN (Andalucia unfiltered fruity, Hojiblanca  Picual olives);5 litre;3;£22.10
3019;ORGANIC OLIVE OIL (Andalucia, organic using Hojiblanca and Picual olives );5 litre;3;£26.93
3020;ORGANIC OLIVE OIL (Andalucia , organic using Hojiblanca and Picual olives);2 litre;6;£11.03
1331;CAPIRETE PICUAL OLIVE OIL (Jaen, single estate high expression oil);500ml;12;£5.77
3021;ORGANIC OLIVE OIL GLASS  (Andalucia, Andalucia, organic using Hojiblanca and Picual olives );1 litre;12;£6.21
\end{filecontents*}

\documentclass[a4paper,11pt]{article}

% Encoding (e.g. Windows Cp1252)
% Has to be changed for UFT-8, if used
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}

\usepackage[top=1cm, bottom=1cm, left=1cm, right=1cm]{geometry}
\usepackage{longtable,array}
\usepackage{csvsimple}

\usepackage{lmodern}% could be left out or chanaged to another font

\newcommand*{\headentry}[2]{\multicolumn{1}{#1}{\centering\arraybackslash\bfseries #2}}

\begin{document}
\section*{Olive Oil}

\csvreader[
  separator=semicolon,
  before table=\sffamily\small,
  longtable={|r|p{10cm}|r|r|r|},
  table head={\hline%
    \headentry{|m{1.8cm}|}{PRODUCT\linebreak CODE}
    & \headentry{m{10cm}|}{PRODUCT NAME}
    & \headentry{m{1cm}|}{UNIT\linebreak SIZE}
    & \headentry{m{1cm}|}{PACK\linebreak SIZE}
    & \headentry{m{2.2cm}|}{WHOLESALE\linebreak UNIT PRICE}\\\hline},
  late after line=\\\hline,
]
{./olive_oil_data.csv}
{Code=\code,Product Name=\name,Unit Size=\unitsize,Pack Size=\packsize,Unit Price=\unitprice}
{\code & \name & \unitsize & \packsize & \unitprice}

\end{document}

使用该booktabs包可能会得到更令人满意的结果。请参阅以下示例代码:

在此处输入图片描述

\begin{filecontents*}{olive_oil_data.csv}
Code;Product Name;Unit Size;Pack Size;Unit Price
1463;AGULLO OLIVE OIL (Valencia , good value and fruity using Picual, Arbequina, Cornicabra and Chancló olives) ;5 litre;3;£17.00
1489;CAL MAS EXTRA VIRGEN OIL (Valencia, artisan single estate using Blanqueta and Alfafara olives );750ml;12;£8.52
3001;GOMEOLIVA OLIVE OIL (Andalucia, smooth and fruity using Hojiblanca and Picual olives);5 litre;3;£20.25
3002;GOMEOLIVA OLIVE OIL (Andalucia, smooth and fruity using Hojiblanca and Picual olives);2 litre;6;£8.33
3003;GOMEOLIVA OLIVE OIL  (Andalucia, smooth and fruity using Hojiblanca and Picual olives );1 litre;15;£4.13
3008;MAIMONA OLIVE OIL GLASS (Extramadura,  intense green colour using Morisca olives);750ml;12;£3.86
3004;MAIMONA OLIVE OIL TIN (Extramadura, intense green colour using Morisca olives);5 litre;4;£23.91
3016;MAS D'EN GIL OLIVE OIL (Priorat, artisan delicate taste using Arberquina olives );500ml;6;£8.57
3011;MOLINO OLIVE OILGLASS (Andalucia, unfiltered fruity using Hojiblanca and Picual olives );750ml;6;£4.85
3009;MOLINO OLIVE OIL TIN (Andalucia unfiltered fruity, Hojiblanca  Picual olives);5 litre;3;£22.10
3019;ORGANIC OLIVE OIL (Andalucia, organic using Hojiblanca and Picual olives );5 litre;3;£26.93
3020;ORGANIC OLIVE OIL (Andalucia , organic using Hojiblanca and Picual olives);2 litre;6;£11.03
1331;CAPIRETE PICUAL OLIVE OIL (Jaen, single estate high expression oil);500ml;12;£5.77
3021;ORGANIC OLIVE OIL GLASS  (Andalucia, Andalucia, organic using Hojiblanca and Picual olives );1 litre;12;£6.21
\end{filecontents*}

\documentclass[a4paper,11pt]{article}

% Encoding (e.g. Windows Cp1252)
% Has to be changed for UFT-8, if used
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}

\usepackage[top=1cm, bottom=1cm, left=1cm, right=1cm]{geometry}
\usepackage{longtable,array,booktabs,xcolor}
\usepackage{csvsimple}

\usepackage{lmodern}% could be left out or chanaged to another font

\newcommand*{\headentry}[2]{\multicolumn{1}{m{#1}}{\centering\arraybackslash\textcolor{red!85!black}{\bfseries #2}}}

\begin{document}
\section*{Olive Oil}

\csvreader[
  separator=semicolon,
  before table=\sffamily\small,
  longtable={cp{10cm}rrr},
  table head={\toprule%
    \headentry{1.8cm}{PRODUCT\linebreak CODE}
    & \headentry{10cm}{PRODUCT NAME}
    & \headentry{1cm}{UNIT\linebreak SIZE}
    & \headentry{1cm}{PACK\linebreak SIZE}
    & \headentry{2.2cm}{WHOLESALE\linebreak UNIT PRICE}\\\midrule},
  late after line=\\\midrule,
  late after last line=\\\bottomrule,
]
{./olive_oil_data.csv}
{Code=\code,Product Name=\name,Unit Size=\unitsize,Pack Size=\packsize,Unit Price=\unitprice}
{\code & \name & \unitsize & \packsize & \bfseries\unitprice}

\end{document}

相关内容