<!DOCTYPE html>
<html lang="zh-Hans">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=5">
    <title>java 如何在pdf中生成表格详解编程语言 | 云图网</title>
    <meta name="keywords" content="c">
<meta name="description" content="1、目标 　　在pdf中生成一个可变表头的表格，并向其中填充数据。通过泛型动态的生成表头，通过反射动态获取实体类（我这里是User）的get方法动态获得数据，从而达到动态生成表格。 　　每天生成一个文件夹存储生成的pdf文件（文件夹的命名是年月日时间戳），如：20151110 　　生成的文件可能在毫秒级别，故文件的命名…">
<meta property="og:type" content="article">
<meta property="og:url" content="https://blog.ytso.com/tech/pnotes/15131.html">
<meta property="og:site_name" content="云图网">
<meta property="og:title" content="java 如何在pdf中生成表格详解编程语言">
<meta property="og:image" content="http://blog.ytso.com/zb_users/plugin/LazyLoad/usr/loading.gif">
<meta property="og:description" content="1、目标 　　在pdf中生成一个可变表头的表格，并向其中填充数据。通过泛型动态的生成表头，通过反射动态获取实体类（我这里是User）的get方法动态获得数据，从而达到动态生成表格。 　　每天生成一个文件夹存储生成的pdf文件（文件夹的命名是年月日时间戳），如：20151110 　　生成的文件可能在毫秒级别，故文件的命名…">
<link rel="canonical" href="https://blog.ytso.com/tech/pnotes/15131.html">
<meta name="applicable-device" content="pc,mobile">
<meta http-equiv="Cache-Control" content="no-transform">
<link rel="shortcut icon" href="https://imgcdn.ytso.com/wp-content/uploads/2024/10/20241015181503433.jpg">
<link rel='dns-prefetch' href='//cdnjs.cloudflare.com' />
<style id='wp-img-auto-sizes-contain-inline-css' type='text/css'>
img:is([sizes=auto i],[sizes^="auto," i]){contain-intrinsic-size:3000px 1500px}
/*# sourceURL=wp-img-auto-sizes-contain-inline-css */
</style>
<link rel='stylesheet' id='stylesheet-css' href='https://blog.ytso.com/wp-content/themes/justnews/css/style.css?ver=6.21.5' type='text/css' media='all' />
<link rel='stylesheet' id='material-icons-css' href='https://blog.ytso.com/wp-content/themes/justnews/themer/assets/css/material-icons.css?ver=6.21.5' type='text/css' media='all' />
<link rel='stylesheet' id='remixicon-css' href='https://cdnjs.cloudflare.com/ajax/libs/remixicon/4.2.0/remixicon.min.css?ver=6.9.4' type='text/css' media='all' />
<link rel='stylesheet' id='font-awesome-css' href='https://blog.ytso.com/wp-content/themes/justnews/themer/assets/css/font-awesome.css?ver=6.21.5' type='text/css' media='all' />
<style id='wp-block-library-inline-css' type='text/css'>
:root{--wp-block-synced-color:#7a00df;--wp-block-synced-color--rgb:122,0,223;--wp-bound-block-color:var(--wp-block-synced-color);--wp-editor-canvas-background:#ddd;--wp-admin-theme-color:#007cba;--wp-admin-theme-color--rgb:0,124,186;--wp-admin-theme-color-darker-10:#006ba1;--wp-admin-theme-color-darker-10--rgb:0,107,160.5;--wp-admin-theme-color-darker-20:#005a87;--wp-admin-theme-color-darker-20--rgb:0,90,135;--wp-admin-border-width-focus:2px}@media (min-resolution:192dpi){:root{--wp-admin-border-width-focus:1.5px}}.wp-element-button{cursor:pointer}:root .has-very-light-gray-background-color{background-color:#eee}:root .has-very-dark-gray-background-color{background-color:#313131}:root .has-very-light-gray-color{color:#eee}:root .has-very-dark-gray-color{color:#313131}:root .has-vivid-green-cyan-to-vivid-cyan-blue-gradient-background{background:linear-gradient(135deg,#00d084,#0693e3)}:root .has-purple-crush-gradient-background{background:linear-gradient(135deg,#34e2e4,#4721fb 50%,#ab1dfe)}:root .has-hazy-dawn-gradient-background{background:linear-gradient(135deg,#faaca8,#dad0ec)}:root .has-subdued-olive-gradient-background{background:linear-gradient(135deg,#fafae1,#67a671)}:root .has-atomic-cream-gradient-background{background:linear-gradient(135deg,#fdd79a,#004a59)}:root .has-nightshade-gradient-background{background:linear-gradient(135deg,#330968,#31cdcf)}:root .has-midnight-gradient-background{background:linear-gradient(135deg,#020381,#2874fc)}:root{--wp--preset--font-size--normal:16px;--wp--preset--font-size--huge:42px}.has-regular-font-size{font-size:1em}.has-larger-font-size{font-size:2.625em}.has-normal-font-size{font-size:var(--wp--preset--font-size--normal)}.has-huge-font-size{font-size:var(--wp--preset--font-size--huge)}.has-text-align-center{text-align:center}.has-text-align-left{text-align:left}.has-text-align-right{text-align:right}.has-fit-text{white-space:nowrap!important}#end-resizable-editor-section{display:none}.aligncenter{clear:both}.items-justified-left{justify-content:flex-start}.items-justified-center{justify-content:center}.items-justified-right{justify-content:flex-end}.items-justified-space-between{justify-content:space-between}.screen-reader-text{border:0;clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal!important}.screen-reader-text:focus{background-color:#ddd;clip-path:none;color:#444;display:block;font-size:1em;height:auto;left:5px;line-height:normal;padding:15px 23px 14px;text-decoration:none;top:5px;width:auto;z-index:100000}html :where(.has-border-color){border-style:solid}html :where([style*=border-top-color]){border-top-style:solid}html :where([style*=border-right-color]){border-right-style:solid}html :where([style*=border-bottom-color]){border-bottom-style:solid}html :where([style*=border-left-color]){border-left-style:solid}html :where([style*=border-width]){border-style:solid}html :where([style*=border-top-width]){border-top-style:solid}html :where([style*=border-right-width]){border-right-style:solid}html :where([style*=border-bottom-width]){border-bottom-style:solid}html :where([style*=border-left-width]){border-left-style:solid}html :where(img[class*=wp-image-]){height:auto;max-width:100%}:where(figure){margin:0 0 1em}html :where(.is-position-sticky){--wp-admin--admin-bar--position-offset:var(--wp-admin--admin-bar--height,0px)}@media screen and (max-width:600px){html :where(.is-position-sticky){--wp-admin--admin-bar--position-offset:0px}}
/*wp_block_styles_on_demand_placeholder:69c6d467c47a4*/
/*# sourceURL=wp-block-library-inline-css */
</style>
<style id='classic-theme-styles-inline-css' type='text/css'>
/*! This file is auto-generated */
.wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none}
/*# sourceURL=/wp-includes/css/classic-themes.min.css */
</style>
<link rel='stylesheet' id='wpcom-member-css' href='https://blog.ytso.com/wp-content/plugins/wpcom-member/css/style.css?ver=1.7.19' type='text/css' media='all' />
<script type="text/javascript" src="https://blog.ytso.com/wp-includes/js/jquery/jquery.min.js?ver=3.7.1" id="jquery-core-js"></script>
<script type="text/javascript" src="https://blog.ytso.com/wp-includes/js/jquery/jquery-migrate.min.js?ver=3.4.1" id="jquery-migrate-js"></script>
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="https://blog.ytso.com/xmlrpc.php?rsd" />
<meta name='description' content='1、目标 　　在pdf中生成一个可变表头的表格，并向其中填充数据。通过泛型动态的生成表头，通过反射动态获取实体类（我这里是User）的get方法动态获得数据，从而达到动态生成表格。 　　每天生成一个文件夹存储生成…' />
<meta name='keywords' content='c' />
<style>:root{--theme-color: #08c; --theme-hover: #07c; --logo-height: 32px; --logo-height-mobile: 26px; --menu-item-gap: 28px; --member-login-bg: url('https://blog.ytso.com/loginwall.jpg'); --header-bg-color: #fff; --header-bg-image: none; --theme-border-radius-s: 3px; --theme-border-radius-m: 5px; --theme-border-radius-l: 8px; --theme-border-radius-xl: 12px; --thumb-ratio-default: 480 / 300; --thumb-ratio-post: 480 / 300; --post-video-ratio: 860 / 482;}</style>
<link rel="icon" href="https://imgcdn.ytso.com/wp-content/uploads/2024/10/20241015181503433.jpg" sizes="32x32" />
<link rel="icon" href="https://imgcdn.ytso.com/wp-content/uploads/2024/10/20241015181503433.jpg" sizes="192x192" />
<link rel="apple-touch-icon" href="https://imgcdn.ytso.com/wp-content/uploads/2024/10/20241015181503433.jpg" />
<meta name="msapplication-TileImage" content="https://imgcdn.ytso.com/wp-content/uploads/2024/10/20241015181503433.jpg" />
    <!--[if lte IE 11]><script src="https://blog.ytso.com/wp-content/themes/justnews/js/update.js"></script><![endif]-->
</head>
<body class="wp-singular post-template-default single single-post postid-15131 single-format-standard wp-theme-justnews lang-cn el-boxed header-fixed">
<header class="header header-fluid">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse" aria-label="menu">
                <span class="icon-bar icon-bar-1"></span>
                <span class="icon-bar icon-bar-2"></span>
                <span class="icon-bar icon-bar-3"></span>
            </button>
                        <div class="logo">
                <a href="https://blog.ytso.com/" rel="home">
                    <img src="https://imgcdn.ytso.com/wp-content/uploads/2021/12/20211207181156143.png" alt="云图网">
                </a>
            </div>
        </div>
        <div class="collapse navbar-collapse mobile-style-0">
            <nav class="primary-menu"><ul id="menu-justnews-footer-menu" class="nav navbar-nav wpcom-adv-menu"><li class="menu-item"><a href="https://blog.ytso.com/category/industrynews">业界资讯</a></li>
<li class="menu-item current-post-ancestor active menu-item-style menu-item-style-1 dropdown"><a target="_blank" href="https://blog.ytso.com/category/tech" class="dropdown-toggle">技术专区</a>
<ul class="dropdown-menu menu-item-wrap menu-item-col-5">
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/cloud">云计算</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/ai">人工智能</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/safety">信息安全</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/bigdata">大数据</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/dev">研发管理</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/webdev">大前端</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/opensource">开源</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/aiops">智能运维</a></li>
	<li class="menu-item current-post-ancestor current-post-parent active"><a href="https://blog.ytso.com/category/tech/pnotes">编程笔记</a></li>
	<li class="menu-item"><a href="https://blog.ytso.com/category/tech/wp">WordPress</a></li>
</ul>
</li>
<li class="menu-item"><a href="https://blog.ytso.com/category/enterprise-strategic-planning">企业战略规划</a></li>
<li class="menu-item"><a href="https://blog.ytso.com/category/download">下载专区</a></li>
<li class="menu-item"><a href="https://blog.ytso.com/category/it%e6%b1%9f%e6%b9%96%e5%8f%b2">江湖史</a></li>
<li class="menu-item"><a href="https://blog.ytso.com/category/notes">随笔记录</a></li>
</ul></nav>            <div class="navbar-action">
                <div class="navbar-search-icon j-navbar-search"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-search"></use></svg></i></div><form class="navbar-search" action="https://blog.ytso.com/" method="get" role="search"><div class="navbar-search-inner"><i class="wpcom-icon wi navbar-search-close"><svg aria-hidden="true"><use xlink:href="#wi-close"></use></svg></i><input type="text" name="s" class="navbar-search-input" autocomplete="off" maxlength="100" placeholder="输入关键词搜索..." value=""><button class="navbar-search-btn" type="submit" aria-label="搜索"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-search"></use></svg></i></button></div></form>                    <div id="j-user-wrap">
                        <a class="login" href="https://blog.ytso.com/login">登录</a>
                        <a class="login register" href="https://blog.ytso.com/register">注册</a>
                    </div>
                                            <a class="wpcom-btn btn-primary btn-xs publish" href="https://blog.ytso.com/contribute">
                            <i class="fa fa-edit"></i> 投稿                        </a>
                                </div>
        </div>
    </div><!-- /.container -->
</header>

<div id="wrap">    <div class="wrap container">
        <ol class="breadcrumb" vocab="https://schema.org/" typeof="BreadcrumbList"><li class="home" property="itemListElement" typeof="ListItem"><a href="https://blog.ytso.com" property="item" typeof="WebPage"><span property="name" class="hide">云图网</span>首页</a><meta property="position" content="1"></li><li property="itemListElement" typeof="ListItem"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-arrow-right-3"></use></svg></i><a href="https://blog.ytso.com/category/tech" property="item" typeof="WebPage"><span property="name">技术专区</span></a><meta property="position" content="2"></li><li property="itemListElement" typeof="ListItem"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-arrow-right-3"></use></svg></i><a href="https://blog.ytso.com/category/tech/pnotes" property="item" typeof="WebPage"><span property="name">编程笔记</span></a><meta property="position" content="3"></li></ol>        <main class="main">
                            <article id="post-15131" class="post-15131 post type-post status-publish format-standard hentry category-pnotes tag-c entry">
                    <div class="entry-main">
                                                                        <div class="entry-head">
                            <h1 class="entry-title">java 如何在pdf中生成表格详解编程语言</h1>
                            <div class="entry-info">
                                                                <time class="entry-date published" datetime="2021-07-19T17:32:46+08:00" pubdate>
                                    2021年7月19日 17:32                                </time>
                                <span class="dot">•</span>
                                <a href="https://blog.ytso.com/category/tech/pnotes" rel="category tag">编程笔记</a>                                                            </div>
                        </div>
                        
                                                <div class="entry-content text-indent text-justify">
                            <p style="background-color: #0098dd; color: white; font-size: 17px; font-weight: bold;">1、目标</p>
<p>　　在pdf中生成一个可变表头的表格，并向其中填充数据。通过泛型动态的生成表头，通过反射动态获取实体类（我这里是User）的get方法动态获得数据，从而达到动态生成表格。</p>
<p>　　每天生成一个文件夹存储生成的pdf文件（文件夹的命名是年月日时间戳），如：20151110</p>
<p>　　生成的文件可能在毫秒级别，故文件的命名规则是"到毫秒的时间戳-uuid"，如：20151110100245690-ece540e5-7737-4ab7-b2d6-87bc23917c8c.pdf</p>
<p>　　通过读取properties文件动态获取文件存储的跟目录。获取方式可查看：<a href="http://www.cnblogs.com/0201zcr/p/4700418.html" target="_blank" rel="noopener">http://www.cnblogs.com/0201zcr/p/4700418.html</a></p>
<p style="background-color: #0098dd; color: white; font-size: 17px; font-weight: bold;">2、所需的jar</p>
<p>　　这里通过itex插件进行pdf的生成，需要的jar包括以下几个</p>
<p><noscript><img decoding="async" src="http://blog.ytso.com/zb_users/plugin/LazyLoad/usr/loading.gif" data-original="/img/76c047fb0078471580fec9158a2df963.jpg" alt="java 如何在pdf中生成表格详解编程语言"></noscript><img decoding="async" class="j-lazy" src="https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png" data-original="http://blog.ytso.com/zb_users/plugin/LazyLoad/usr/loading.gif" data-original="/img/76c047fb0078471580fec9158a2df963.jpg" alt="java 如何在pdf中生成表格详解编程语言"></p>
<p style="background-color: #0098dd; color: white; font-size: 17px; font-weight: bold;">3、编码实现</p>
<p>1）、实体类</p>
<div class="cnblogs_code">
<pre class="prism-language-java"><span style="color: #0000ff;">package</span><span style="color: #000000;"> com.zcr.until; 
 
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> User  
{ 
    </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String name; 
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> age ; 
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">float</span><span style="color: #000000;"> height; 
    </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String adress; 
    </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String sex; 
    </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String jj; 
     
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getJj() 
    { 
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> jj; 
    } 
 
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setJj(String jj) 
    { 
        </span><span style="color: #0000ff;">this</span>.jj =<span style="color: #000000;"> jj; 
    } 
 
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> User() 
    { 
     
    } 
 
    </span><span style="color: #0000ff;">public</span> User(String name,<span style="color: #0000ff;">int</span> age,<span style="color: #0000ff;">float</span><span style="color: #000000;"> height,String adress,String sex,String jj) 
    { 
        </span><span style="color: #0000ff;">this</span>.name =<span style="color: #000000;"> name; 
        </span><span style="color: #0000ff;">this</span>.age =<span style="color: #000000;"> age; 
        </span><span style="color: #0000ff;">this</span>.height =<span style="color: #000000;"> height; 
        </span><span style="color: #0000ff;">this</span>.adress =<span style="color: #000000;"> adress; 
        </span><span style="color: #0000ff;">this</span>.sex =<span style="color: #000000;"> sex; 
        </span><span style="color: #0000ff;">this</span>.jj =<span style="color: #000000;"> jj; 
    } 
     
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getAdress() 
    { 
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> adress; 
    } 
 
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setAdress(String adress) 
    { 
        </span><span style="color: #0000ff;">this</span>.adress =<span style="color: #000000;"> adress; 
    } 
 
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getSex() 
    { 
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> sex; 
    } 
 
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setSex(String sex) 
    { 
        </span><span style="color: #0000ff;">this</span>.sex =<span style="color: #000000;"> sex; 
    } 
 
     
     
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getName() { 
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> name; 
    } 
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setName(String name) { 
        </span><span style="color: #0000ff;">this</span>.name =<span style="color: #000000;"> name; 
    } 
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> getAge() { 
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> age; 
    } 
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> setAge(<span style="color: #0000ff;">int</span><span style="color: #000000;"> age) { 
        </span><span style="color: #0000ff;">this</span>.age =<span style="color: #000000;"> age; 
    } 
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">float</span><span style="color: #000000;"> getHeight() { 
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> height; 
    } 
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> setHeight(<span style="color: #0000ff;">float</span><span style="color: #000000;"> height) { 
        </span><span style="color: #0000ff;">this</span>.height =<span style="color: #000000;"> height; 
    } 
     
}</span></pre>
</div>
<p>2）、properties文件</p>
<div class="cnblogs_code">
<pre class="prism-language-java">pdfPath=E/:/appDataPdf</pre>
</div>
<p>3)、读取properties文件，获取pdf存储的路径</p>
<div class="cnblogs_code">
<pre class="prism-language-java"><span style="color: #0000ff;">package</span><span style="color: #000000;"> com.zcr.until; 
 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.BufferedInputStream; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.File; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.FileInputStream; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.IOException; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.InputStream; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.Properties; 
 
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> GetFilePlace  
{ 
    </span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
     * 读取文件，获取excel保存的根目录 
     * </span><span style="color: #808080;">@return</span><span style="color: #008000;">  excel保存的根目录 
     </span><span style="color: #008000;">*/</span> 
    <span style="color: #0000ff;">public</span><span style="color: #000000;">   String getFilePath() 
    { 
        String dir </span>= System.getProperty("user.dir");  <span style="color: #008000;">//</span><span style="color: #008000;">获得tomcat所在的工作路径   
         
        </span><span style="color: #008000;">//</span><span style="color: #008000;">获取到存储了文件存储位置的filedir.properties 文件路径   ---&gt;java Project的文件路径</span> 
        String realDir = dir + File.separator + "src" + File.separator +"META-INF" + File.separator + "filedir.properties"<span style="color: #000000;">; 
        <br>　　　　 //Web project存储路径 
        </span><span style="color: #008000;">/*</span><span style="color: #008000;">String realDir = dir.substring(0, dir.length()-4) + File.separator +"webapps" + File.separator + "generateExcels"  
                      + File.separator + "classes" + File.separator + "META-INF" + File.separator + "config" + File.separator + "filedir.properties"; 
    </span><span style="color: #008000;">*/</span> 
        <span style="color: #0000ff;">return</span><span style="color: #000000;"> realDir; 
    } 
     
    </span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
     * 获取filePath路径【properities文件】中key对应的值， 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> filePath properities文件路径【包含properities文件】 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> key 要查找的key值 
     * </span><span style="color: #808080;">@return</span><span style="color: #008000;"> key对应的value 
     </span><span style="color: #008000;">*/</span> 
     <span style="color: #0000ff;">public</span><span style="color: #000000;">   String GetValueByKey(String filePath, String key)  
     { 
         Properties pps </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Properties(); 
         </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> { 
              InputStream in </span>= <span style="color: #0000ff;">new</span> BufferedInputStream (<span style="color: #0000ff;">new</span><span style="color: #000000;"> FileInputStream(filePath));   
              pps.load(in); 
             String value </span>=<span style="color: #000000;"> pps.getProperty(key); 
             in.close(); 
             </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> value; 
              
         }</span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) { 
             e.printStackTrace(); 
             </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">null</span><span style="color: #000000;">; 
         } 
     } 
     
    </span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
     * 查询properities文件中可以对应的存储地点 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> key 查询主键 
     * </span><span style="color: #808080;">@return</span><span style="color: #008000;">    key对应的存储地址 
     </span><span style="color: #008000;">*/</span> 
    <span style="color: #0000ff;">public</span><span style="color: #000000;">  String getFileDirFromProperties(String key) 
    { 
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> GetValueByKey(getFilePath(),key); 
    } 
 
}</span></pre>
</div>
<p>4）、获取当天存在的文件路径，不存在则生成一个新的文件夹</p>
<div class="cnblogs_code">
<pre class="prism-language-java"><span style="color: #0000ff;">package</span><span style="color: #000000;"> com.zcr.service; 
 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.File; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.text.SimpleDateFormat; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.Calendar; 
 
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> GenerateFold 
{ 
    </span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
     * 查询当前生成的excel需要存在在哪个路径，如果存在则存储在相应的位置，否则生成改目录， 每天生成一个文件夹，文件夹的命名规则为 年月日的时间戳 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> foldName  生成excel保存路径 
     * </span><span style="color: #808080;">@return</span><span style="color: #008000;">            现在的excel需要保存路径 
     </span><span style="color: #008000;">*/</span> 
    <span style="color: #0000ff;">public</span><span style="color: #000000;">  String getFold(String foldName) 
    { 
        SimpleDateFormat format </span>= <span style="color: #0000ff;">new</span> SimpleDateFormat("yyyyMMdd"<span style="color: #000000;">); 
         
        String todayStr </span>=<span style="color: #000000;"> format.format(Calendar.getInstance().getTime()); 
         
        String foldPath </span>= foldName + File.separator +<span style="color: #000000;"> todayStr;  
         
        File file </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> File(foldPath); 
         
        </span><span style="color: #0000ff;">if</span>(!file.exists() &amp;&amp; !<span style="color: #000000;">file.isDirectory()) 
        { 
            System.out.println(</span>"不存在"<span style="color: #000000;">); 
            file.mkdirs(); 
        } 
        </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> 
        { 
            System.out.println(</span>"存在"<span style="color: #000000;">); 
        } 
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;">  foldPath; 
    } 
 
}</span></pre>
</div>
<p>5）、生成文件的名字</p>
<div class="cnblogs_code">
<pre class="prism-language-java"><span style="color: #0000ff;">package</span><span style="color: #000000;"> com.zcr.until; 
 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.File; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.text.SimpleDateFormat; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.Calendar; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.UUID; 
 
