Springmvc导出excel表

先导入几个poi包
poi-3.8-20120326.jar
poi-excelant-3.8-20120326.jar
poi-ooxml-3.8-20120326.jar
poi-ooxml-schemas-3.8-20120326.jar
poi-scratchpad-3.8-20120326.jar

Controller.java

//点击按钮下载表格
    @RequestMapping("/excel.do")
    public ModelAndView downExcel(HttpServletResponse response){
        String filename;
        try {
            filename = new String("excel文件名称".getBytes(),"iso8859-1");

        response.setHeader("Content-Disposition","attachment;filename="+filename+".xls");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
         Map<String,List> dataMap = new HashMap<String,List>();
        List<excelinfo> logList =excel要显示的所有数据
        dataMap.put("excelList", logList);
        ExcelView excelview=new ExcelView();
        return new ModelAndView(excelview,dataMap);

    }

ExcelViewExcelView继承AbstractExcelView,,excelinfo作为实体类存放一行数据

package com.lu.Dao;
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.HSSFRichTextString;
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.lu.entity.excelinfo;


public class ExcelView extends AbstractExcelView {


    @Override
    protected void buildExcelDocument(Map<String, Object> excelList,
            HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
         HSSFCell cell;
        List<excelinfo> loglist = (List<excelinfo>) excelList.get("excelList");//从控制器Controller中返回的业务数据
        int len=loglist.size();
        HSSFSheet sheet = workbook.createSheet("报名表"); //创建表格
        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);

        HSSFRow title = sheet.createRow(0);//定义表格的表头第一行
        String[] headname={"xx","xx","xx","xx","xx","xx","xx","xx"};
        for (int i = 0; i < headname.length; i++) { 
            cell = title.createCell(i); //定义第一行的第i列
            HSSFRichTextString text = new HSSFRichTextString(headname[i]); 
            cell.setCellValue(text); 
            //设置单元格格式为字符串类型
            cell.setCellType(HSSFCell.ENCODING_UTF_16);

        }
        HSSFRow row;

       int num=1;
       for(excelinfo excelnum:loglist){
           row= sheet.createRow(num++);  //创建表格行
           row.createCell(0).setCellValue(excelnum.getId()); 
           row.createCell(1).setCellValue(excelnum.getProduction_name()); 
           row.createCell(2).setCellValue(excelnum.getSchool()); 
           row.createCell(3).setCellValue(excelnum.getClassify()); 
           row.createCell(4).setCellValue(excelnum.getTeam()); 
           row.createCell(5).setCellValue(excelnum.getAuthor_one()); 
           row.createCell(6).setCellValue(excelnum.getAuthor_sec()); 
           row.createCell(7).setCellValue(excelnum.getAuthor_third()); 
        }
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值