小弟我做了一个用extjs导出excel文件
仿造网上例子做的。。之前能导出,可是现在比较怪异的问题是过了几天发现导出的excel不能打开了。。
我把代码贴出来:
js代码:
checkOutIndemnity : function(){
var vExportContent = this.PviewGrid.getExcelXml();
                        var fd=Ext.get('frmDummy');
                        if (!fd) {
                            fd=Ext.DomHelper.append(Ext.getBody(),
                                {
                                    tag:'form',
                                    method:'POST',
                                    id:'frmDummy',
                                    action:'exportexcel.jsp', 
                                    target:'_blank',
                                    name:'frmDummy',
                                    cls:'x-hidden',
                                    cn:[
                                    {
                                        tag:'input',
                                        name:'exportContent',
                                        id:'exportContent',
                                        type:'hidden'
                                    }
                                    ]
                                },true);
                        }
                        fd.child('#exportContent').set({value:vExportContent});
                        fd.dom.submit();
}
jsp代码:
<%@ page language="java"  pageEncoding="UTF-8"%>
<%  
String content = request.getParameter("exportContent");
System.out.print(content);
content = new String(content.getBytes("ISO8859_1"),"utf-8");
response.setHeader("Content-Type","application/force-download"); 
response.setHeader("Content-Type","application/vnd.ms-excel"); 
response.setHeader("Content-disposition","attachment;filename=export.xls"); 
out.print(content); 
%>
之前的乱码问题也解决了。。但是现在就是导出的excel 不能打开,2003和2007我都试过了 没用。出现:加载期间出现问题,工作簿设置。。
内容肯定是在excel里面,,因为文件很大,,但是就是打不开。。我晕死了。。

解决方案 »

  1.   

    前端一个请求把内容发给后端,然后后端返回一个文件流。这跟前台JS关系不大。 你可以试着用FB调一下 看看向后台发请求的时候给的字符流对不对,然后再继续找问题。
      

  2.   


     这个问题我在用jxl时候碰到这个问题
     最后发现是excel版本问题。
    extjs 没有碰到过
    记得官方extjs插件有这个插件
    能直接到出 和导入
      

  3.   

    LZ好像没有贴出生成EXCEL的代码?如果我没理解错的话,生成EXCEL的工作是在后台执行的。首先看看后台生成的EXCEL文件在没有下载之前是否可以打开。最总要的下载的时候加个这个:
    Response.reset();
       
      

  4.   

    不知道我理解的对不对,我做了一个方法获取XML配置然后导出所有库存信息也就好几万条数据吧不会出现说文件打不开,其实只是一个文件流的输出什么格式都支持的, LZ可以参考下 private void ListToCsv(IList<Storage> dataList, string fileName, int beginIndex, int endIndex, string ExeType, string FileType)
        {
            // 当前对话 
            System.Web.HttpContext curContext = System.Web.HttpContext.Current;
            endIndex = pageCount;
            if (fileName == "")
                FileType = "csv";
            curContext.Response.Clear();
            if (dataList == null)
                return;
            if (endIndex <= 0 || endIndex > dataList.Count)
                endIndex = dataList.Count;        if (dataList != null)
            {
                // 设置编码和附件格式 
                curContext.Response.ContentType = "application/vnd.ms-excel";
                curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                curContext.Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(fileName,Encoding.GetEncoding("gb2312")) + "." + FileType);
                curContext.Response.Flush();
                //导出excel文件             StringBuilder sb = new StringBuilder();
                StringBuilder sbline = new StringBuilder();
                string coltmp = FileType.ToLower() == "csv" ? "\"{0}\"," : "{0},";            Storage rage = new Storage();
                int itemp = 0;
                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath("/Xml/OutStockFormart.xml"));
                XmlNodeList xmlNodeList = doc.SelectNodes("//FileFormats/FileFormat[@Code='" + ExeType + "']/Field[@Display='True']");
                foreach (XmlNode xdcolName in xmlNodeList)
                {
                    sbline.Append(string.Format(coltmp, xdcolName.Attributes["Description"].Value));
                }
                if (sbline.Length == 0)
                    return;
                sbline.ToString().Remove(sbline.Length - 1);
                sb.AppendLine(sbline.ToString());
                sbline.Remove(0, sbline.Length);            foreach (Storage age in dataList)
                {
                    if (itemp == endIndex || itemp == 10000)
                        break;                sbline.Remove(0, sbline.Length);
                    foreach (XmlNode xdcolValue in xmlNodeList)
                    {
                        string fieldName = xdcolValue.Attributes["Name"].Value;
                        if (!string.IsNullOrEmpty(fieldName))
                        {
                            PropertyInfo p = age.GetType().GetProperty(fieldName);
                            if (p == null)
                            {
                                sbline.Append(string.Format(coltmp, ""));
                                continue;
                            }
                            object value = p.GetValue(age, null);
                            if (value == null)
                            {
                                sbline.Append(string.Format(coltmp, ""));
                                continue;
                            }
                            if (p.PropertyType == typeof(Decimal))
                            {
                                if ((Decimal)value == decimal.MinValue)
                                {
                                    value = 0;
                                }
                            }                        sbline.Append(string.Format(coltmp, value.ToString()));                    }
                    }
                    sb.AppendLine(sbline.ToString());
                    itemp++;
                }            //返回客户端 
               
               //HttpCookie   aCookie = new HttpCookie("cbo"); 
               //aCookie.Expires = DateTime.Now.AddDays(-1); 
               //Response.Cookies.Add(aCookie); 
               
               
                curContext.Response.Write(sb.ToString());
                curContext.Response.End();
             
            }
        }