poi封装及导入下载服务资源[根据类名称资源下载方式]

这里直接贴代码了

导入Excel

前端上传

<from enctype="multipart/from-data" method="post" action="/import">

<input type="file" name="file"/>

<input type="submit" value="上传"/>

</from>

上传文件的参数名为file

后端接收

@RequestMapping("/import") public void importexcle(@ExcelRequestBody(titleRows = 2, headRows = 1, requireClass = City.class) List<City>vo) throws Exception {     for (int i = 0; i < vo.size(); i++) {         System.out.println(vo.get(i));

//做相应的数据处理

    } }

在SpringMVC的方法内使用@ExcelRequestBody注解,titleRow为标题的行数,headRows为标题栏的行数,通常为1。requireClass为 Excel类数据需要转换成的对象。

看一下对象的示例:

@ExcelTarget("ItsmSystemUserBean")
@Table(name = "itsm_sys_user")
public class ItsmSystemUserBean {
    @Id
    @GeneratedValue(generator = "JDBC")
    private String id;

    /**
     * 姓名
     */
    @NotEmpty
    @Excel(name = "姓名 *", orderNum = "2", width = 20,needMerge = false)
    private String userName;

    /**
     * 登录id
     */
    @NotEmpty
    @Excel(name = "登录id *", orderNum = "3", width = 18,needMerge = false)
    private String loginName;


导入导出Excel的对象上使用@ExcelTarget注解,并指定一个唯一的id。
在需要进行处理的字段上使用@Excel注解,name为Excel表格的head,orderNum为字段的顺序,不可重复!
needMerge用于合并,通常不需要使用。更详细的用法可参考:http://www.afterturn.cn/doc/easypoi.html
3、导出Excel
在需要导出Excel的方法上添加@ExportExcel(fileName = "测试")注解。
fileName用于指定下载的文件名称。excelType可用于指定Excel版本,默认为03格式。
示例:

ExportParams的title为标题,secondTitle为二级标题,sheetName为sheet的名称。
使用ExcelExportUtil.exportExcel方法导出数据到指定对象,最后写入response。此行代码后不可以有任何代码!导出方法的返回值为void
/**
 * 导出Excel,这里没有使用向模版填充数据
 * <p>
 * <br>【请求地址】/itsm/system/sso/user/exportExcelUser
 *
 * @param userIds 用户id 字符串
 * @throws IOException
 */
@RequestMapping("/exportExcelUser")
@ExportExcel(fileName = "人员管理基本信息")
public void exportExcelUser(HttpServletResponse response, @RequestParam(name = "userIds") List<String> userIds) throws IOException {
    logger.info(this.getClass().getName());
    List<ItsmSystemUserBean> userBeanList = this.userService.queryUserBeanById(userIds);
    ExportParams params = new ExportParams("人员管理基本信息", "*为必填", "人员基本信息");
    ExcelExportUtil.exportExcel(params, ItsmSystemUserBean.class, userBeanList).write(response.getOutputStream());
}

=========================第二篇=============================
下载服务资源
1.controlle方法体
/**
 * 下载导入模版
 * <br>【请求地址】/itsm/system/sso/user/downloadExcelUser
 *
 * @param
 * @throws IOException
 */
@RequestMapping("/downloadExcelUser")
public void downloadExcelUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
    //multipart/form-data  设置文件ContentType类型,这样设置,会自动判断下载文件类型
    ResponseUtill.download(request, response, "人员管理基本信息.xls", "multipart/form-data");
}
2.引用工具类
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

/**
     * 文件下载.
     *
     * @param request
     * @param response
     * @param name 文件名.
     * @param contentType 文件类型.
     */
    public static void download(HttpServletRequest request,
                                HttpServletResponse response,
                                String name,
                                String contentType) throws Exception {
        // 最终返回给浏览器的文件名.
        String finalFileName = null;

        final String userAgent = request.getHeader("USER-AGENT");
        // IE浏览器.
        if(StringUtils.contains(userAgent, "MSIE")) {
            finalFileName = URLEncoder.encode(name,"UTF8");
        }
        // Google,火狐浏览器
        else if(StringUtils.contains(userAgent, "Mozilla")) {
            finalFileName = new String(name.getBytes(), "ISO8859-1");
        }
        // 其他浏览器.
        else{
            finalFileName = URLEncoder.encode(name,"UTF8");
        }

        // 告诉浏览器用什么软件可以打开此文件.
        response.setHeader("content-Type", contentType);
        // 下载文件的默认名称.
        response.setHeader("Content-Disposition", "attachment;filename=" + finalFileName);
        // 编码.
        response.setCharacterEncoding("UTF-8");

        Resource resource = new ClassPathResource(name);
        IOUtils.copy(resource.getInputStream(), response.getOutputStream());
        response.flushBuffer();
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值