如何将Excel工作表中的数据导入jsp页面?

时间:2015-10-26 15:35:22

标签: java jsp-tags

我尝试读取excel文件并将其显示在jsp页面中,我使用下面的代码,但它收到错误消息。

代码:

<%@page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%@page import="java.io.*" %>
<%@page import="java.util.*" %>
<html>
<table border="1">
<%
 short a=0;
  short b=1;
  short c=2;
  short d=3;
  int i=0;
   String   value1="", value2="",value3=" ", value4="";
    String filename ="a.xlsx"; 
    if (filename != null && !filename.equals("")) {
    try{
    FileInputStream fs =new FileInputStream(filename);
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    for (int k = 0; k < wb.getNumberOfSheets(); k++){
    int j=i+1;
    HSSFSheet sheet = wb.getSheetAt(k);
    int rows  = sheet.getPhysicalNumberOfRows();
    for (int r = 0; r < rows; r++){
    HSSFRow row   = sheet.getRow(r);
    int     cells = row.getPhysicalNumberOfCells(); 
    out.write("<br>");
    HSSFCell cell1  = row.getCell(a);
      value1 = cell1.getStringCellValue();
      HSSFCell cell2  = row.getCell(b);
        value2 = cell2.getStringCellValue();
         HSSFCell cell3  = row.getCell(c);
         value3 = cell3.getStringCellValue();
          HSSFCell cell4  = row.getCell(d);
          value4 = cell4.getStringCellValue();
    %>
    <tr><td><%=value1%></td><td><%=value2%></td><td><%=value3%></td><td><%=value4%></td></tr>
    <%
    }
        i++;
    }
    }
    catch(Exception e){
        System.out.println(e);
    }
    }
    %>
    </table>
    </html>

错误消息

  

HTTP状态500 - 无法为JSP编译类:键入Exception   报告消息无法为JSP编译类:description   服务器遇到内部错误,导致无法完成   这个请求。异常org.apache.jasper.JasperException:无法执行   JSP的编译类:在行中发生错误:[14]   生成的java文件:   [C:\用户\武\ workspace.metadata.plugins \ org.eclipse.wst.server.core \ TMP0 \工作\卡塔利娜\本地主机\ jpsexcel \组织\阿帕奇\ JSP \ index_jsp.java]   只能导入一种类型。 org.apache.poi.hssf.usermodel.HSSFSheet   解析为包

     

