导出Excel文件

导入 pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.xingxue.poi</groupId>
  <artifactId>poi-demo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.2.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <path>/</path>
                <port>80</port>
            </configuration>
        </plugin>
    </plugins>
    </build>


</project>

页面 poidemo.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/4/28
  Time: 9:37
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="scripts/jquery-3.2.1.js"></script>
    <script>
        $(function () {
            $("#but").click(function () {
                // 返回的是 原生的 Dom 对象
                var dowloadExcleIdObj = $("#dowloadExcleId").get(0);
                var url = "getPoi";
                dowloadExcleIdObj.src = url;
                return false;
            });
        });

    </script>
</head>
<body>
<button id="but">excle导出</button>
<%--用 display 属性隐藏标签不会影响整个html文档的结构 没有什么改变,如果 visibility 属性来隐藏标签就会影响整个html 结构  内容全部掉下来--%>
<iframe id="dowloadExcleId"  style="display: none"></iframe>
</body>
</html>

在webapp下面导入jQuery

E:\excelfinal\src\main\webapp\scripts\jquery-3.2.1.js  自己下载

三。写model  这里没有连接数据库  如果连接数据库了,要和数据库一致

package com.xingxue.poi.model;

public class UserModel {
    private String logname;
    private String password;

    public String getLogname() {
        return logname;
    }

    public void setLogname(String logname) {
        this.logname = logname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

写接口

package com.xingxue.poi.web.controller;

import com.xingxue.poi.model.UserModel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.event.DocumentEvent;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(value = "/getPoi")
public class PoiController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("请求来了...");

        List<UserModel> users = new ArrayList<UserModel>();

        for (int i = 1; i <= 10; i++) {
            UserModel u = new UserModel();
            u.setLogname("test" + i);
            u.setPassword("88888");

            users.add(u);

        }

        //==============================================================
        doExcel(users,resp);
    }

    private void doExcel(List<UserModel> users,HttpServletResponse response) throws IOException {

        //HSSFWorkbook 处理的 .xls 文件  
        Workbook wb = new HSSFWorkbook();

        //wb 对象的操作的 工具类型对象 CreationHelper
        CreationHelper createHelper = wb.getCreationHelper();

        //创建工作表 Sheet 对象   创建表名“用户信息统计”
        Sheet s = wb.createSheet("用户信息统计");

        //声明表头  占一行  行从下标 0 开始
        Row headRow = s.createRow(0);
        //声明单元格  第一个单元格 下标从 0 开始
        Cell cellUserName = headRow.createCell(0);
        Cell cellPassword = headRow.createCell(1);

        cellUserName.setCellValue("用户名");//将第一个单元格设置成用户名
        cellPassword.setCellValue("密码");//将第二个单元格设置成为密码

        int index = 1;//将user遍历循环出来
        for (UserModel u : users  ) {
            Row r = s.createRow(index);
            r.createCell(0).setCellValue(u.getLogname());
            r.createCell(1).setCellValue(u.getPassword());
            index++;
        }


        // 保存文件的名字
        String filename = "用户信息表.xls";
        //防止中文乱码
        filename =  new String(filename.getBytes(),"iso-8859-1");

        //创建一个 用户信息表.xls 文件,并把输出流管道插入到 用户信息表.xls 这个文件准备向外进行输出
        //FileOutputStream out = new FileOutputStream(filename);
        response.setHeader("content-disposition", "attachment;filename="+filename);
        ServletOutputStream out = response.getOutputStream();
        wb.write(out);
        out.close();
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值