java中如何使用POI来实现导出excel功能

题记:excel的导入导出是在后面管理常常用到的功能,往往实现有两种方法,用jxl或者poi,都可以的,他俩区别不大,这里主要写Poi如何实现。
一:
1页面预览

java中如何使用POI来实现导出excel功能_第1张图片
20170106105210648.png

点击导出按钮,会下载个excel,打开

java中如何使用POI来实现导出excel功能_第2张图片
20170106105301539.png

说明:这里是将所有导出了,我也可以根据条件来导出

java中如何使用POI来实现导出excel功能_第3张图片
20170106105346914.png

点击导出按钮,会下载个excel,打开

java中如何使用POI来实现导出excel功能_第4张图片
20170106105422673.png

二:实现代码
这里我把主要代码提出来
user_list.jsp

  

user.js

//导出excel  
function toExcel(){  
    var USERNAME = $("#nav-search-input").val();  
    var lastLoginStart = $("#lastLoginStart").val();  
    var lastLoginEnd = $("#lastLoginEnd").val();  
    var ROLE_ID = $("#role_id").val();  
    window.location.href='<%=basePath%>user/excel.do?USERNAME='+USERNAME+'&lastLoginStart='+lastLoginStart+'&lastLoginEnd='+lastLoginEnd+'&ROLE_ID='+ROLE_ID;  
}  

UserController:

/* 
     * 导出用户信息到EXCEL 
     * @return 
     */  
    @RequestMapping(value="/excel")  
    public ModelAndView exportExcel(){  
        ModelAndView mv = this.getModelAndView();  
        PageData pd = new PageData();  
        pd = this.getPageData();  
        try{  
            if(Jurisdiction.buttonJurisdiction(menuUrl, "cha")){  
                //检索条件===  
                String USERNAME = pd.getString("USERNAME");  
                if(null != USERNAME && !"".equals(USERNAME)){  
                    USERNAME = USERNAME.trim();  
                    pd.put("USERNAME", USERNAME);  
                }  
                String lastLoginStart = pd.getString("lastLoginStart");  
                String lastLoginEnd = pd.getString("lastLoginEnd");  
                if(lastLoginStart != null && !"".equals(lastLoginStart)){  
                    lastLoginStart = lastLoginStart+" 00:00:00";  
                    pd.put("lastLoginStart", lastLoginStart);  
                }  
                if(lastLoginEnd != null && !"".equals(lastLoginEnd)){  
                    lastLoginEnd = lastLoginEnd+" 00:00:00";  
                    pd.put("lastLoginEnd", lastLoginEnd);  
                }   
                //检索条件===  
                  
                Map dataMap = new HashMap();  
                List titles = new ArrayList();  
                  
                titles.add("用户名");      //1  
                titles.add("编号");       //2  
                titles.add("姓名");           //3  
                titles.add("职位");           //4  
                titles.add("手机");           //5  
                titles.add("邮箱");           //6  
                titles.add("最近登录");     //7  
                titles.add("上次登录IP");   //8  
                  
                dataMap.put("titles", titles);  
                  
                List userList = userService.listAllUser(pd);  
                List varList = new ArrayList();  
                for(int i=0;i

ObjectExcelView

package com.fh.util;  
  
import java.util.Date;  
import java.util.List;  
import java.util.Map;  
  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
import org.apache.poi.hssf.usermodel.HSSFCell;  
import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
import org.apache.poi.hssf.usermodel.HSSFFont;  
import org.apache.poi.hssf.usermodel.HSSFSheet;  
import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
import org.springframework.web.servlet.view.document.AbstractExcelView;  
  
import com.fh.util.PageData;  
import com.fh.util.Tools;  
/** 
* 导入到EXCEL 
* 类名称:ObjectExcelView.java 
* @version 1.0 
 */  
public class ObjectExcelView extends AbstractExcelView{  
  
    @Override  
    protected void buildExcelDocument(Map model,  
            HSSFWorkbook workbook, HttpServletRequest request,  
            HttpServletResponse response) throws Exception {  
        // TODO Auto-generated method stub  
        Date date = new Date();  
        String filename = Tools.date2Str(date, "yyyyMMddHHmmss");  
        HSSFSheet sheet;  
        HSSFCell cell;  
        response.setContentType("application/octet-stream");  
        response.setHeader("Content-Disposition", "attachment;filename="+filename+".xls");  
        sheet = workbook.createSheet("sheet1");  
          
        List titles = (List) model.get("titles");  
        int len = titles.size();  
        HSSFCellStyle headerStyle = workbook.createCellStyle(); //标题样式  
        headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
        headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
        HSSFFont headerFont = workbook.createFont();    //标题字体  
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
        headerFont.setFontHeightInPoints((short)11);  
        headerStyle.setFont(headerFont);  
        short width = 20,height=25*20;  
        sheet.setDefaultColumnWidth(width);  
        for(int i=0; i varList = (List) model.get("varList");  
        int varCount = varList.size();  
        for(int i=0; i

maven的pom.xml如何引入poi的jar包

        
          
            poi  
            poi     
            1.0  
            system  
            ${project.basedir}/src/main/webapp/WEB-INF/lib/poi-3.11-20141221.jar  
          
          
          
            poi-examples  
            poi-examples  
            1.0  
            system  
            ${project.basedir}/src/main/webapp/WEB-INF/lib/poi-examples-3.11-20141221.jar  
          
          
          
            poi-excelant  
            poi-excelant  
            1.0  
            system  
            ${project.basedir}/src/main/webapp/WEB-INF/lib/poi-excelant-3.11-20141221.jar  
          
          
          
            poi-ooxml  
            poi-ooxml  
            1.0  
            system  
            ${project.basedir}/src/main/webapp/WEB-INF/lib/poi-ooxml-3.11-20141221.jar  
          
          
          
            poi-ooxml-schemas  
            poi-ooxml-schemas  
            1.0  
            system  
            ${project.basedir}/src/main/webapp/WEB-INF/lib/poi-ooxml-schemas-3.11-20141221.jar  
          
          
          
            poi-scratchpad  
            poi-scratchpad  
            1.0  
            system  
            ${project.basedir}/src/main/webapp/WEB-INF/lib/poi-scratchpad-3.11-20141221.jar  
          

你可能感兴趣的:(java中如何使用POI来实现导出excel功能)