生成的java文件中的行:[15]发生错误:   [C:\用户\武\ workspace.metadata.plugins \ org.eclipse.wst.server.core \ TMP0 \工作\卡塔利娜\本地主机\ jpsexcel \组织\阿帕奇\ JSP \ index_jsp.java]   只能导入一种类型。   org.apache.poi.hssf.usermodel.HSSFWorkbook解析为包An   在生成的java文件中的行:[16]处发生错误:   [C:\用户\武\ workspace.metadata.plugins \ org.eclipse.wst.server.core \ TMP0 \工作\卡塔利娜\本地主机\ jpsexcel \组织\阿帕奇\ JSP \ index_jsp.java]   只能导入一种类型。 org.apache.poi.hssf.usermodel.HSSFRow   解析为包生成的行中出现错误:[17]   java文件:   [C:\用户\武\ workspace.metadata.plugins \ org.eclipse.wst.server.core \ TMP0 \工作\卡塔利娜\本地主机\ jpsexcel \组织\阿帕奇\ JSP \ index_jsp.java]   只能导入一种类型。 org.apache.poi.hssf.usermodel.HSSFCell   解析为包在jsp文件中的行:20处发生错误:   /index.jsp HSSFWorkbook无法解析为类型17:if   (filename!= null&amp;&amp;!filename.equals(&#34;&#34;)){18:try {19:
  FileInputStream fs = new FileInputStream(filename); 20:
  HSSFWorkbook wb =新HSSFWorkbook(fs); 21:for(int k = 0; k&lt;   wb.getNumberOfSheets(); k ++){22:int j = i + 1; 23:HSSFSheet   sheet = wb.getSheetAt(k);在jsp中的行:20处发生错误   file:/index.jsp HSSFWorkbook无法解析为类型17:if   (filename!= null&amp;&amp;!filename.equals(&#34;&#34;)){18:try {19:
  FileInputStream fs = new FileInputStream(filename); 20:
  HSSFWorkbook wb =新HSSFWorkbook(fs); 21:for(int k = 0; k&lt;   wb.getNumberOfSheets(); k ++){22:int j = i + 1; 23:HSSFSheet   sheet = wb.getSheetAt(k);

     

jsp文件中的第23行出现错误:/index.jsp HSSFSheet   无法解析为20型:HSSFWorkbook wb = new   HSSFWorkbook(FS); 21:for(int k = 0; k&lt; wb.getNumberOfSheets();   k ++){22:int j = i + 1; 23:HSSFSheet sheet = wb.getSheetAt(k);   24:int rows = sheet.getPhysicalNumberOfRows(); 25:for(int   r = 0; r&lt;行; r ++){26:HSSFRow row = sheet.getRow(r);一个   在jsp文件中的行:26发生错误:/ index.jsp HSSFRow不能   被解析为23型:HSSFSheet sheet = wb.getSheetAt(k); 24:   int rows = sheet.getPhysicalNumberOfRows(); 25:for(int r = 0; r   &LT;行; r ++){26:HSSFRow row = sheet.getRow(r); 27:int
  cells = row.getPhysicalNumberOfCells(); 28:out.write(&#34;
&#34;);   29:HSSFCell cell1 = row.getCell(a);

     

jsp文件中的第29行:/index.jsp HSSFCell发生错误   无法解析为类型26:HSSFRow row = sheet.getRow(r);   27:int cells = row.getPhysicalNumberOfCells(); 28:
  out.write(&#34;
&#34); 29:HSSFCell cell1 = row.getCell(a); 30:
  value1 = cell1.getStringCellValue(); 31:HSSFCell cell2 =   row.getCell(b)中; 32:value2 = cell2.getStringCellValue();一个   在jsp文件中的行:31处发生错误:/ index.jsp HSSFCell不能   被解析为28型:out.write(&#34;
&#34;); 29:HSSFCell   cell1 = row.getCell(a); 30:value1 =   cell1.getStringCellValue(); 31:HSSFCell cell2 =   row.getCell(b)中; 32:value2 = cell2.getStringCellValue(); 33:
  HSSFCell cell3 = row.getCell(c); 34:value3 =   cell3.getStringCellValue();在jsp中的第33行发生错误   file:/index.jsp HSSFCell无法解析为类型30:
  value1 = cell1.getStringCellValue(); 31:HSSFCell cell2 =   row.getCell(b)中; 32:value2 = cell2.getStringCellValue(); 33:
  HSSFCell cell3 = row.getCell(c); 34:value3 =   cell3.getStringCellValue(); 35:HSSFCell cell4 =   row.getCell(d); 36:value4 = cell4.getStringCellValue();

     

jsp文件中的第35行发生错误:/index.jsp HSSFCell   无法解析为类型32:value2 =   cell2.getStringCellValue(); 33:HSSFCell cell3 =   row.getCell(C); 34:value3 = cell3.getStringCellValue(); 35:   HSSFCell cell4 = row.getCell(d); 36:value4 =   cell4.getStringCellValue(); 37:%&gt; 38:
  &LT;%=值1%GT;&LT;%=值2%GT;&LT;%=值3%GT;&LT;%= VALUE4%GT;   堆栈跟踪:     org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)     org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:198)     org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:450)     org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)     org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)     org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)     org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)     javax.servlet.http.HttpServlet.service(HttpServlet.java:729)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

     

注意Apache中提供了根本原因的完整堆栈跟踪   Tomcat / 8。0。26日志。

1 个答案:

答案 0 :(得分:1)

Upload+ read an excel file in a jsp using POI

您可以使用jsp从excel文件中读取此数据来检查此问题。

如果我是你,我将研究如何使用jsp处理excel表并处理它。之后,我创建了后端java对象,用于从sheet中读取数据,并将表单从jsp发送到后端java对象。