问题描述
大神,现象如标题一样,就是MVC在往页面输出EXCEL的输出流的时候,页面自己就关闭了,只剩一个下载页面。这个该怎么解决啊~~代码是这样的导出帮助类如下:///<summary>///EXCEL导出帮助类///</summary>///<typeparamname="T">泛型类型,制定实体</typeparam>publicclassExcelHelper<T>{publicstaticMemoryStreamListToExcel<T>(IList<T>list,stringtitleName,stringfileName,Dictionary<string,string>propertyName){//创建流对象using(MemoryStreamms=newMemoryStream()){//将参数写入到一个临时集合中List<string>propertyNameList=newList<string>();if(propertyName!=null)propertyNameList.AddRange(propertyName.Keys);//床NOPI的相关对象IWorkbookworkbook=newHSSFWorkbook();ISheetsheet=workbook.CreateSheet();IRowheaderRow=sheet.CreateRow(0);#region样式设定//--------------表头标题及文字显示样式----------------//ICellcell=headerRow.CreateCell(0);cell.SetCellValue(titleName);ICellStylestyle=workbook.CreateCellStyle();style.Alignment=HorizontalAlignment.Center;IFontfont=workbook.CreateFont();font.FontHeight=20*20;style.SetFont(font);cell.CellStyle=style;//--------------列名样式----------------//IFontfont2=workbook.CreateFont();ICellStylestyle1=workbook.CreateCellStyle();font2.FontName="Arial";font2.FontHeightInPoints=12;font2.Color=HSSFColor.Red.Index;style1.SetFont(font2);#endregionif(list.Count>0){//通过反射得到对象的属性集合PropertyInfo[]propertys=list[0].GetType().GetProperties();//定义储存26个字母的数组string[]LetterStr={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};intindex=0;//读取26个字母位置索引的变量intcellIndex=0;//储存输入数据列的索引IRowdataRow1=sheet.CreateRow(1);//遍历属性集合生成excel的表头标题for(inti=0;i<propertys.Count();i++){//判断此属性是否是用户定义属性if(propertyNameList.Count==0){dataRow1.CreateCell(i).SetCellValue(propertys[i].Name);}else{if(propertyNameList.Contains(propertys[i].Name)){dataRow1.CreateCell(cellIndex).SetCellValue(propertyName[propertys[i].Name]);index++;cellIndex++;}}}//设置合并单元格sheet.AddMergedRegion(newCellRangeAddress(0,0,0,(index==0?1:index-1)));//设置筛选框范围CellRangeAddressc=CellRangeAddress.ValueOf("A2:"+(index==0?"A2":LetterStr[index-1])+"2");sheet.SetAutoFilter(c);introwIndex=2;//遍历集合生成excel的行集数据for(inti=0;i<list.Count;i++){IRowdataRow=sheet.CreateRow(rowIndex);intcellIndex2=0;for(intj=0;j<propertys.Count();j++){if(propertyNameList.Count==0){objectobj=propertys[j].GetValue(list[i],null);dataRow.CreateCell(j).SetCellValue(obj.ToString());}else{if(propertyNameList.Contains(propertys[j].Name)){objectobj=propertys[j].GetValue(list[i],null);dataRow.CreateCell(cellIndex2).SetCellValue(obj.ToString());cellIndex2++;}}}rowIndex++;}}workbook.Write(ms);ms.Flush();ms.Position=0;returnms;}}}调用如下(直接在Index里输出了):publicActionResultIndex(){List<User>list=newList<User>();for(inti=0;i<10;i++){Useru=newUser();u.Age=i.ToString();u.IsLike="是";u.Name="测试"+i;u.School="学校"+i;u.Sex="男";list.Add(u);}Dictionary<string,string>dic=newDictionary<string,string>();//dic.Add("Name","名字");dic.Add("Sex","性别");//dic.Add("Age","年龄");dic.Add("School","学校");//dic.Add("IsLike","是否喜欢他");string[]propertyName=newstring[]{"Name","Age","School"};MemoryStreamms=ExcelHelper<User>.ListToExcel(list,"ThisIsMyTestEXCEL","Result",dic);Response.ContentType="application/vnd.ms-excel;charset=UTF-8";HttpContext.Current.Response.AddHeader("Content-Disposition",string.Format("attachment;filename={0}.xls","Result"));HttpContext.Current.Response.Clear();Response.BinaryWrite(ms.GetBuffer());HttpContext.Current.Response.End();returnView();