当前位置: 首页 > 图文教程 > 网络编程 > JSP > Web页面数据批量录入----使用上传组件与JXL工具包联合实现

JSP
我认为JSP有问题(上)
我认为JSP有问题(下)
jsp“抓”网页代码的程序
关于在bean里面打印html的利弊看法
bean里面如何打印到html页面
jdbc3中的RowSet 接口规范
Apusic Application Server1.0中jsp源代码泄漏漏洞
Unify的eWave ServletExec拒绝服务漏洞
通过提交超长的GET请求导致IBM HTTP Server远程溢出
在HTTP请求中添加特殊字符导致暴露JSP源代码文件
Resin 1.2 重要源代码暴露漏洞
多中WEB服务器的通用JSp源代码暴露漏洞
Tomcat 暴露JSP文件内容
IBM WebSphere Application Server 暴露JSP文件内容
JRun 2.3.x 范例文件暴露站点安全信息
BEA WebLogic 暴露源代码漏洞
IBM WebSphere Application Server 3.0.2 存在暴露源代码漏洞
Tomcat 3.1 存在暴露网站路径问题
Sun Java Web Server 能让攻击者远程执行任意命令
Netscape 修复 JAVA 安全漏洞

JSP 中的 Web页面数据批量录入----使用上传组件与JXL工具包联合实现


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-03   浏览: 592 ::
收藏到网摘: n/a

     JspsmartUpload可以对一般的文件进行上传,而jxl工具包提供良好的对MSExcel文件格式的读取性能,可以结合这两者在Web页面上通过Excel文件对数据进行批量录入。
这里提供一点思路,供大家参考。
    首先可以将文件上传到Web服务器的某个地方,可以根据需要任意选取,然后通过使用工具包对文件进行读取,最后删除该临时文件。
    我使用的是jspSmartUpload包的源代码编译后自己压缩而成的包,而Jxl则是在网上找到的,包的全名是jexcelapi_2_3_7,可以通过google进行搜索找到,如果需要可以与我联系。
   以下是整个最为简单的例子代码,可供大家参考:
index.jsp:
  1. <%@ page contentType="text/html; charset=GBK" %>
  2. <HTML>
  3. <BODY BGCOLOR="white">
  4. <H1>上载界面</H1>
  5. <HR>
  6. <FORM METHOD="POST" ACTION="/WebExcel/display.jsp" ENCTYPE="multipart/form-data">
  7.    <INPUT TYPE="FILE" NAME="FILE1" SIZE="50" ><BR>
  8.    <INPUT TYPE="SUBMIT" VALUE="上载">
  9. </FORM>
  10. </BODY>
  11. </HTML>

display.jsp

  1. <!--
  2.   @author 杨丰
  3.   @time 2003年7月16日
  4.   @用于实现基本的Web页面读取Excel
  5. -->
  6. <%@ page contentType="text/html; charset=GBK"
  7.   language="java" import="jxl.*,com.jspsmart.upload.*" %>
  8. <jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
  9. <HTML>
  10. <BODY BGCOLOR="white">
  11. <H1>Excel文件内容显示</H1>
  12. <HR>
  13. <%
  14.     int count=0;
  15.     mySmartUpload.initialize(pageContext);
  16.     mySmartUpload.upload();
  17.     for (int i=0;i<mySmartUpload.getFiles().getCount();i++){
  18.         com.jspsmart.upload.File myFile = 
  19.                      mySmartUpload.getFiles().getFile(i);
  20.         if (!myFile.isMissing()) {
  21.             myFile.saveAs("/upload/"+myFile.getFileName());
  22.         }
  23.     }
  24.     int ROW = 4;
  25.     int COLUMN = 3;
  26.     ServletContext servletContext = pageContext.getServletContext();
  27.     java.io.File xmlFile =
  28.         new java.io.File(servletContext.getRealPath("upload/Book1.xls"));
  29.     Cell cell = null;
  30.         try{
  31.             Workbook workbook = Workbook.getWorkbook(xmlFile);
  32.             Sheet sheet = workbook.getSheet(0);
  33.         out.println("<table border=1>");
  34.             for(int j=0;j<ROW;j++){
  35.                 out.println("<tr>");
  36.                 for(int i=0;i<COLUMN;i++){
  37.                 out.println("<td>");
  38.                     cell = sheet.getCell(i,j);
  39.                     out.print(cell.getContents());
  40.                 out.println("</td>");
  41.                 }
  42.                 out.println("</tr>");
  43.             }
  44.                 out.println("</table>");
  45.                 workbook.close();
  46.         xmlFile.delete();
  47.         }catch(Exception e){
  48.             System.out.println(e);
  49.               }
  50. %>
  51. </BODY>
  52. </HTML>

实现这一功能的前提是Excel文件格式必须事先确定。