首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Apache使用ahref链接创建和下载excel文件

问使用Apache使用ahref链接创建和下载excel文件
EN

Stack Overflow用户
提问于 2017-08-27 18:30:10
回答 1查看 966关注 0票数 1

我在JSP页面上有一个excel文件列表,这些文件作为表存储在数据库中。它们是超链接。每当用户单击特定文件时,我都需要下载该文件。

我有下面的代码。

JSP页面

代码语言:javascript
复制
<html>
<head>
    <title></title>
</head>
<body>
<%
  List<String> encrypted = (List<String>)session.getAttribute("encryptedlist");
  List<String> original = (List<String>)session.getAttribute("originallist");
  List<String> decrypted = (List<String>)session.getAttribute("decryptedlist");

%>

<table>
  <tr>
    <td style="font-weight: bold">Original Files</td>
    <td style="font-weight: bold">Encrypted Files</td>
    <td style="font-weight: bold">Decrypted Files</td>
  </tr>
  <%for(int i=0;i<size;i++) {%>
  <tr>
    <td><a href="DownloadServlet1?file=<%=original.get(i)%>"><%=original.get(i)%></a></td>
    <td><a href="DownloadServlet1?file=<%=encrypted.get(i)%>"><%=encrypted.get(i)%></a></td>
    <td><a href="DownloadServlet2?file=<%=decrypted.get(i)%>"><%=decrypted.get(i)%></a></td>
  </tr>
  <%}%>
</table>
</body>
</html> 

SERVLET

代码语言:javascript
复制
public class DownloadServlet1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String file = request.getParameter("file");
        System.out.println(file);
        int columns = getCount(file);
        System.out.println(columns);
        List<String> columnNames = getColumnNames(file,columns);
        System.out.println(columnNames);
        saveFile(columnNames,columns,file,response);
        response.sendRedirect("Login.jsp");
    }

    protected int getCount(String file){
        //String sql = "select * from " + "`" + file + "`";
        int col=0;
        try {
            Connection con = DBOperations.getConnection();
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM " + file);
            System.out.println("SELECT * FROM " + file);
            ResultSetMetaData md = rs.getMetaData();
            col = md.getColumnCount();
        }catch (Exception ex)
        {
            System.out.println("getcount" + ex);
        }
        return col;
    }

    protected List<String> getColumnNames(String file, int columns)
    {
        List<String> columnNames = new ArrayList<>();
        try {
            Connection con = DBOperations.getConnection();
            Statement st = con.createStatement();
            String sql = "Select * from " + file;
            System.out.println("getcolumnnames " + sql);
            ResultSet rs = st.executeQuery(sql);
            System.out.println("SELECT * FROM " + file);
            ResultSetMetaData md = rs.getMetaData();
            for (int i = 1; i <= columns; i++){
                String col_name = md.getColumnName(i);
                columnNames.add(col_name);
                //System.out.println(col_name);
            }
        }catch (Exception ex)
        {
            System.out.println("get column names"+ex);
        }
        return columnNames;
    }
    protected void saveFile(List<String> columnNames,int columns,String file,HttpServletResponse response)
    {
        Connection con = DBOperations.getConnection();

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename="+file+".xls");
        String excelFileName = file;

        String sheetName = "Sheet1";//name o sheet

        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet(sheetName) ;

        //iterating r number of rows
        for (int r=0;r < 1; r++ )
        {
            XSSFRow row = sheet.createRow(r);

            //iterating c number of columns
            for (int c=0;c < columns; c++ )
            {
                XSSFCell cell = row.createCell(c);

                cell.setCellValue(columnNames.get(c));
            }
        }

        try
        {
            String sql = "Select * from " + file;
            System.out.println(sql);
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(sql);
            int r=1;
            while(rs.next())
            {
                XSSFRow row = sheet.createRow(r);
                for(int c=1;c<columns;c++)
                {
                    XSSFCell cell = row.createCell(c);

                    cell.setCellValue(rs.getString(c));
                }
                r=r+1;
            }

        }catch (Exception ex)
        {
            System.out.println(ex);
        }

        try {
            FileOutputStream fileOut = new FileOutputStream(excelFileName);
            //write this workbook to an Outputstream.
            wb.write(fileOut);
            fileOut.flush();
            fileOut.close();
        }catch (Exception ex)
        {
            System.out.println(ex);
        }

    }
}

getCount函数从数据库返回数据库文件的列数。getColumnNames获取所有列的名称。

这两个函数都返回正确的值,这表明访问数据库表没有问题。

saveFile函数应该下载excel文件。

我在浏览器网页上看到了这个错误

代码语言:javascript
复制
The webpage at http://localhost:8084/DownloadServlet1?file=Hil_1566869_27_08_17_20_24_18 might be temporarily down or it may have moved permanently to a new web address.

有什么问题吗?

EN

回答 1

Stack Overflow用户

发布于 2017-08-27 21:12:16

将Excel文档直接写入HTTP响应:

代码语言:javascript
复制
wb.write(response.getOutputStream());

在服务器上本地保存Excel文档是没有意义的。

另外,不要重定向到Login.jsp,这可能是导致错误的原因。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45908079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档