• Excel导出(适合初学者)


    这里有两个版本`,第一个是我自己写的一个导入导出,另一个是不知道复制哪位大佬的,这已经不重要了,重点是自己看懂会用就可以

    Excel导出步骤:生成Excel文档--------新建Sheet--------新建Row-------新建Cell单元格--------写单元格数据

    版本一:

    这个适合刚开始学习Excel导出,代码简洁易懂

    package com.jttxtech.www.controller;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.servlet.ModelAndView;
    import com.jttxtech.www.entity.ProductRecordBean;
    import com.jttxtech.www.service.ProductRecordService;
    import com.jttxtech.www.util.ExportExcelUtil;
    import com.jttxtech.www.util.FileUtils;

    @RequestMapping("/api/find")
    @ResponseBody
    public String Export2(HttpServletRequest request,HttpServletResponse response) throws Exception{
    List<ProductRecordBean> dataList = productRecordService.findAll();
    //生成Excel文件
    HSSFWorkbook hssfWorkbook=new HSSFWorkbook();
    HSSFSheet sheet=hssfWorkbook.createSheet("项目记录");
    //表头
    HSSFRow headRow=sheet.createRow(0);
    headRow.createCell(0).setCellValue("数据");
    headRow.createCell(1).setCellValue("提交时间");
    headRow.createCell(2).setCellValue("更新时间");
    headRow.createCell(3).setCellValue("提交人");
    headRow.createCell(4).setCellValue("项目编码");
    //表数据
    for(ProductRecordBean date :dataList) {
    HSSFRow dataRow=sheet.createRow(sheet.getLastRowNum()+1);
    dataRow.createCell(0).setCellValue(date.getRecord_data());
    dataRow.createCell(1).setCellValue(date.getSubmit_time());
    dataRow.createCell(2).setCellValue(date.getUpdate_time());
    dataRow.createCell(3).setCellValue(date.getInfo_id());
    dataRow.createCell(4).setCellValue(date.getProject_code());

    }


    //下载导出
    //设置头信息

    response.setContentType("application/vnd.ms-excel");
    String filename="项目记录表.xls";
    String agent = request.getHeader("user-agent");
    filename=FileUtils.encodeDownloadFilename(agent, filename);
    response.setHeader("Content-disposition", "attachment; filename="+filename);//文件名这里可以改
    OutputStream output=response.getOutputStream();
    hssfWorkbook.write(output);
    hssfWorkbook.close();


    return null;

    }

    这是上面代码需要用到的工具类:

    package com.jttxtech.www.util;

    import java.io.IOException;

    import java.net.URLEncoder;

    import sun.misc.BASE64Encoder;
    public class FileUtils {

    public static String encodeDownloadFilename(String agent, String filename) throws IOException {
    if (agent.contains("MSIE")) {
    // IE浏览器
    filename = URLEncoder.encode(filename, "utf-8");
    filename = filename.replace("+", " ");
    } else if (agent.contains("Firefox")) {
    // 火狐浏览器
    BASE64Encoder base64Encoder = new BASE64Encoder();
    filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
    } else {
    // 其它浏览器
    filename = URLEncoder.encode(filename, "utf-8");
    }
    return filename;
    }
    }

  • 相关阅读:
    [剑指offer] 7. 斐波那契数列
    [剑指offer] 6. 旋转数组的最小数字
    [剑指offer] 5. 用两个栈实现队列
    [剑指offer] 4. 重建二叉树
    [剑指offer] 3. 从头到尾打印链表
    vue.js从输入中的contenteditable元素获取innerhtml
    CSS3 ------- object-fit属性
    mouseenter和mouseover区别
    元素scroll系列属性
    淘宝flexible.js源码分析
  • 原文地址:https://www.cnblogs.com/zhangxiaozhen/p/10473146.html
Copyright © 2020-2023  润新知