SSM框架使用POI导出Excel表

//依赖导入

 
      org.apache.poi
      poi-ooxml
      3.14-beta1
    
    
      org.apache.poi
      poi-ooxml-schemas
      3.14-beta1
    
    
      org.apache.poi
      poi
      3.14-beta1
    
    
      org.apache.httpcomponents
      httpclient
      4.5.2
    

bean建立.........(对应上就好了)

public class Cost {
    private String cost_id,base_duration,base_cost,unit_cost;
    private String name,status,descr,creatime,startime,cost_type;
}

controller层

package com.chao.fee.controller;

import com.chao.fee.bean.Cost;
import com.chao.fee.service.impl.FeeServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/* I AM A PURE SHEEP
 *                   
 * WHAT YOU WANT TO DO                 
 *                 
 * FUCK AS YOU WISH                                           
 */
@Controller
@RequestMapping("/fee")
public class FeeController {
    @Resource(name = "feeImpl")
    private FeeServiceImpl feeService;

    @RequestMapping("/feeList")// 注意@ResponseBody就是写在这个位置
    public @ResponseBody String export(HttpServletResponse response){
        response.setContentType("application/binary;charset=UTF-8");
        try{
            ServletOutputStream out=response.getOutputStream();
            String fileName=new String(("CostInfo "+ new SimpleDateFormat("yyyy-MM-dd").format(new Date())).getBytes(),"UTF-8");
            response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls");
            String[] titles = { "用户编号", "用户姓名", "用户密码", "用户年龄" };
            feeService.export(titles, out);
            return "success";
        } catch(Exception e){
            e.printStackTrace();
            return "导出信息失败";
        }
    }


}

service

package com.chao.fee.service;

import com.chao.fee.bean.Cost;

import javax.servlet.ServletOutputStream;
import java.util.List;

/* I AM A PURE SHEEP
 *                   
 * WHAT YOU WANT TO DO                 
 *                 
 * FUCK AS YOU WISH                                           
 */
public interface FeeService {
    public void export(String[] titles, ServletOutputStream out);


}
package com.chao.fee.service.impl;

import com.chao.fee.bean.Cost;
import com.chao.fee.dao.FeeMapper;
import com.chao.fee.service.FeeService;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/* I AM A PURE SHEEP
 *                   
 * WHAT YOU WANT TO DO                 
 *                 
 * FUCK AS YOU WISH                                           
 */
@Service("feeImpl")
public class FeeServiceImpl implements FeeService {
    @Resource
    private FeeMapper mapper;


    @Override
    public void export(String[] titles, ServletOutputStream out) {
        try{
            // 第一步,创建一个workbook,对应一个Excel文件
            HSSFWorkbook workbook = new HSSFWorkbook();
            // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet hssfSheet = workbook.createSheet("sheet1");
            // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
            HSSFRow hssfRow = hssfSheet.createRow(0);
            // 第四步,创建单元格,并设置值表头 设置表头居中
            HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
            //居中样式
            hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

            HSSFCell hssfCell = null;
            for (int i = 0; i < titles.length; i++) {
                hssfCell = hssfRow.createCell(i);//列索引从0开始
                hssfCell.setCellValue(titles[i]);//列名1
                hssfCell.setCellStyle(hssfCellStyle);//列居中显示
            }

            // 第五步,写入实体数据
            List costs = mapper.query();

            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            if(costs != null && !costs.isEmpty()){
                for (int i = 0; i < costs.size(); i++) {
                    hssfRow = hssfSheet.createRow(i+1);
                    Cost cost = costs.get(i);

                    // 第六步,创建单元格,并设置值
                    int id = 0;
                    if(Integer.parseInt(cost.getCost_id()) != 0){
                        id = Integer.parseInt(cost.getCost_id());
                    }
                    hssfRow.createCell(0).setCellValue(id);
                    String username = "";
                    if(cost.getName() != null){
                        username = cost.getName();
                    }
                    hssfRow.createCell(1).setCellValue(username);
                    String password = "";
                    if(cost.getStatus() != null){
                        password = cost.getStatus();
                    }
                    hssfRow.createCell(2).setCellValue(password);
                    int age = 0;
                    if(Integer.parseInt(cost.getBase_cost()) != 0){
                        age = Integer.parseInt(cost.getBase_cost());
                    }
                    hssfRow.createCell(3).setCellValue(age);
                }
            }

            // 第七步,将文件输出到客户端浏览器
            try {
                workbook.write(out);
                out.flush();
                out.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }catch(Exception e){
            e.printStackTrace();
            try {
                throw new Exception("导出信息失败!");
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
}

dao层

@Repository
public interface FeeMapper {
    List query();

}

    

jsp


Hello World!

点击

效果:实验效果,内容不必在意

SSM框架使用POI导出Excel表_第1张图片

你可能感兴趣的:(SSM框架使用POI导出Excel表)