How POI export excel table()

The purpose of this time is to export the existing data table in excel format to the locally
used technology (front-end: easyui+jQuery; back-end s2sh)

//前台代码(这里用的toolbar工具条)
{
    id : 'button-export',
        text : '导出',
        iconCls : 'icon-undo',
        //点击事件
        handler : doExport
}

//写点击事件的函数
//导出所有区域(同步请求)
function doExport(){                                         location.href="../../area_exportData.action";
}

Background code:

AreaAction

//导出所有区域的数据
    @Action("area_exportData")
    public String exportData() throws Exception{
        /**
         * 两大步:
         *  第一步:从数据库查询数据,写成excel
         *  使用poi技术写excel
         *  编程技巧,现实中怎么写,这里就怎么写
         *  创建一个空的工作簿
         *  97格式
         */
        //创建空的工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //在工作薄中创建新的工作表(迭代器,空)
        HSSFSheet sheet = workbook.createSheet();
        //在工作表中写行(行中有格)
        //先写行头信息(第一行是头)
        HSSFRow headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("区域编号");
        headerRow.createCell(1).setCellValue("省份");
        headerRow.createCell(2).setCellValue("城市");
        headerRow.createCell(3).setCellValue("区域");
        headerRow.createCell(4).setCellValue("邮编");

        //查询数据列表
        List<Area> areaList = areaService.findAreaList();

        if(areaList != null && !areaList.isEmpty()){
            //写内容
            for (Area area : areaList) {
                //创建一行:在有内容的行的下一行
                HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
                //创建一格一格,写入数据
                dataRow.createCell(0).setCellValue(area.getId());
                dataRow.createCell(1).setCellValue(area.getProvince());
                dataRow.createCell(2).setCellValue(area.getCity());
                dataRow.createCell(3).setCellValue(area.getDistrict());
                dataRow.createCell(4).setCellValue(area.getPostcode());
                //值最好都是字符串(String),不是的话手动转换
            }
        }

        //第二步:设置下载两个的参数,将Excel写入响应即可
        //响应对象
        HttpServletResponse response = ServletActionContext.getResponse();
        //设置客户端浏览器用于识别附件的两个参数Content-Type和Content-DisPosition
        //文件名随便写
        String downFileName = "区域数据.xls";
        //获取文件的MIME类型
        String contentType = ServletActionContext.getServletContext().getMimeType(downFileName);
        //将MIME类型放入响应
        response.setContentType(contentType);
        //浏览器类型
        String agent = ServletActionContext.getRequest().getHeader("user-agent");
        //附件名编码,解决中文乱码问题(这里自己写的工具类)
        downFileName = FileUtils.encodeDownloadFilename(downFileName, agent);
        //获取附件的名字和下载方式
        String contentDisposition="attachment;filename="+downFileName;
        //将附件名字和下载方式放入响应头信息中
        response.setHeader("Content-Disposition", contentDisposition);
        //将excel文件流写入响应
        workbook.write(response.getOutputStream());
        return NONE;//响应为空格
    }

The FileUtils tool class is written by myself and posted here

package cn.itcast.utils;
import java.io.IOException;
import java.net.URLEncoder;
import sun.misc.BASE64Encoder;
@SuppressWarnings("all")
public class FileUtils {
        /**
         * 下载文件时,针对不同浏览器,进行附件名的编码
         * 
         * @param filename
         *            下载文件名
         * @param agent
         *            客户端浏览器
         * @return 编码后的下载附件名
         * @throws IOException
         */
        public static String encodeDownloadFilename(String filename, String agent)
                throws IOException {
            if (agent.contains("Firefox")) { // 火狐浏览器
                filename = "=?UTF-8?B?"
                        + new BASE64Encoder().encode(filename.getBytes("utf-8"))
                        + "?=";
                filename = filename.replaceAll("\r\n", "");
            } else { // IE及其他浏览器
                filename = URLEncoder.encode(filename, "utf-8");
                filename = filename.replace("+"," ");
            }
            return filename;
        }
}

Service layer interface
AreaService

/**
 * 说明:查询所有区域列表
 */
List<Area> findAreaList();

Service layer implementation class code
AreaServiceImpl

@Override
    public List<Area> findAreaList() {
        return areaRepository.findAll();
    }

DAO layer code (AreaRepository extends JpaRepository

@Override
    public List<Area> findAreaList() {
        return areaRepository.findAll();
    }

If you want to provide a data template for customers to fill in, you only need to find out the header, download it and fill in the data yourself, and then upload the data through one-click upload after the data is written
; If you press the key to upload, the version of the data table and the content of the excel data should be in text format (select the data in excel, right-click to set the cell properties, select the text in the number column and save it before uploading)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325652020&siteId=291194637