zoukankan      html  css  js  c++  java
  • springmvc 导出excel

    springmvc 导出excel

    http://blog.csdn.net/wlsyn/article/details/10195193

    package com.tuling.view;
    import java.io.OutputStream;
    import java.util.Map;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.springframework.web.servlet.view.document.AbstractExcelView;
    public class ViewExcel extends AbstractExcelView {
        @Override
        public void buildExcelDocument(Map<String, Object> obj, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
            String filename = "message.xls";// 设置下载时客户端Excel的名称
            // filename = StringUtils.encodeFilename(filename, request);//处理中文文件名
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + filename);
            OutputStream ouputStream = response.getOutputStream();
            workbook.write(ouputStream);
            ouputStream.flush();
            ouputStream.close();
        }
    }

    controller

    @RequestMapping(value = "export")
        public ModelAndView exportExcel(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
            ViewExcel viewExcel = new ViewExcel();
            Map<String, Object> obj = null;
            // 获取数据库表生成的workbook
            HSSFWorkbook workbook = new HSSFWorkbook();
            List<Message> list = messageMapper.selectAll();
            // excel文件名
            try {
                HSSFSheet sheet = workbook.createSheet("消息反馈");
                // workbook.setSheetName(0,sheetName,HSSFWorkbook..ENCODING_UTF_16);
                HSSFRow row = sheet.createRow((short) 0);
                HSSFCell cell = null;
                // 写入各个字段的名称
                cell = row.createCell(0);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue("名称");
                cell = row.createCell(1);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue("公司");
                int iRow = 1;
                for (Message message : list) {
                    // 写入各条记录,每条记录对应Excel中的一行
                    row = sheet.createRow((short) iRow);
                    cell = row.createCell(0);
                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                    cell.setCellValue(message.getName());
                    cell = row.createCell(1);
                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                    cell.setCellValue(message.getCompany());
                    iRow++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                viewExcel.buildExcelDocument(obj, workbook, request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new ModelAndView(viewExcel, model);
        }

     处理一下中文

            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
            String filename = format.format(new Date())+"消息反馈.xls";// 设置下载时客户端Excel的名称
            // filename = StringUtils.encodeFilename(filename, request);//处理中文文件名
            filename = URLEncoder.encode(filename, "UTF-8"); 

     设置表格的列宽

    sheet.setColumnWidth(2, 10000);
  • 相关阅读:
    Python解决编码问题: `UnicodeEncodeError: 'gbk' codec can't encode character 'xa0' in position 10
    python中关于windows文件名非法字符的过滤处理
    IDEA 卡住半天,buid(编译)不动——解决办法(适用于maven和gradle)及定位全过程
    Linux 系统常见压缩文件(.deb、.rpm等)解压记录
    Linux Redis 安装异常处理
    从hdfs导入数据到hive表
    Python:Rocketmq消息队列使用
    Linux的nohup命令使用 —— 在服务器后台一直执行程序
    kafka:安装和命令行使用
    kafka报错:kafka.errors.NoBrokers Available,Close of session 0x100457e83740000 java.io.IOException 和 The broker is trying to join the wrong cluster
  • 原文地址:https://www.cnblogs.com/stono/p/6580302.html
Copyright © 2011-2022 走看看