0 0

struts2 导出excel后 打开提示“扩展名文件格式不一致”问题5

下面是我的代码

<form action="userAction!exportExcel"> 
<input type="submit">导出</input> 
</form> 

struts.xml配置:
<package name="" namespace="/" extends="json-default"> 
        <action name="userAction" class="com.truth99.action.UserAction"> 
            <result name="excel" type="stream"> 
                <param name="encode">true</param> 
                <param name="contentType">application/vnd.ms-excel;charset=GBK</param> 
                <param name="inputName">excelStream</param> 
                <param name="contentDisposition">attachment;filename=${fileName}</param> 
                <param name="bufferSize">20480</param> 
            </result> 
        </action> 
    </package>

package com.truth99.action; 
 
import java.io.ByteArrayInputStream; 
import java.io.InputStream; 
 
public class UserAction{ 
 
private InputStream excelStream; 
private String fileName = "golf.xls";//导出excel 
 
public String exportExcel(){ 
        try{ 
            StringBuffer sb = new StringBuffer(); 
            sb.append("title1").append("\t").append("title2").append("\n");
//\t表示下一列,\n表示下一行 
//导入的数据进行迭代,拼成字符串
            for(;;){ 
                sb.append("content1").append("\t").append("content2").append("\n"); 
            } 
//将字符串放入流中
            excelStream = new ByteArrayInputStream(sb.toString().getBytes("GBK"), 0, sb.toString().getBytes("GBK").length); 
        }catch(Exception e){ 
        } 
        return "excel"; 
    } 
    public InputStream getExcelStream() { 
        return excelStream; 
    } 
    public void setExcelStream(InputStream excelStream) { 
        this.excelStream = excelStream; 
    } 
    public String getFileName() { 
        return fileName; 
    } 
    public void setFileName(String fileName) { 
        this.fileName = fileName; 
    } 
} 

打开后提示



哪位大神帮忙解决一下。
2013年1月18日 17:27

3个答案 按时间排序 按投票排序

0 0

采纳的答案

像生成word、excel这类的文件,不是把后缀名改好之后就可以了,需要有第三方的框架来进行处理,就好比你把一个txt的文件直接把后缀名字改为excel的后缀名,你那样是打不开的,会报错误处理。
可以看下 poi或者是jxl这些专门处理excel的文档。

2013年1月22日 22:23
0 0

楼主这种Excel导出方式,是本人有生以来,第一次见!人才。。。

2013年1月18日 20:12
0 0

你不会认为随便一个文件名改成xls扩展后就变成excel、了吧,导出为excel、
你需要用poi等软件输出excel、流

2013年1月18日 19:22

相关推荐

Global site tag (gtag.js) - Google Analytics