</span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
 * 生成文件名字 
 * </span><span style="color: #808080;">@author</span><span style="color: #008000;"> zcr 
 * 
 </span><span style="color: #008000;">*/</span> 
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> GenerateFileName 
{ 
    </span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
     * 根据文件类别生成文件的名字,文件的命名规则是:文件目录/生成时间-uuid（全球唯一编码）.文件类别 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> fileDir  文件的存储路径 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> fileType 文件的类别 
     * </span><span style="color: #808080;">@return</span><span style="color: #008000;">                 文件的名字   
     </span><span style="color: #008000;">*/</span> 
    <span style="color: #0000ff;">public</span><span style="color: #000000;"> String generateFileName(String fileDir,String fileType) 
    { 
        String saveFileName </span>= ""<span style="color: #000000;">; 
        SimpleDateFormat format </span>= <span style="color: #0000ff;">new</span> SimpleDateFormat("yyyyMMddHHmmssSS"<span style="color: #000000;">); 
        saveFileName </span>+=<span style="color: #000000;"> format.format(Calendar.getInstance().getTime()); 
         
        UUID uuid </span>= UUID.randomUUID();  <span style="color: #008000;">//</span><span style="color: #008000;">全球唯一编码</span> 
<span style="color: #000000;">         
        saveFileName </span>+= "-" +<span style="color: #000000;"> uuid.toString(); 
        saveFileName </span>+= "." +<span style="color: #000000;"> fileType; 
         
        saveFileName </span>= fileDir + File.separator +<span style="color: #000000;"> saveFileName; 
         
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> saveFileName; 
    }</span><span style="color: #000000;"> 
}</span></pre>
</div>
<p>6）、生成pdf</p>
<div class="cnblogs_code">
<pre class="prism-language-java"><span style="color: #0000ff;">package</span><span style="color: #000000;"> com.zcr.service; 
 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.File; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.FileOutputStream; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.IOException; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.lang.reflect.InvocationTargetException; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.lang.reflect.Method; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.ArrayList; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.List; 
 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> com.lowagie.text.Document; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> com.lowagie.text.DocumentException; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> com.lowagie.text.Element; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> com.lowagie.text.Font; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> com.lowagie.text.PageSize; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> com.lowagie.text.Phrase; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> com.lowagie.text.pdf.BaseFont; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> com.lowagie.text.pdf.PdfPCell; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> com.lowagie.text.pdf.PdfPTable; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> com.lowagie.text.pdf.PdfWriter; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> com.zcr.until.GenerateFileName; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> com.zcr.until.GetFilePlace; 
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> com.zcr.until.User; 
 
