关于Excel表格导出方法--application/vnd.ms-excel

    由于本人所做的项目中需要用到两种将JSP页面table导出到Excel表格的方法(老板也是坑爹),一种是在后台操作数据库来实现,比较简单。由于本人对JSP页面导出方法不了解,所以在此做个笔记,也希望对各位有帮助,下面将完整JSP代码贴出来:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Export to Excel - Demo</title>
</head>
<body>
    <%
        String exportToExcel = request.getParameter("exportToExcel");
        if (exportToExcel != null
                && exportToExcel.toString().equalsIgnoreCase("YES")) {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "inline; filename="
                    + "excel.xls");
 
        }
    %>
    <table align="left" border="2">
        <thead>
            <tr bgcolor="lightgreen">
                <th>Sr. No.</th>
                <th>Text Data</th>
                <th>Number Data</th>
            </tr>
        </thead>
        <tbody>
            <%
                for (int i = 0; i < 10; i++) {
            %>
            <tr bgcolor="lightblue">
                <td align="center"><%=i + 1%></td>
                <td align="center">This is text data <%=i%></td>
                <td align="center"><%=i * i%></td>
            </tr>
            <%
                }
            %>
        </tbody>
    </table>
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
            
    <%
        if (exportToExcel == null) {
    %>
    <a href="text.jsp?exportToExcel=YES">Export to Excel</a><!--注意这里的href链接页面就是你当前JSP页面的名字-->
    <%
        }
    %>
</body>
</html>

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现文档流导出Excel表格方法有很多,这里介绍一种比较简单的方法: 1. 引入Apache POI库,这是一个Java处理Microsoft Office文档的开源库,包括Excel、Word和PowerPoint等。 2. 创建工作簿对象Workbook和工作表对象Sheet。 3. 在工作表中创建行对象Row和单元格对象Cell。 4. 将数据写入单元格中。 5. 将工作簿写入输出流中,输出到客户端浏览器。 示例代码如下: ```java // 创建工作簿对象 Workbook workbook = new XSSFWorkbook(); // 创建工作表对象 Sheet sheet = workbook.createSheet("Sheet1"); // 创建行对象 Row row = sheet.createRow(0); // 创建单元格对象,并设置值 Cell cell = row.createCell(0); cell.setCellValue("姓名"); cell = row.createCell(1); cell.setCellValue("年龄"); // 写入数据 for(int i=1; i<=10; i++){ row = sheet.createRow(i); cell = row.createCell(0); cell.setCellValue("张三" + i); cell = row.createCell(1); cell.setCellValue(i); } // 输出到客户端浏览器 response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=test.xlsx"); OutputStream ouputStream = response.getOutputStream(); workbook.write(ouputStream); ouputStream.flush(); ouputStream.close(); ``` 以上代码中,response是HttpServletResponse对象,用于向客户端浏览器输出Excel文件。在输出前需要设置Content-type为application/vnd.ms-excel,Content-disposition为attachment,并指定文件名。最后将Workbook写入输出流中即可。 注意:以上代码仅供参考,具体实现方式需要根据实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dengrz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值