繁体   English   中英

在JSP页面中显示Excel数据

[英]Display excel data in a jsp page

我在java / jsp中有一个Web应用程序。 在我们的应用程序中,有多个选项卡,并且在其中一个选项卡中,我必须显示Excel工作表中的数据。 我试图通过使用“另存为网页”选项将Excel工作表转换为data.html文件。 但是当我尝试在选项卡中包括html文件时,我收到以下错误消息

This content cannot be displayed in a frame

我也尝试通过Eclipse中的tomcat服务器运行html文件,但得到了以上消息。 当我尝试通过网络浏览器打开它时,数据会正确显示。

我需要帮助以获取在jsp页面中显示的excel数据。 任何帮助对此表示感谢。 谢谢

当Excel工作表位于服务器端(Tomcat,Glassfish,Wildfly或每个其他servlet容器)时,您可以使用POI部署一些行代码,以读取excel文件。

https://poi.apache.org/spreadsheet/quick-guide.html#FileInputStream

然后,您可以创建一个动态JSP ...,以将结果显示在HTML表中。

请检出http://w3lessons.info/2015/07/13/export-html-table-to-excel-csv-json-pdf-png-using-jquery/ 可能会有帮助,如果没有,请检查apache-poi。

我将在此处附加图像,显示标记为已读框的列

您可以使用以下代码将数据从servlet传递到jsp,它可以正常工作Modules.java(Servlet页面)和Modules.jsp页面代码如下所示

FileInputStream file1 = new FileInputStream(new File("your_file_path"));
            System.out.println(file1);
            file.close();    
            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook111 = new XSSFWorkbook(file1);

            //Get first/desired sheet from the workbook
            XSSFSheet sheet111 = workbook111.getSheetAt(0);


            Object[][] bookData_read = new String[sheet111.getLastRowNum()+1][3];

            int row_count11 = 0;

            Iterator<Row> rowIterator111 = sheet111.iterator();

            while (rowIterator111.hasNext()) 
            {
                Row row111 = rowIterator111.next();
                if (row_count11 == 0) {
                    row_count11++;
                continue;

                }

                if (row_count11 > sheet111.getLastRowNum())
                    break;



                Cell cell11 = row111.getCell(4);
                String cellvalue1 = "";

                if (cell11 != null && ! "".equals(cell11.getStringCellValue()) ||cell11 != null && ! "".equals(cell11.getNumericCellValue())) {

                    switch (cell11.getCellType()) {

                        case Cell.CELL_TYPE_STRING:
                            //System.out.print(cell.getStringCellValue());
                            cellvalue1 = cell11.getStringCellValue();
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            //System.out.print(cell.getBooleanCellValue());
                            cellvalue1 = "" + cell11.getBooleanCellValue();
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            //System.out.print(cell.getNumericCellValue());
                            cellvalue1 = "" + cell11.getNumericCellValue();
                            break;

                    }}


                    String module1 = cellvalue1;
                    //System.out.println("module"+module1);
                    //System.out.println(cellvalue);
                    bookData_read[row_count11][0] = cellvalue1;


                    if(row111.getCell(5)!=null)
                    {
                          Cell cell2 = row111.getCell(5);
                          cellvalue1 = "";
                        if (cell2 != null && ! "".equals(cell2.getStringCellValue())) {



                    switch (cell2.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            //System.out.print(cell.getStringCellValue());
                            cellvalue1 = cell2.getStringCellValue();
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            //System.out.print(cell.getBooleanCellValue());
                            cellvalue1 = "" + cell2.getBooleanCellValue();
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            //System.out.print(cell.getNumericCellValue());
                            cellvalue1 = "" + cell2.getNumericCellValue();
                            break;
                    }  }}
                    String submodule = cellvalue1;
                    //System.out.println("submodule"+submodule);

                   // System.out.println(cellvalue);
                    bookData_read[row_count11][1] = cellvalue1;



                   if(row111.getCell(5)!=null && ! "".equals(row111.getCell(5).getStringCellValue()) )
                   {
    //               if(row.getCell(7)!=null && ! "".equals(row.getCell(7).getStringCellValue()))
    //              {
    //                    
                    Cell  cell3 = row111.getCell(8);
                    cellvalue1 = "";
                    if (cell3 != null && ! "".equals(cell3.getStringCellValue())) {
                    switch (cell3.getCellType()) {
                        case Cell.CELL_TYPE_STRING:

                            cellvalue1 = cell3.getStringCellValue();
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:

                            cellvalue1 = "" + cell3.getBooleanCellValue();
                            break;
                        case Cell.CELL_TYPE_NUMERIC:

                            cellvalue1 = "" + cell3.getNumericCellValue();
                            break;}}


                   String  temp = cellvalue1;
                   bookData_read[row_count11][2] = cellvalue1;

                   }
                    else{

                       continue;
                    }


                   row_count11++;



            }
            workbook111.close();

    request.setAttribute("modulesList", bookData_read);
 RequestDispatcher rd = request.getRequestDispatcher("/All_Modules.jsp");
        rd.forward(request, response);  

Modules.jsp页面

<pre>  <%
    String[][] book_data = (String[][]) request.getAttribute("modulesList");
    System.out.println("book_data: " + book_data);
    %>
  <table class="table table-striped jambo_table bulk_action">
                        <thead>
                          <tr class="headings">

                            <th class="column-title"><center>Sr No. </center></th>
                            <th class="column-title"><center>Modules</center></th>
                            <th class="column-title"><center>Sub Modules </center></th>
                                  <th class="column-title"><center>Test Case ID</center></th>

                    <th class="column-title"><center>Select All&nbsp;&nbsp;<input type="checkbox" class="chkSelectAll btn btn-primary" /></center></th>
                         <th class="column-title"><center>Screen Shot&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</center></th>


                        </thead>
  <%
  int counter11 = 1;
  for (String[] device : book_data) {
      if (device[0] != null && device[2] != null) {
          System.out.println("device[0]: " + device[0]);
  %>
    <tr>
        <td><center><%=counter11 %></center></td>
        <td><center><%=device[0] %></center></td>
        <td><center><%=device[1] %></center></td>
        <td><center><%=device[2] %></center></td>

        <td><center><input type="checkbox" id="check-all" class="flat" name="selectedmodules" value=<%=device[2]%>></center></td>
        <td><center><input type="checkbox" id="check-all" class="flat" name="screenshots" value=<%=device[2]%>></center></td>

    </tr>

  <%
    counter11++;
      }
  }
  %>
                       </table>
    </pre>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM