带有 Excel 电子表格的邮件合并包示例

带有 Excel 电子表格的邮件合并包示例

我正在寻找使用的示例mailmerge编写求职信的软件包,在电子表格上填写姓名和地址。该软件包附带一个示例,但您必须手动输入所有数据。

答案1

我用datatool包。我不使用letter文档类,因为我使用自己的信头包加上article类。但如何做到这一点的基本知识应该很清楚,并且适用于letter类。检查地址行中的空字段很重要,否则您将得到空白行。

我使用parskip带有字母的包,它将设置\parindent为 0 并使用空行分隔段落。如果您不喜欢这样,那么您可能希望在环境中设置地址行{tabular}{@{}l}

\documentclass{article}
\usepackage{datatool}
\usepackage{parskip}

\makeatletter
% Blank/missing fields commands
% \skipblank adds \\ to filled field; * version adds \space instead of newline
\newcommand\skipblank{\@ifstar\@spskip\@nlskip}
\newcommand\@nlskip[1]{\ifthenelse{\DTLiseq{#1}{}}{\relax}{#1\\}}
\newcommand\@spskip[1]{\ifthenelse{\DTLiseq{#1}{}}{\relax}{#1\space}}
% \checkblank replaces blank fields with ***
\newcommand\checkblank[1]{\ifthenelse{\DTLiseq{#1}{}}{***}{#1}}
\makeatother
\begin{document}
\DTLloaddb{addresses}{mail-merge-addresses.csv} % use your actual address database here
\DTLforeach*{addresses}{%
    \addtitle=Title,
    \firstname=FirstName,
    \lastname=LastName,
    \position=Position,
    \addressi=Address1,
    \addressii=Address2,
    \addressiii=Address3,
    \city=City,
    \state=State,
    \zip=Zip,
    \country=Country}
    {%
\setcounter{page}{1}
\thispagestyle{empty}
\checkblank{\addtitle}\ \firstname\ \lastname,\\
\skipblank{\position}
\skipblank{\addressi}
\skipblank{\addressii}
\skipblank{\addressiii}
\city, \state\ \zip\\
\skipblank*{\country}

Dear \checkblank{\addtitle}\ \lastname:

This is the text of the letter.

Sincerely,


\clearpage
}
\end{document}

mail-merge-addresses.csv文件如下:(它故意有空白字段来说明字段检查。)

Title,  LastName,   FirstName,  Position,   Address1,   Address2,   Address3,   City,   State,  Zip,    Country
Dr.,    Smith,  James,  Chair,  Dept. of Physics,   University of Somewhere,    434 East Hall,  Cambridge,  MA, 02139,  
Mr.,    Jones,  Bill,   Director of Human Resources,    ,   University of Nowhere,  A203 King St.,  Stanford,   CA, 94305,  
,   Brown,  Sally,  ,   Firstline,  ,   3rdline,    East Lansing,   MI, ,   USA

相关内容