源代码请从这里下载:

http://download.csdn.net/source/2984395

 

使用的是JSP编程

‍ 这是导出后的效果

 

这是数据库中的内容

‍ 部分代码:

<%@ page language="java" contentType="text/html" pageEncoding="GBK"%>

<%@ page import="com.jwy.dao.*" %>

<%@ page import="com.lowagie.text.pdf.*" %>

<%@ page import="com.lowagie.text.*" %>

<%@ page import="java.io.*" %>

 

<html>

  <head>

    <title>'index.jsp'</title>

    <!--

<link rel="stylesheet" type="text/css" href="styles.css">

    -->

  </head>

  

  <body>

    <% 

    out.clear();

    out = pageContext.pushBody();

    

    response.setHeader("Content-Disposition","attachment;filename=stuInfo.pdf");

    response.setContentType("application/x-download; charset=utf-8");

    

    java.util.List<String[]> list = new StuInfoDao().findByAll();

    

    //创建一个对中文字符集支持的基础字体

    BaseFont bfChinese = BaseFont.createFont("STSong-Light",

    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

    //使用基础字体对象创建新字体对像,粗体12号红色字

    Font font = new Font(bfChinese, 12, Font.BOLD); 

    Document document = new Document(PageSize.A4);//创建document对象

    PdfWriter.getInstance(document, response.getOutputStream());//创建书写器

    document.open();//打开文档

    String title = "学生信息表"; //文档标题

    Paragraph paragraph = new Paragraph(title, font); //创建段落,并设置字体

    paragraph.setAlignment(Paragraph.ALIGN_CENTER);//设置段落居中

    document.add(paragraph);//将段落添加到文档中

    PdfPTable table = new PdfPTable(5);//建立一个5列的空白表格对象

    table.setSpacingBefore(30f);//设置表格上面空白宽度

    String[] tableTitle = { "学号", "姓名", "性别", "出生日期", "联系电话" };//表头

    for (int i = 0; i < tableTitle.length; i++) {//循环写入表头

    paragraph = new Paragraph(tableTitle[i], new Font(bfChinese, 10,Font.BOLD));

    PdfPCell cell = new PdfPCell(paragraph);//建立一个单元格

    cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置内容水平居中显示

    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中

    table.addCell(cell);//将单元格加入表格

    }

    for(int i=0;i<list.size();i++){//循环写入表文

    String[] stuInfo = list.get(i);

    for(int j=1;j<stuInfo.length;j++){

    PdfPCell cell = new PdfPCell(new Paragraph(stuInfo[j],

       new Font(bfChinese, 10)));//建立一个单元格

       cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置内容水平居中显示

       cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中

       table.addCell(cell);//将单元格加入表格

    }

    }

    document.add(table);//将表格加入文档中

    document.close();//关闭文档

    %>

  </body>

</html>