</span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
 * 生成pdf 
 * </span><span style="color: #808080;">@author</span><span style="color: #008000;"> zcr 
 *  
 </span><span style="color: #008000;">*/</span> 
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> CreatePdf 
{ 
    Document document </span>= <span style="color: #0000ff;">new</span> Document();<span style="color: #008000;">//</span><span style="color: #008000;"> 建立一个Document对象</span> 
 
    <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Font headfont;<span style="color: #008000;">//</span><span style="color: #008000;"> 设置字体大小</span> 
    <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Font keyfont;<span style="color: #008000;">//</span><span style="color: #008000;"> 设置字体大小</span> 
    <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Font textfont;<span style="color: #008000;">//</span><span style="color: #008000;"> 设置字体大小</span> 
 
    <span style="color: #0000ff;">static</span><span style="color: #000000;"> 
    { 
        </span><span style="color: #008000;">//</span><span style="color: #008000;">中文格式</span> 
<span style="color: #000000;">        BaseFont bfChinese; 
        </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> 
        { 
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> 设置中文显示</span> 
            bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H"<span style="color: #000000;">,BaseFont.NOT_EMBEDDED); 
            headfont </span>= <span style="color: #0000ff;">new</span> Font(bfChinese, 10, Font.BOLD);<span style="color: #008000;">//</span><span style="color: #008000;"> 设置字体大小</span> 
            keyfont = <span style="color: #0000ff;">new</span> Font(bfChinese, 8, Font.BOLD);<span style="color: #008000;">//</span><span style="color: #008000;"> 设置字体大小</span> 
            textfont = <span style="color: #0000ff;">new</span> Font(bfChinese, 8, Font.NORMAL);<span style="color: #008000;">//</span><span style="color: #008000;"> 设置字体大小</span> 
<span style="color: #000000;">        } 
        </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) 
        { 
            e.printStackTrace(); 
        } 
    } 
     
    </span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
     * 文成文件 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> file 待生成的文件名 
     </span><span style="color: #008000;">*/</span> 
    <span style="color: #0000ff;">public</span><span style="color: #000000;"> CreatePdf(File file) 
    { 
        document.setPageSize(PageSize.A4);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 设置页面大小</span> 
        <span style="color: #0000ff;">try</span><span style="color: #000000;"> 
        { 
            PdfWriter.getInstance(document, </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> FileOutputStream(file)); 
            document.open(); 
        } 
        </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) 
        { 
            e.printStackTrace(); 
        } 
    } 
     
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> CreatePdf() 
    { 
         
    } 
     
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> initFile(File file) 
    { 
        document.setPageSize(PageSize.A4);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 设置页面大小</span> 
        <span style="color: #0000ff;">try</span><span style="color: #000000;"> 
        { 
            PdfWriter.getInstance(document, </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> FileOutputStream(file)); 
            document.open(); 
        } 
        </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) 
        { 
            e.printStackTrace(); 
        } 
    } 
     
 
    </span><span style="color: #0000ff;">int</span> maxWidth = 520<span style="color: #000000;">; 
     
    </span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
     * 为表格添加一个内容 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> value           值 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> font            字体 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> align            对齐方式 
     * </span><span style="color: #808080;">@return</span><span style="color: #008000;">                添加的文本框 
     </span><span style="color: #008000;">*/</span> 
    <span style="color: #0000ff;">public</span> PdfPCell createCell(String value, Font font, <span style="color: #0000ff;">int</span><span style="color: #000000;"> align) 
    { 
        PdfPCell cell </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> PdfPCell(); 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
        cell.setHorizontalAlignment(align); 
        cell.setPhrase(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Phrase(value, font)); 
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> cell; 
    } 
     
    </span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
     * 为表格添加一个内容 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> value           值 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> font            字体 
     * </span><span style="color: #808080;">@return</span><span style="color: #008000;">                添加的文本框 
     </span><span style="color: #008000;">*/</span> 
    <span style="color: #0000ff;">public</span><span style="color: #000000;"> PdfPCell createCell(String value, Font font) 
    { 
        PdfPCell cell </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> PdfPCell(); 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
        cell.setPhrase(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Phrase(value, font)); 
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> cell; 
    } 
 
    </span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
     * 为表格添加一个内容 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> value           值 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> font            字体 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> align            对齐方式 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> colspan        占多少列 
     * </span><span style="color: #808080;">@return</span><span style="color: #008000;">                添加的文本框 
     </span><span style="color: #008000;">*/</span> 
    <span style="color: #0000ff;">public</span> PdfPCell createCell(String value, Font font, <span style="color: #0000ff;">int</span> align, <span style="color: #0000ff;">int</span><span style="color: #000000;"> colspan) 
    { 
        PdfPCell cell </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> PdfPCell(); 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
        cell.setHorizontalAlignment(align); 
        cell.setColspan(colspan); 
        cell.setPhrase(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Phrase(value, font)); 
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> cell; 
    } 
     
    </span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
     * 为表格添加一个内容 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> value           值 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> font            字体 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> align            对齐方式 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> colspan        占多少列 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> boderFlag        是否有有边框 
     * </span><span style="color: #808080;">@return</span><span style="color: #008000;">                添加的文本框 
     </span><span style="color: #008000;">*/</span> 
    <span style="color: #0000ff;">public</span> PdfPCell createCell(String value, Font font, <span style="color: #0000ff;">int</span> align, <span style="color: #0000ff;">int</span><span style="color: #000000;"> colspan, 
            </span><span style="color: #0000ff;">boolean</span><span style="color: #000000;"> boderFlag) 
    { 
        PdfPCell cell </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> PdfPCell(); 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
        cell.setHorizontalAlignment(align); 
        cell.setColspan(colspan); 
        cell.setPhrase(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Phrase(value, font)); 
        cell.setPadding(</span>3.0f<span style="color: #000000;">); 
        </span><span style="color: #0000ff;">if</span> (!<span style="color: #000000;">boderFlag) 
        { 
            cell.setBorder(</span>0<span style="color: #000000;">); 
            cell.setPaddingTop(</span>15.0f<span style="color: #000000;">); 
            cell.setPaddingBottom(</span>8.0f<span style="color: #000000;">); 
        } 
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> cell; 
    } 
 
    </span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
     * 创建一个表格对象 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> colNumber  表格的列数 
     * </span><span style="color: #808080;">@return</span><span style="color: #008000;">              生成的表格对象 
     </span><span style="color: #008000;">*/</span> 
    <span style="color: #0000ff;">public</span> PdfPTable createTable(<span style="color: #0000ff;">int</span><span style="color: #000000;"> colNumber) 
    { 
        PdfPTable table </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> PdfPTable(colNumber); 
        </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> 
        { 
            table.setTotalWidth(maxWidth); 
            table.setLockedWidth(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">); 
            table.setHorizontalAlignment(Element.ALIGN_CENTER); 
            table.getDefaultCell().setBorder(</span>1<span style="color: #000000;">); 
        } 
        </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) 
        { 
            e.printStackTrace(); 
        } 
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> table; 
    } 
 
    </span><span style="color: #0000ff;">public</span> PdfPTable createTable(<span style="color: #0000ff;">float</span><span style="color: #000000;">[] widths) 
    { 
        PdfPTable table </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> PdfPTable(widths); 
        </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> 
        { 
            table.setTotalWidth(maxWidth); 
            table.setLockedWidth(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">); 
            table.setHorizontalAlignment(Element.ALIGN_CENTER); 
            table.getDefaultCell().setBorder(</span>1<span style="color: #000000;">); 
        } 
        </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) 
        { 
            e.printStackTrace(); 
        } 
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> table; 
    } 
 
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> PdfPTable createBlankTable() 
    { 
        PdfPTable table </span>= <span style="color: #0000ff;">new</span> PdfPTable(1<span style="color: #000000;">); 
        table.getDefaultCell().setBorder(</span>0<span style="color: #000000;">); 
        table.addCell(createCell(</span>""<span style="color: #000000;">, keyfont)); 
        table.setSpacingAfter(</span>20.0f<span style="color: #000000;">); 
        table.setSpacingBefore(</span>20.0f<span style="color: #000000;">); 
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> table; 
    } 
 
    </span><span style="color: #0000ff;">public</span> &lt;T&gt; <span style="color: #0000ff;">void</span> generatePDF(String [] head,List&lt;T&gt; list,<span style="color: #0000ff;">int</span><span style="color: #000000;"> colNum)  
    { 
        Class classType </span>= list.get(0<span style="color: #000000;">).getClass(); 
         
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建一个只有5列的表格</span> 
        PdfPTable table =<span style="color: #000000;"> createTable(colNum); 
 
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 添加备注,靠左，不显示边框</span> 
        table.addCell(createCell("APP信息列表：", keyfont, Element.ALIGN_LEFT, colNum,<span style="color: #0000ff;">false</span><span style="color: #000000;">)); 
         
        </span><span style="color: #008000;">//</span><span style="color: #008000;">设置表头</span> 
        <span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i = 0 ; i &lt; colNum ; i++<span style="color: #000000;">) 
        { 
            table.addCell(createCell(head[i], keyfont, Element.ALIGN_CENTER)); 
        } 
         
         
        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">null</span> != list &amp;&amp; list.size() &gt; 0<span style="color: #000000;">) 
        { 
            </span><span style="color: #0000ff;">int</span> size =<span style="color: #000000;"> list.size(); 
            </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i = 0 ; i &lt; size ; i++<span style="color: #000000;">) 
            { 
                T t </span>=<span style="color: #000000;"> list.get(i); 
                </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> j = 0 ; j &lt; colNum ; j ++<span style="color: #000000;">) 
                { 
                    </span><span style="color: #008000;">//</span><span style="color: #008000;">获得首字母</span> 
                    String firstLetter = head[j].substring(0,1<span style="color: #000000;">).toUpperCase();  
                     
                    </span><span style="color: #008000;">//</span><span style="color: #008000;">获得get方法,getName,getAge等</span> 
                    String getMethodName = "get" + firstLetter + head[j].substring(1<span style="color: #000000;">); 
                    
                    Method method; 
                    </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> 
                    { 
                        </span><span style="color: #008000;">//</span><span style="color: #008000;">通过反射获得相应的get方法，用于获得相应的属性值</span> 
                        method = classType.getMethod(getMethodName, <span style="color: #0000ff;">new</span><span style="color: #000000;"> Class[]{}); 
                        </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> 
                        { 
                             System.out.print(getMethodName </span>+":" + method.invoke(t, <span style="color: #0000ff;">new</span> Class[]{}) +","<span style="color: #000000;">); 
                             </span><span style="color: #008000;">//</span><span style="color: #008000;">添加数据</span> 
                             table.addCell(createCell(method.invoke(t, <span style="color: #0000ff;">new</span><span style="color: #000000;"> Class[]{}).toString(), textfont)); 
                        } 
                        </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IllegalArgumentException e) 
                        { 
                            e.printStackTrace(); 
                        } 
                        </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IllegalAccessException e) 
                        { 
                            e.printStackTrace(); 
                        } 
                        </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (InvocationTargetException e) 
                        { 
                            e.printStackTrace(); 
                        }   
                    } 
                    </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (SecurityException e) 
                    { 
                        e.printStackTrace(); 
                    } 
                    </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (NoSuchMethodException e) 
                    { 
                        e.printStackTrace(); 
                    } 
                } 
                 
                System.out.println(</span>""<span style="color: #000000;">); 
            } 
        } 
         
        </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> 
        { 
            </span><span style="color: #008000;">//</span><span style="color: #008000;">将表格添加到文档中</span> 
<span style="color: #000000;">            document.add(table); 
        } 
        </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (DocumentException e) 
        { 
            e.printStackTrace(); 
        } 
         
        </span><span style="color: #008000;">//</span><span style="color: #008000;">关闭流</span> 
