MVC 导出Excel 问题(求方案)

wx8849 2015-10-23 03:31:19
 前台调用
$.ajax({
url: '/SO/ExportData',
type: 'POST',
data: DataWhere
});

控制器
public ActionResult ExportData()
{
///查询条件
DataSet ds = SOExManager.GetInstance().GetSOList2(ht, GetSession().User.SysNo);
Dictionary<string, string> columnInfo = new Dictionary<string, string>();
columnInfo.Add("SOID", "测试");
//columnInfo.Add("ExpensesDate", "测试1");
List<C3Export> List = (from p in ds.Tables[0].AsEnumerable()
select new C3Export { SOID = p.Field<string>("SOID") }).ToList();
ECFuncs.ExExcel<C3Export>(List, "测试.xls", columnInfo);
return null;
}

 输出excel
/// <summary>
/// 将一组对象导出成EXCEL
/// </summary>
/// <typeparam name="T">要导出对象的类型</typeparam>
/// <param name="objList">一组对象</param>
/// <param name="FileName">导出后的文件名</param>
/// <param name="columnInfo">列名信息</param>
public static void ExExcel<T>(IList objList, string FileName, Dictionary<string, string> columnInfo)
{
if (columnInfo.Count == 0) { return ; }
if (objList.Count == 0) { return ; }
//生成EXCEL的HTML
string excelStr = "";
Type myType = objList[0].GetType();
//根据反射从传递进来的属性名信息得到要显示的属性
List<System.Reflection.PropertyInfo> myPro = new List<System.Reflection.PropertyInfo>();
foreach (string cName in columnInfo.Keys)
{
System.Reflection.PropertyInfo p = myType.GetProperty(cName);
if (p != null)
{
myPro.Add(p);
excelStr += columnInfo[cName] + "\t";
}
}
//如果没有找到可用的属性则结束
if (myPro.Count == 0) { return ; }
excelStr += "\n";
foreach (T obj in objList)
{
foreach (System.Reflection.PropertyInfo p in myPro)
{
excelStr += p.GetValue(obj, null) + "\t";
}
excelStr += "\n";
}
//输出EXCEL
HttpResponse rs = System.Web.HttpContext.Current.Response;
rs.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
rs.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
rs.ContentType = "application/ms-excel";
rs.Write(excelStr);
rs.End();
}
...全文
279 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
wx8849 2015-10-23
  • 打赏
  • 举报
回复
引用 13 楼 sp1234 的回复:
你这里写
rs.Write(....)
代码,又写什么 ajax post,这二者是根本不匹配的。ajax 需要获得服务器端返回来的“数据”,然后在 jaavscript 端的 success 属性所引用的回调函数中做进一步前端处理。从你的代码上根本看不出来这个路数。 反而你的思路是搞 window.open(....) 弹出页面的模式。
刚开始不想生成excel 存在服务器上面,然后post 有不能下载 ,如果没有参数肯定不考虑ajax post
wx8849 2015-10-23
  • 打赏
  • 举报
回复
引用 12 楼 wyd1520 的回复:
[quote=引用 11 楼 wx8849 的回复:] [quote=引用 8 楼 wyd1520 的回复:] 你不能用JQUERY去请求。。。 function downloadFile() { window.location = 你的页面地址; 只能这样才会弹出下载框。 }
这样是不是必须先生成excel 保存到服务器 然后根据名称去下载?[/quote] 如果你要传参POST 那也不能用AJAX 来POST,你只能用表单POST,如果不想用表单 那就用urlGET网址带参数 这是我之前下载图片用的方式。也是带参数的


<script>
    function downloadFile() {
        var url = $("#pic").attr("src");
        window.location = url + "&f=1";
    }
</script>
<div style="padding: 15px; overflow: hidden" >
    <form id="Form_ViewCode" method="post">
      <table style="width:100%; height:100%">
        <tr>
            <td align="center"><img id="pic"></td>
        </tr>
         <tr>
            <td align="center"><label id="code"/></td>
        </tr>
        <tr>
            <td align="center" height="13"></td>
        </tr>
        <tr>
            <td align="center"><a href="#" class="easyui-linkbutton" iconCls="icon-save"  onclick="downloadFile()">下载</a></td>
        </tr>
    </table>
         
    </form>
</div>
[/quote] 你这个只有1个参数没什么,我这个多的时候有10几个参数,URL直接传怕长度不够,我还是生成excel存在服务器,post 返回名称去下载吧
  • 打赏
  • 举报
回复
引用 11 楼 wx8849 的回复:
[quote=引用 8 楼 wyd1520 的回复:] 你不能用JQUERY去请求。。。 function downloadFile() { window.location = 你的页面地址; 只能这样才会弹出下载框。 }
这样是不是必须先生成excel 保存到服务器 然后根据名称去下载?[/quote] 这个思路是可以的。 ajax post 语句应该返回的一个服务器端保存的excel文件的地址,然后在 ajax 的success 函数中去打开这个地址的网页,就能下载了。
  • 打赏
  • 举报
回复
你这里写
rs.Write(....)
代码,又写什么 ajax post,这二者是根本不匹配的。ajax 需要获得服务器端返回来的“数据”,然后在 jaavscript 端的 success 属性所引用的回调函数中做进一步前端处理。从你的代码上根本看不出来这个路数。 反而你的思路是搞 window.open(....) 弹出页面的模式。
本拉灯 2015-10-23
  • 打赏
  • 举报
