springmvc生成excel表

一、Jar包依赖,Maven依赖 引入POI
spring依赖POI或jExcel来实现对excel输出的支持

<dependency>
     <groupId>org.apache.poi</groupId>
     <artifactId>poi</artifactId>
     <version>3.11</version>
</dependency>

二、自定义视图解析器

HelloView.java

package com.view;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.pojo.User;
public class HelloView extends AbstractExcelView {

    @Override
    protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        // TODO Auto-generated method stub
         String fileName = "用户列表excel.xls";  
            response.setCharacterEncoding("UTF-8");  
            response.setContentType("application/ms-excel");  
            response.setHeader("Content-Disposition", "inline; filename="+new String(fileName.getBytes(),"iso8859-1"));  
            OutputStream outputStream = response.getOutputStream();

            List<User> userList = (List<User>) model.get("userList");
            // 产生Excel表头
            HSSFSheet sheet = workbook.createSheet("用户信息");
            HSSFRow header = sheet.createRow(0);
            // 产生标题列
            header.createCell(0).setCellValue("姓名");
            header.createCell(1).setCellValue("年龄");

            header.createCell(2).setCellValue("身高");
            header.createCell(3).setCellValue("性别");
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("mm/dd/yyyy"));
            int rowNumber = 1;
            for (User user : userList) {
                HSSFRow row = sheet.createRow(rowNumber++);
                // 产生标题列
                row.createCell(0).setCellValue(user.getName());
                row.createCell(1).setCellValue(user.getAge());
                row.createCell(2).setCellValue(user.getHeight());
                row.createCell(3).setCellValue(user.getSex());
            }
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
    }


}

User.java

package com.pojo;
public class User {
    private int  age;
    private String name;
    private int height;
    private String sex;
    public User() {
        super();
    }
    public User(int age, String name, int height, String sex) {
        super();
        this.age = age;
        this.name = name;
        this.height = height;
        this.sex = sex;
    }
    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getHeight() {
        return height;
    }
    public void setHeight(int height) {
        this.height = height;
    }
}

三、视图映射配置
在视图解析器配置文件中加入

 <!-- 通过order属性来定义视图解析器的优先级,order的值越小优先级越高 -->
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
        <property name="order" value="0"></property>
    </bean>

四、控制层转发视图

package com.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.pojo.User;
import com.view.HelloView;

@Controller
public class Test {
     @RequestMapping(value = "/helloView")
        public ModelAndView helloView() {
            Map<String, Object> model = new HashMap<>();
            model.put("userList", getUsers());
            return new ModelAndView(new HelloView(), model);
        }
        private List<User> getUsers() {
            Random random=new Random();
            String[] sex={"男","女"};
            List<User> userList = new ArrayList<>();
            for(int i=1;i<101;i++){
                int d=random.nextInt(2);
                 com.pojo.User user = new User(i, "ys"+i, 170, sex[d]);
            userList.add(user);
            }

            return userList;
        }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值