<span style="color: #000000;">        document.close(); 
    } 
     
     
    </span><span style="color: #008000;">/**</span><span style="color: #008000;"> 
     * 提供外界调用的接口，生成以head为表头，list为数据的pdf 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> head  //数据表头 
     * </span><span style="color: #808080;">@param</span><span style="color: #008000;"> list  //数据 
     * </span><span style="color: #808080;">@return</span><span style="color: #008000;">        //excel所在的路径 
     </span><span style="color: #008000;">*/</span> 
    <span style="color: #0000ff;">public</span> &lt;T&gt; String generatePDFs(String [] head,List&lt;T&gt;<span style="color: #000000;"> list) 
    { 
        </span><span style="color: #0000ff;">final</span> String FilePath = "pdfPath"<span style="color: #000000;">; 
        String saveFilePathAndName </span>= ""<span style="color: #000000;">; 
     
        </span><span style="color: #008000;">//</span><span style="color: #008000;">获得存储的根目录</span> 
        String savePath = <span style="color: #0000ff;">new</span><span style="color: #000000;"> GetFilePlace().getFileDirFromProperties(FilePath); 
         
        </span><span style="color: #008000;">//</span><span style="color: #008000;">获得当天存储的路径,不存在则生成当天的文件夹</span> 
        String realSavePath = <span style="color: #0000ff;">new</span><span style="color: #000000;"> GenerateFold().getFold(savePath); 
         
        saveFilePathAndName </span>= <span style="color: #0000ff;">new</span> GenerateFileName().generateFileName(realSavePath,"pdf"<span style="color: #000000;">); 
         
        File file </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> File(saveFilePathAndName); 
        </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> 
        { 
            file.createNewFile(); 
        } 
        </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e1) 
        { 
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> TODO Auto-generated catch block</span> 
<span style="color: #000000;">            e1.printStackTrace(); 
        } 
            initFile(file); 
        </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> 
        { 
            file.createNewFile();  </span><span style="color: #008000;">//</span><span style="color: #008000;">生成一个pdf文件</span> 
<span style="color: #000000;">        } 
        </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) 
        { 
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> TODO Auto-generated catch block</span> 
<span style="color: #000000;">            e.printStackTrace(); 
        } 
        </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> CreatePdf(file).generatePDF(head,list,head.length); 
         
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> saveFilePathAndName; 
    } 