回复
引用 11 楼 wx8849 的回复:
[quote=引用 8 楼 wyd1520 的回复:] 你不能用JQUERY去请求。。。 function downloadFile() { window.location = 你的页面地址; 只能这样才会弹出下载框。 }
这样是不是必须先生成excel 保存到服务器 然后根据名称去下载?[/quote] 如果你要传参POST 那也不能用AJAX 来POST,你只能用表单POST,如果不想用表单 那就用urlGET网址带参数 这是我之前下载图片用的方式。也是带参数的


<script>
    function downloadFile() {
        var url = $("#pic").attr("src");
        window.location = url + "&f=1";
    }
</script>
<div style="padding: 15px; overflow: hidden" >
    <form id="Form_ViewCode" method="post">
      <table style="width:100%; height:100%">
        <tr>
            <td align="center"><img id="pic"></td>
        </tr>
         <tr>
            <td align="center"><label id="code"/></td>
        </tr>
        <tr>
            <td align="center" height="13"></td>
        </tr>
        <tr>
            <td align="center"><a href="#" class="easyui-linkbutton" iconCls="icon-save"  onclick="downloadFile()">下载</a></td>
        </tr>
    </table>
         
    </form>
</div>
wx8849 2015-10-23
  • 打赏
  • 举报
回复
引用 8 楼 wyd1520 的回复:
你不能用JQUERY去请求。。。 function downloadFile() { window.location = 你的页面地址; 只能这样才会弹出下载框。 }
这样是不是必须先生成excel 保存到服务器 然后根据名称去下载?
  • 打赏
  • 举报
回复
asp.net mvc 在 ajax 交互操作上本身就是个坑。 ajax 只要访问数据,又没有什么页面“界面”,从“天然”上说它就是对应着“一般处理程序ashx”。
wx8849 2015-10-23
  • 打赏
  • 举报
回复
引用 8 楼 wyd1520 的回复:
你不能用JQUERY去请求。。。 function downloadFile() { window.location = 你的页面地址; 只能这样才会弹出下载框。 }
我有传递查询参数的 而且还很多
本拉灯 2015-10-23
  • 打赏
  • 举报
回复
你不能用JQUERY去请求。。。 function downloadFile() { window.location = 你的页面地址; 只能这样才会弹出下载框。 }
本拉灯 2015-10-23
  • 打赏
  • 举报
回复
你又没POST数据过来,为毛要POST。。。改用GET试试
wx8849 2015-10-23
  • 打赏
  • 举报
回复
引用 2 楼 wyd1520 的回复:
excelStr 不是字节数组当然没返应 你这个只是输出HTML 还有 ActionResult ExportData() { 这个要返回FileResult 才会提示下载。 return FileResult(FileName,contentTypexxx); }

DataSet ds = SOExManager.GetInstance().GetSOList2(ht, GetSession().User.SysNo);
            Dictionary<string, string> columnInfo = new Dictionary<string, string>();
            columnInfo.Add("SOID", "测试");
            //columnInfo.Add("ExpensesDate", "测试1");
            List<C3Export> List = (from p in ds.Tables[0].AsEnumerable()
                          select new C3Export { SOID = p.Field<string>("SOID") }).ToList();
            string ex = ECFuncs.ExExcel<C3Export>(List, "测试.xls", columnInfo);
            byte[] b = System.Text.Encoding.Default.GetBytes(ex);//字串转byte阵列 
            return File(b, "pplication/vnd.ms-excel");
改成这样还是不行 ,不能用post 吗?
本拉灯 2015-10-23
  • 打赏
  • 举报
回复
context.Response.ClearContent(); context.Response.AppendHeader(“ContentType”, "application/ms-excel"); context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + enCodeString + ".xls"); //关键 是要这个 context.Response.AppendHeader("Content-Length", buffer.Length.ToString()); //关键 是要这个 context.Response.BinaryWrite(buffer); //关键 是要这个
本拉灯 2015-10-23
  • 打赏
  • 举报
回复
你还是改成ashx一般应用程序来的更容易


          context.Response.ClearContent();
     context.Response.AppendHeader(“ContentType”, "application/ms-excel");
            context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + enCodeString + ".xls"); //关键 是要这个
             context.Response.AppendHeader("Content-Length", buffer.Length.ToString()); //关键 是要这个
             context.Response.BinaryWrite(buffer);   //关键 是要这个

wx8849 2015-10-23
  • 打赏
  • 举报
回复
引用 2 楼 wyd1520 的回复:
excelStr 不是字节数组当然没返应 你这个只是输出HTML 还有 ActionResult ExportData() { 这个要返回FileResult 才会提示下载。 return FileResult(FileName,contentTypexxx); }
这里必须要生成了excel 才能下载?
本拉灯 2015-10-23
  • 打赏
  • 举报
回复
excelStr 不是字节数组当然没返应 你这个只是输出HTML 还有 ActionResult ExportData() { 这个要返回FileResult 才会提示下载。 return FileResult(FileName,contentTypexxx); }
wx8849 2015-10-23
  • 打赏
  • 举报
回复
现在导出没有反映这为什么?

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