想请问..在网络上找到了一个导出SQL并存成Excel的范例,但是实作后一直出现问题...自己找也找不到...想麻烦大家是否可以帮小弟解答...谢谢..
当第一个网页搜寻到关键字后,就会跳到下方这个JSP档并存成Excel
<%@ page contentType="text/html;charset=utf-8" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="user.Prof"%>
<%@ page import="org.apache.poi.hssf.usermodel.*"%>
<html>
<head><title>try</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<%
request.setCharacterEncoding("utf-8");
String ID=request.getParameter("ID");
String strsel="";
Connection con = db.ConnectionPool.getIstance().getConnection();
Statement stmt;
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=null;
strsel = "SELECT ID,name,number,ss,smobile FROM student '%"+ID+"%'";
rs = stmt.executeQuery(strsel);int CountColumnNum = rs.getMetaData().getColumnCount() ;
int i = 1 ;
HSSFWorkbook wb = new HSSFWorkbook() ;
HSSFSheet sheet = wb.createSheet("student") ;
HSSFRow firstrow = sheet.createRow(0);
HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
String[] names ={"ID","姓名","性别","电话"};
for(int j= 0 ;j<CountColumnNum; j++){
firstcell[j] = firstrow.createCell((short)j);
firstcell[j].setCellValue(new HSSFRichTextString(names[j]));}while(rs.next()){
HSSFRow row = sheet.createRow(i) ;for(int j=0;j<CountColumnNum;j++){
HSSFCell cell = row.createCell((short) j) ;
cell.setCellValue(new HSSFRichTextString(rs.getString(j+1))) ;}
i++ ;}
%>
<%
FileOutputStream fso=new FileOutputStream("C://AppServ/Tomcat/webapps/app/excel1.xls");
wb.write(fso);
fso.close();
rs.close() ;
con.close() ;
%>