腐乳麻酱韭菜花 2017-02-22 11:44 采纳率: 0%
浏览 2064

导出execl文件弹不出下载框

package com.dogoserver.tools;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.dogoserver.user.beans.UserAble;

public class UserExeclUtil {

public static void createUserAbleExecl(List<UserAble> listUserAble,HttpServletResponse response) throws IOException{

    // 创建excel工作簿
    Workbook wb = new HSSFWorkbook();
    // 创建第一个sheet(页)
    Sheet sheet = (Sheet) wb.createSheet();
    //创建第行
    Row row = sheet.createRow((short) 0);
    Cell serialNo = row.createCell(0);
    serialNo.setCellValue("序号");
    Cell userName = row.createCell(1);
    userName.setCellValue("用户姓名");
    Cell SCP = row.createCell(2);
    SCP.setCellValue("送检人");
    Cell focus = row.createCell(3);
    focus.setCellValue("专注能力");
    Cell selfControl = row.createCell(4);
    selfControl.setCellValue("自控能力");
    Cell language = row.createCell(5);
    language.setCellValue("语言能力");
    Cell music = row.createCell(6);
    music.setCellValue("音乐辨识能力");
    Cell memory = row.createCell(7);
    memory.setCellValue("情景记忆能力");
    Cell eq = row.createCell(8);
    eq.setCellValue("情绪控制能力");
    Cell learn = row.createCell(9);
    learn.setCellValue("学习能力");
    Cell create = row.createCell(10);
    create.setCellValue("创造能力");
    Cell math = row.createCell(11);
    math.setCellValue("数学能力");
    Cell sport = row.createCell(12);
    sport.setCellValue("运动能力");
    Cell wit = row.createCell(13);
    wit.setCellValue("智力");

    int rowNo = 1;
    for(UserAble userAble: listUserAble){
        Row rowInfo = sheet.createRow(rowNo);
        Cell serialNoInfo = row.createCell(0);
        serialNoInfo.setCellValue(rowNo);
        Cell userNameInfo = rowInfo.createCell(1);
        userNameInfo.setCellValue(userAble.getU_name());
        Cell SCPInfo = row.createCell(2);
        SCPInfo.setCellValue(userAble.getSend_check_person());
        Cell focusInfo = row.createCell(3);
        focusInfo.setCellValue(userAble.getFocus());
        Cell selfControlInfo = row.createCell(4);
        selfControlInfo.setCellValue(userAble.getSelf_control());
        Cell languageInfo = row.createCell(5);
        languageInfo.setCellValue(userAble.getLanguage());
        Cell musicInfo = row.createCell(6);
        musicInfo.setCellValue(userAble.getMusic());
        Cell memoryInfo = row.createCell(7);
        memoryInfo.setCellValue(userAble.getScene_memory());
        Cell eqInfo = row.createCell(8);
        eqInfo.setCellValue(userAble.getEmotions_control());
        Cell learnInfo = row.createCell(9);
        learnInfo.setCellValue(userAble.getLearn_ab());
        Cell createInfo = row.createCell(10);
        createInfo.setCellValue(userAble.getCreate_ab());
        Cell mathInfo = row.createCell(11);
        mathInfo.setCellValue(userAble.getMath());
        Cell sportInfo = row.createCell(12);
        sportInfo.setCellValue(userAble.getSport());
        Cell witInfo = row.createCell(13);
        witInfo.setCellValue(userAble.getWit());
        rowNo++;
    }
    ByteArrayOutputStream os = new ByteArrayOutputStream();

    try {
        wb.write(os);
    } catch (IOException e) {
        e.printStackTrace();
    }

    byte[] content = os.toByteArray();
    InputStream is = new ByteArrayInputStream(content);

    // 设置response参数,可以打开下载页面
    response.reset();
    response.setContentType("application/vnd.ms-excel;charset=utf-8");
    response.setHeader("Content-Disposition", "attachment;filename=" + 
            new String(("aaa" + ".xls").getBytes(), "iso-8859-1")); 
    response.setContentType("application/octet-stream;charset=UTF-8");          


    ServletOutputStream out = response.getOutputStream();

    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;

    try {

        bis = new BufferedInputStream(is);
        bos = new BufferedOutputStream(out);

        byte[] buff = new byte[2048];
        int bytesRead;

        // Simple read/write loop.
        while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
            bos.write(buff, 0, bytesRead);
        }

    } catch (final IOException e) {
        throw e;
    } finally {
        if (bis != null)
            bis.close();
        if (bos != null)
            bos.close();
    }

}

}

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-02-22 14:22
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境