1.将POI组件中的jar包放入项目中路径下的WEB-INF/lib目录中

2.在javabean中创建一个poi的java文件,中间出了创建getXXX(),setXXX()方法外,加入readRecord(int rsRow)方法,返回值是list集合(需要遍历);

3.必须创建一个HSSFSheet实例,使用POI组件中的createSheet("sheetname")创建工作表,根据显示内容大小创建指定大小的单元格。

4.最后创建一JSP页面在看到合同的同时还能打印下载到Excel

首先将POI组件中的jar包放入项目中路径下的WEB-INF/lib目录中,然后便可以在JSP中使用POI组件了。

新建一数据库连接类Conn.java,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Conn {
    private final String dbDriver="com.mysql.jdbc.Driver" ;
    private final String url="jdbc:mysql://localhost:3306/employee";
    private final String userName = "root";
    private final String password = "ldm";
    private Connection con=null;
    Statement s=null;
    public Conn(){
        try{
            Class.forName(dbDriver);
            con=DriverManager.getConnection(url,userName,password);
             
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
     
    public ResultSet executeQuery(String sql){
        ResultSet rs=null;
        try{
            Conn conn=new Conn();
            s=con.createStatement();
            rs=s.executeQuery(sql);
        }catch(Exception e){
            e.printStackTrace();
        }
        return rs;
    }
     
    public void close(){
        try{
            if(s!=null||con!=null){
                s.close();
                con.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

为了将从数据库中取出的数据放入List,创建JavaBean,命名为POITest.java;这个JavaBean除了setXXX()和getXXX()方法之外,还有一个根据用户需要显示的行数从数据库中取值ReadRecord(int rsRow)方法,它的返回值类型是List集合,程序代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.List;
import java.util.ArrayList;
 
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 
public class POITest {
     
    private int id;
    private String studentCode;
    private String name;
    private String sex;
    private String institute;
    private String department;
    private String classes;
    private String phone;
    private String address;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getStudentCode() {
        return studentCode;
    }
    public void setStudentCode(String studentCode) {
        this.studentCode = studentCode;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getInstitute() {
        return institute;
    }
    public void setInstitute(String institute) {
        this.institute = institute;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public String getClasses() {
        return classes;
    }
    public void setClasses(String classes) {
        this.classes = classes;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
 
      
     public static List ReadRecord(int rsRow){
         Conn conn=new Conn();
         String sql="select * from student where id<='"+rsRow+"'";
         ResultSet rs=conn.executeQuery(sql);
         List l=new ArrayList();
         try{
             while(rs.next()){
                 POITest p=new POITest();
                 p.setId(rs.getInt("id"));
                 p.setStudentCode(rs.getString("studentCode"));
                 p.setName(rs.getString("name"));
                 p.setSex(rs.getString("sex"));
                 p.setInstitute(rs.getString("institute"));
                 p.setDepartment(rs.getString("department"));
                 p.setClasses(rs.getString("classes"));
                 p.setPhone(rs.getString("phone"));
                 p.setAddress(rs.getString("address"));
                 l.add(p);
             }
             conn.close();
         }catch(Exception e){
             e.printStackTrace();
         }
         return l;
     }
         
     public static List ReadRecord(){
         Conn conn=new Conn();                     //连接数据库
         String sql="select * from student";       //查询学生信息
         ResultSet rs=conn.executeQuery(sql);      //执行查询
         List l=new ArrayList();                   //定义List集合
          
            try{   
                while(rs.next()){
                     POITest p=new POITest();       //实例化对象
                     p.setId(rs.getInt("id"));      //为对象赋值
                     p.setStudentCode(rs.getString("studentCode"));
                     p.setName(rs.getString("name"));
                     p.setSex(rs.getString("sex"));
                     p.setInstitute(rs.getString("institute"));
                     p.setDepartment(rs.getString("department"));
                     p.setClasses(rs.getString("classes"));
                     p.setPhone(rs.getString("phone"));
                     p.setAddress(rs.getString("address"));
                     l.add(p);                      //将对象添加到List集合中
                     
                }
                conn.close();                       //关闭数据库
            }catch(Exception e){
                e.printStackTrace();
            }
            return l;                               //返回集合
             
     }
     /*
      * 为了添加工作表,必须创建一个HSSFSheet实例,使用POI组件中的createSheet("sheetname")创建工作表,根据显示内容大小创建指定大小的单元格。
      * 程序代码如下:
      *
      */
     public void WriteCell(int rsRow){
         HSSFWorkbook wb=new HSSFWorkbook();
         HSSFSheet sheet=wb.createSheet("poiexample");//建立名为poiexample的报表
         sheet.setColumnWidth(0, 2500);
         sheet.setColumnWidth(1, 6000);
         sheet.setColumnWidth(2, 3500);
         sheet.setColumnWidth(3, 9000);
         sheet.setColumnWidth(4, 9000);
         sheet.setColumnWidth(5, 8000);
         sheet.setColumnWidth(6, 8000);
         sheet.setColumnWidth(7, 9000);
         sheet.setColumnWidth(8, 9000);
         HSSFRow row=sheet.createRow(0);
         HSSFCell cell[]=new HSSFCell[9];
         for(int i=0;i<9;i++){                //遍历每行的每列
             cell[i]=row.createCell(i);
         }
         cell[0].setCellValue("id号");         //在单元格写入文字
         cell[1].setCellValue("学号");
         cell[2].setCellValue("姓名");
         cell[3].setCellValue("性别");
         cell[4].setCellValue("系别");
         cell[5].setCellValue("专业");
         cell[6].setCellValue("班级");
         cell[7].setCellValue("电话");
         cell[8].setCellValue("地址");
          
         List l=POITest.ReadRecord(rsRow);
         if(l.size()>0 && l!=null){
             for(int i=0;i<l.size();i++){
                 POITest p2=(POITest)l.get(i);
                 HSSFRow datarow=sheet.createRow(i+1);
                 HSSFCell data[]=new HSSFCell[9];
                 for(int j=0;j<9;j++){
                     data[j]=datarow.createCell(j);
                 }
                 data[0].setCellValue(p2.getId());
                 data[1].setCellValue(p2.getStudentCode());
                 data[2].setCellValue(p2.getName());
                 data[3].setCellValue(p2.getSex());
                 data[4].setCellValue(p2.getInstitute());
                 data[5].setCellValue(p2.getDepartment());
                 data[6].setCellValue(p2.getClasses());
                 data[7].setCellValue(p2.getPhone());
                 data[8].setCellValue(p2.getAddress());
             }
         }
         File file=new File("e:\\workbook.xls");
         try{
             FileOutputStream fileOut=new  FileOutputStream(file);
             wb.write(fileOut);
             fileOut.close();
         }catch(IOException e){
             e.printStackTrace();
         }
     }
}

最后创建一JSP页面,命名为PoiTest.jsp;程序代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*,com.wsy.POITest,java.io.*,java.io.FileOutputStream,
    org.apache.poi.hssf.usermodel.*,java.util.*,java.text.*"%>
      <jsp:useBean id="poi" scope="page" class="com.wsy.POITest"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>学生管理系统</title>
</head>
<script type="text/javascript" language="javascript">
      function fn1(){
         <%poi.WriteCell(9); %>
         alert("导出报表完毕!导出位置为e:\\workbook.xls!!!");
      }
</script>
<body>
<table border="1">
<tr><td>ID号</td><td>学号</td><td>姓名</td><td>性别</td><td>系别</td><td>专业</td><td>班级</td><td>电话</td><td>地址</td></tr>
<% List l=poi.ReadRecord();
for(int i=0;i<l.size();i++){
    POITest p2=(POITest)l.get(i);
%><tr>
<td><%=p2.getId() %><td>
<td><%=p2.getStudentCode() %></td>
<td><%=p2.getName()%></td>
<td><%=p2.getSex() %></td>
<td><%=p2.getInstitute() %></td>
<td><%=p2.getDepartment() %></td>
<td><%=p2.getClasses() %></td>
<td><%=p2.getPhone() %></td>
<td><%=p2.getAddress() %></td></tr>
<% 
}
%>
</table><br/>
<input type="button" value="导出报表" onClick='fn1()'/>
</body>
</html>

重启Tomcat服务器,在IE浏览器中运行JSP页面;效果如下:

单击“导出报表”按钮,出现一个提示框

打开E盘,可以看到已经生成一个名为workbook.xls的文件,打开此文件,如下:

POI打印Excel报表的更多相关文章

  1. 使用POI生成Excel报表

    先把报表模板截图贴上来 下面是POI编写的报表生成类ExcelReport.java package com.jadyer.report; import java.io.FileNotFoundExc ...

  2. 10、借助POI实现Java生成并打印excel报表(1)

    10.1.了解 Apache POI 实际开发中,用到最多的是把数据库中数据导出生成报表,尤其是在生产管理或者财务系统中用的非常普遍.生成报表格式一般是EXCEL或者PDF .利用Apache  PO ...

  3. 11、借助POI实现Java生成并打印excel报表(2)

    11.POI打印功能 11.1.常用模块形式: HSSFPrintSetup printSetup = sheet.getPrintSetup(); printSetup.setVResolution ...

  4. poi导出Excel报表多表头双层表头、合并单元格

    效果图: controller层方法: /**     *      * 导出Excel报表     * @param request     * @return     *      */    @ ...

  5. ASP.NET打印EXCEl报表技术总结

    序言:我们在做企业项目或者一些管理系统的时候往往会用到导出到excel报表这项功能,下面我介绍的是用windows自带的excel来打印 首先必须引入:Interop.Excel.dll.Intero ...

  6. poi生成excel报表合并列

    功能任务 poi导出excel统计报表,有合并列的. 根据结构生成层级. 目标 1输入一个linkshashmap LinkedHashMap<String, Object> fieldM ...

  7. POI打印Excel

    一.POI概述 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 结构:HSSF - 提供读写Mic ...

  8. apache poi导出excel报表

    Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能.POI为"P ...

  9. java使用POI读取excel报表

    留此作为记录 package com.demo; import java.io.FileInputStream; import java.util.Iterator; import org.apach ...

随机推荐

  1. ShellExecute调用另外一个进程(demo为一个控制led的一段代码)

     public enum ShowCommands : int         {             SW_HIDE = 0,             SW_SHOWNORMAL = 1,    ...

  2. discuz手机版模板开发

    1.触屏版模板手机路径 discuz X3触屏版模板路径:/template/default/touch/forum/discuz.htm(主页面模板) discuz X3标准版模板路径:/templ ...

  3. java selenium (十二) 操作弹出窗口

    selenium 中如何处理弹出窗口 阅读目录 原理 在代码里, 通过         Set<String> allWindowsId = driver.getWindowHandles ...

  4. javascript无线端的判断

    经常在工作中要求网页做这样的处理判断:针对无线端的网页,我们需要兼容一种异于PC端的网页效果. 如下代码就可以实现无线端的判断: var mobs = ['android', 'ipad', 'ipo ...

  5. [版本管理]有惊无险修复svn服务器Invalid filesystem revision number问题

    问题起因:某一天下午,团队成员在向svn服务端提交新内容,突然整栋楼断电了,自然,提交的过程被中断了.当时,还没有什么想法. 等有电后,另外一同事在update项目时,发现无法正常使用svn,一直报异 ...

  6. Android中的Service小结

    简介 Service适合执行不需要和用户交互,而且长期运行的任务.即使程序被切换回后台,服务仍然可以正常运行.Service并不自动开启线程,默认运行在主线程中. Service中需要重载的函数 on ...

  7. 对于Python中self的看法

    首先看一段Java代码 public class Test { public String name; public int age; public String gender; public Str ...

  8. Python Day03

    set Collections系列: Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上,提供了几 ...

  9. 【墙内备份】Android 6.0 APIs

    Android 6.0 APIs In this documentSHOW MORE Fingerprint Authentication Confirm Credential App Linking ...

  10. christian louboutin ballerinas outlet

    www.heelschuhe.de, Wie auch immer, Schneiden auf den Punkt; David und Connie in Vancouver: Wir hoffe ...