首先將POI組件中的jar包放入項目中路徑下的WEB-INF/lib目錄中,而後即可以在JSP中使用POI組件了。javascript
新建一數據庫鏈接類Conn.java,代碼以下:html
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集合,程序代碼以下:java
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;程序代碼以下:mysql
<%@ 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頁面;效果以下:sql
單擊「導出報表」按鈕,出現一個提示框數據庫
打開E盤,能夠看到已經生成一個名爲workbook.xls的文件,打開此文件,以下:apache