如何使用jsp在浏览器中查看excel文件

时间:2016-08-03 07:52:54

标签: java excel jsp

我有一个excel文件,想在jsp页面中显示相同的文件。我使用以下代码显示相同的

add_filter( 'get_custom_logo', 'wecodeart_com' );
function wecodeart_com() {
    $custom_logo_id = get_theme_mod( 'custom_logo' );
    $html = sprintf( '<a href="%1$s" class="custom-logo-link" rel="home" itemprop="url">%2$s</a>',
            esc_url( 'www.google.com' ),
            wp_get_attachment_image( $custom_logo_id, 'full', false, array(
                'class'    => 'custom-logo',
            ) )
        );
    return $html;   
} 

当我尝试单击jsp链接时,excel表会自动下载。但我希望excel表能够在jsp页面的浏览器中显示。

非常感谢上述任何帮助。

3 个答案:

答案 0 :(得分:0)

对我来说,您应该将文件流式传输到servlet的响应中,如下所示:

transfert(yourFile.getInputStream(),response.getOutputStream(), true);

public static void transfert(InputStream in , OutputStream out, boolean closeOnExit ) throws Exception{
try{byte buf[] = new byte[1024];
    int n;
    while((n=in.read(buf))!=-1){
        out.write(buf,0,n);
        out.flush();}  
   }
    catch(Exception e){
        e.getMessage();
    }

}

答案 1 :(得分:0)

可以有两种方法。

  • 如果您在使用谷歌文档或MS OneDrive等第三方服务托管Excel文件时没有任何问题,那么您可以在那里上传文件并使用这些服务生成的embedded html代码

  • 另一种方法是使用apache-poi之类的库来处理excel文件,并在jsp页面上呈现它。

答案 2 :(得分:0)

我正在使用以下代码在jsp页面中显示excel文件

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Excel Web View</title>

<%@ page import="java.io.FileInputStream" %>
<%@ page import="org.apache.poi.ss.usermodel.DataFormatter" %>
<%@ page import="org.apache.poi.xssf.usermodel.*" %>
<%@page import="org.apache.poi.hssf.usermodel.*"%>
</head>
<body>

<table border="1">

<%

 String filename="C:\\Users\\User01\\Desktop\\hello.xls"
 String extension = "";

 int i = filename.lastIndexOf('.');
 if (i >= 0) {
     extension = filename.substring(i+1);
 }

 if(extension !=null && extension.contains("xlsx"))
  {

    if(filename != null && !filename.equals("")){
        try{

            FileInputStream fs =new FileInputStream(filename);
            XSSFWorkbook  wb = new XSSFWorkbook (fs);
            for(int k = 0; k < wb.getNumberOfSheets(); k++){
            XSSFSheet sheet = wb.getSheetAt(k);
            int rows  = sheet.getPhysicalNumberOfRows();
            DataFormatter formatter = new DataFormatter();

            for(int r = 0; r < rows; r++){
            XSSFRow row   = sheet.getRow(r);

            int cells = row.getPhysicalNumberOfCells(); 
            %><tr><%
            for(int n=0;n<cells;n++)
            {
              %><td><%
           =formatter.formatCellValue(row.getCell(n))
             %></td><%
            }
           %></tr><%
            }
            }
            }
           catch(Exception e){
            System.out.println(e);
           }
           }
           }
         else {
           if(filename != null && !filename.equals("")){
           try{
                FileInputStream fs =new FileInputStream(filename);
                HSSFWorkbook  wb = new HSSFWorkbook (fs);
                for(int k = 0; k < wb.getNumberOfSheets(); k++){
                HSSFSheet sheet = wb.getSheetAt(k);
                int rows  = sheet.getPhysicalNumberOfRows();
                DataFormatter formatter = new DataFormatter();

                for(int r = 0; r < rows; r++){
                    HSSFRow row   = sheet.getRow(r);
                    int cells = row.getPhysicalNumberOfCells(); 
                %><tr><%
                    for(int n=0;n<cells;n++){
                %><td><%
                =formatter.formatCellValue(row.getCell(n))
                %></td><%
                }
                %></tr><%
                }
                }
                }
                 catch(Exception e){
                 System.out.println(e);
                 }
                 }
                 }
               %>
         </table>
         </body>
         </html>    

并在pom.xml文件中添加Dependency

<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>3.16</version>
</dependency>
相关问题