62,046
社区成员
发帖
与我相关
我的任务
分享
前台调用
$.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();
}
<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 返回名称去下载吧rs.Write(....)
代码,又写什么 ajax post,这二者是根本不匹配的。ajax 需要获得服务器端返回来的“数据”,然后在 jaavscript 端的 success 属性所引用的回调函数中做进一步前端处理。从你的代码上根本看不出来这个路数。
反而你的思路是搞 window.open(....) 弹出页面的模式。
<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>
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 吗?
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); //关键 是要这个