</span><span style="color: #000000;"> 
}</span></pre>
</div>
<p>7）、测评函数</p>
<div class="cnblogs_code">
<pre class="prism-language-java">    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args)  
    { 
        System.out.println(</span>"begin"<span style="color: #000000;">); 
         
        String [] head </span>= {"name","sex","adress","height","age","jj"<span style="color: #000000;">}; 
         
        List</span>&lt;User&gt; list = <span style="color: #0000ff;">new</span> ArrayList&lt;User&gt;<span style="color: #000000;">(); 
        User user1 </span>= <span style="color: #0000ff;">new</span> User("zhangsan",1,1.1f,"北京","男","AA"<span style="color: #000000;">); 
        User user2 </span>= <span style="color: #0000ff;">new</span> User("lisi",22222,3.2f,"上海","女","BB"<span style="color: #000000;">); 
         
        list.add(user1); 
        list.add(user2); 
         
         
        String filePath </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> CreatePdf().generatePDFs(head,list); 
        System.out.println(filePath); 
        System.out.println(</span>"end"<span style="color: #000000;">); 
    }</span></pre>
</div>
<p>8）、测试结果</p>
<p><noscript><img decoding="async" src="http://blog.ytso.com/zb_users/plugin/LazyLoad/usr/loading.gif" data-original="/img/99f685f8e42e47de91ba8e652eecf87e.png" alt="java 如何在pdf中生成表格详解编程语言"></noscript><img decoding="async" class="j-lazy" src="https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png" data-original="http://blog.ytso.com/zb_users/plugin/LazyLoad/usr/loading.gif" data-original="/img/99f685f8e42e47de91ba8e652eecf87e.png" alt="java 如何在pdf中生成表格详解编程语言"></p>
<p>9）、文件内容如下</p>
<p><noscript><img decoding="async" src="http://blog.ytso.com/zb_users/plugin/LazyLoad/usr/loading.gif" data-original="/img/860b0cbf788c43d9ad98ba32648abd1f.jpg" alt="java 如何在pdf中生成表格详解编程语言"></noscript><img decoding="async" class="j-lazy" src="https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png" data-original="http://blog.ytso.com/zb_users/plugin/LazyLoad/usr/loading.gif" data-original="/img/860b0cbf788c43d9ad98ba32648abd1f.jpg" alt="java 如何在pdf中生成表格详解编程语言"></p>
<p style="background-color: #0098dd; color: white; font-size: 17px; font-weight: bold;">4、其他相关链接</p>
<p>生成可变表头excel：<a href="http://www.cnblogs.com/0201zcr/p/4950619.html" target="_blank" rel="noopener">http://www.cnblogs.com/0201zcr/p/4950619.html</a></p>
<p>读取excel：<a href="http://www.cnblogs.com/0201zcr/p/4656779.html" target="_blank" rel="noopener">http://www.cnblogs.com/0201zcr/p/4656779.html</a></p>
<p>&nbsp;</p>
<p>　　<strong>致谢</strong>：感谢您的阅读！</p>
<div class="entry-readmore"><div class="entry-readmore-btn"></div></div>                                                        <div class="entry-copyright"><p>原创文章，作者：ItWorker，如若转载，请注明出处：https://blog.ytso.com/tech/pnotes/15131.html</p></div>                        </div>

                        <div class="entry-tag"><a href="https://blog.ytso.com/tag/c" rel="tag">c</a></div>
                        <div class="entry-action">
                            <div class="btn-zan" data-id="15131"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-thumb-up-fill"></use></svg></i> 赞 <span class="entry-action-num">(0)</span></div>
                                                    </div>

                        <div class="entry-bar">
                            <div class="entry-bar-inner">
                                                                <div class="entry-bar-info entry-bar-info2">
                                    <div class="info-item meta">
                                                                                    <a class="meta-item j-heart" href="javascript:;" data-id="15131"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-star"></use></svg></i> <span class="data">0</span></a>                                        <a class="meta-item" href="#comments"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-comment"></use></svg></i> <span class="data">0</span></a>                                                                            </div>
                                    <div class="info-item share">
                                                                                    <a class="meta-item mobile j-mobile-share" href="javascript:;" data-id="15131" data-qrcode="https://blog.ytso.com/tech/pnotes/15131.html">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-share"></use></svg></i> 生成海报                                            </a>
                                                                                    <a class="meta-item wechat" data-share="wechat" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-wechat"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item weibo" data-share="weibo" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-weibo"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item qq" data-share="qq" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-qq"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item qzone" data-share="qzone" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-qzone"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item douban" data-share="douban" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-douban"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item linkedin" data-share="linkedin" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-linkedin"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item facebook" data-share="facebook" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-facebook"></use></svg></i>                                            </a>
                                                                                    <a class="meta-item twitter" data-share="twitter" target="_blank" rel="nofollow noopener noreferrer" href="#">
                                                <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-twitter"></use></svg></i>                                            </a>
                                                                            </div>
                                    <div class="info-item act">
                                        <a href="javascript:;" id="j-reading"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-article"></use></svg></i></a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                                            <div class="entry-page">
                    <div class="entry-page-prev j-lazy" style="background-image: url('https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png');" data-original="http://blog.ytso.com/zb_users/plugin/LazyLoad/usr/loading.gif">
                <a href="https://blog.ytso.com/tech/pnotes/15130.html" title="通过url 下载文件详解编程语言" rel="prev">
                    <span>通过url 下载文件详解编程语言</span>
                </a>
                <div class="entry-page-info">
                    <span class="pull-left"><i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-arrow-left-double"></use></svg></i> 上一篇</span>
                    <span class="pull-right">2021年7月19日 17:32</span>
                </div>
            </div>
                            <div class="entry-page-next j-lazy" style="background-image: url('https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png');" data-original="http://blog.ytso.com/zb_users/plugin/LazyLoad/usr/loading.gif">
                <a href="https://blog.ytso.com/tech/pnotes/15132.html" title="如何生成可变表头的excel详解编程语言" rel="next">
                    <span>如何生成可变表头的excel详解编程语言</span>
                </a>
                <div class="entry-page-info">
                    <span class="pull-right">下一篇 <i class="wpcom-icon wi"><svg aria-hidden="true"><use xlink:href="#wi-arrow-right-double"></use></svg></i></span>
                    <span class="pull-left">2021年7月19日 17:32</span>
                </div>
            </div>
            </div>
                                        <error>
    <code>wp_die</code>
    <title><![CDATA[WordPress &amp;rsaquo; Error]]></title>
    <message><![CDATA[&lt;h1&gt;Error establishing a Redis connection&lt;/h1&gt;
&lt;p&gt;To disable Redis, delete the &lt;code&gt;object-cache.php&lt;/code&gt; file in the &lt;code&gt;/wp-content/&lt;/code&gt; directory.&lt;/p&gt;
]]></message>
    <data>
        <status>500</status>
    </data>
</error>
<!--
Performance optimized by Redis Object Cache. Learn more: https://wprediscache.com

Retrieved 1752 objects (424 KB) from Redis using Predis (v2.4.0).
-->
