poi:Excel

poi:Excel
package com.hc.controller;

import com.hc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;


@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("run")
    public void export(HttpServletResponse response){
        //获取数据
        ExcelUtil.ExcelPoI(userService.allUser(),response);
    }
}
package com.hc.controller;

import com.hc.model.User;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;


public class ExcelUtil {


    public static void ExcelPoI(List<User> values, HttpServletResponse response) {
        //sheet名
        String sheetName = "信息表";
        //excel标题
        String[] title = {"编号", "名称", "日期", "说明"};
        //excel文件名
        String fileName = System.currentTimeMillis() + ".xls";
        //创建HSSFWorkbook
        HSSFWorkbook wb = getHSSFWorkbook(sheetName,title, values, null);
        //响应到客户端
        try {
            fileName = new String(fileName.getBytes(), "UTF-8");
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");

            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static HSSFWorkbook getHSSFWorkbook(String sheetName,String[] title, List<User> values, HSSFWorkbook wb) {

        // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
        if (wb == null) {
            wb = new HSSFWorkbook();
        }

        // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);

        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        HSSFRow row = sheet.createRow(0);

        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        //声明列对象
        HSSFCell cell = null;

        //创建标题
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }

        //创建内容
        int i = 0;
        for (User user : values) {
            row = sheet.createRow(i + 1);
            for (int j = 0; j < title.length; j++) {
                //将内容按顺序赋给对应的列对象
                if (j == 0) {
                    row.createCell(j).setCellValue(user.getUserId());
                } else if (j == 1) {
                    row.createCell(j).setCellValue(user.getUserName());
                } else if (j == 2) {
                    row.createCell(j).setCellValue(user.getUserDay());
                } else if (j == 3) {
                    row.createCell(j).setCellValue(user.getUserBase());
                }
            }
            i++;
        }
        return wb;
    }
}