firefox 下载文件时选择使用wps 打开会出现无法打开文件的情况

黑白
  • 129

今天在使用java导出excel 功能时,发现一个奇怪的问题,就是在使用firefox 进行测试时,选择打开通过默认的wps 时出一下情况

操作:
image.png

结果
image.png

但是查看下载是下载完成并能够成功打开的
image.png


excel 工具类

package top.codedesigner.www.util;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 *
 *
 * @Description:
 */
public class ExcelUtil {

    /**
     * 导出Excel
     * @param sheetName sheet名称
     * @param title     标题
     * @param values    内容
     * @param wb        HSSFWorkbook对象
     * @return
     */
    public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {

        // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
        if (wb == null)
            wb = new HSSFWorkbook();

        // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet(sheetName);

        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        HSSFRow row = sheet.createRow(0);

        // 第四步,创建单元格,并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        //声明列对象
        HSSFCell cell = null;

        //创建标题
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }

        //创建内容
        for (int i = 0; i < values.length; i++) {
            row = sheet.createRow(i + 1);
            for (int j = 0; j < values[i].length; j++) {
                //将内容按顺序赋给对应的列对象
                row.createCell(j).setCellValue(values[i][j]);
            }
        }
        return wb;
    }
}



java 导出excel

/**
     * 导出excel;
     * @param response
     * @return
     */
    @Override
    public boolean exportExcel(HttpServletResponse response) {
        List<enroll> enrollList=registerMapper.findAllData();
        String[] title = { "姓名", "昵称", "学校", "专业班级", "学号", "电话","qq号","是否打印题面",
            "邮箱","注册时间","ip地址","请求头","cookie信息"
          };
        String fileName = "registerForm.xls";
        String sheetName = "sheet1";
        String[][] content = new String[enrollList.size()][title.length];
        //填充表格;
        for(int i=0;i<enrollList.size();i++){
            content[i][0]=enrollList.get(i).getReal_name();
            content[i][1]=enrollList.get(i).getNickname();
            content[i][2]=enrollList.get(i).getSchool();
            content[i][3]=enrollList.get(i).getStudent_class();
            content[i][4]=enrollList.get(i).getStudent_number();
            content[i][5]=enrollList.get(i).getTelephone();
            content[i][6]=enrollList.get(i).getQq_number();

            content[i][7]="1".equals(enrollList.get(i).getPrint_question()) ? "是" : "否";
            content[i][8]=enrollList.get(i).getEmail_number();
            content[i][9]= String.valueOf(enrollList.get(i).getRegistration_time());
            content[i][10]=enrollList.get(i).getIp_address();
            content[i][11]=enrollList.get(i).getRequest_head();
            content[i][12]=enrollList.get(i).getCookie();
        }

        //生成excel;

        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        try {
            //设置为utf-8格式;
            fileName = new String(fileName.getBytes(), "UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


        OutputStream os = null;
        try {
            os = response.getOutputStream();
            wb.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return true;
    }

请问这是什么原因造成的又该怎么解决呢?

回复
阅读 256
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