用 ajax 导出 execl 生成csv格式

前端之家收集整理的这篇文章主要介绍了用 ajax 导出 execl 生成csv格式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//

前段代码

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


<script type="text/javascript" >
$(document).ready(function () {
$('#EXECL').click(function () {

var dataPost = { action: "DOWNLOAD",selCTIME: $("#selCTIME").val(),selState: $("#selState").val(),txtOID: $("#txtOID").val(),textBTANAME: $("#textBTANAME").val()};

mc_post("/Reportform/_Ajax/DOWNLOADEXECL.aspx?t=" + Math.random(),dataPost,function (rtext) { //jQuery ajax的封装目的是查询你要导出数据
getXlsFromTbl(true,rtext)
});
});

});

//下面是把ajax查询出的数据进行下载

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function isIE() { //ie?
if (!!window.ActiveXObject || "ActiveXObject" in window)
return true;
else
return false;
}
function getXlsFromTbl(inWindow,rtext) {
try {
var allStr = "";
var curStr = "";
var fileName = getExcelFileName();

curStr = rtext
if (curStr != null) {
allStr += curStr;
}
else {
alert("你要导出的表不存在!");
return;
}
//fout.w
var uri = "data:text/csv;charset=utf-8,\ufeff" + encodeURIComponent(allStr);
//创建a标签模拟点击下载
var downloadLink = document.createElement("a");
downloadLink.href = uri;
downloadLink.download = fileName;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);


}
catch (e) {
alert("导出发生异常:" + e.name + "->" + e.description + "!");
}
}

function getExcelFileName() {
var d = new Date();
var curYear = d.getYear();
var curMonth = "" + (d.getMonth() + 1);
var curDate = "" + d.getDate();
var curHour = "" + d.getHours();
var curMinute = "" + d.getMinutes();
var curSecond = "" + d.getSeconds();
if (curMonth.length == 1) {
curMonth = "0" + curMonth;
}
if (curDate.length == 1) {
curDate = "0" + curDate;
}
if (curHour.length == 1) {
curHour = "0" + curHour;
}
if (curMinute.length == 1) {
curMinute = "0" + curMinute;
}
if (curSecond.length == 1) {
curSecond = "0" + curSecond;
}
var fileName = curYear + curMonth + curDate + "_"
+ curHour + curMinute + curSecond + ".csv";
//alert(fileName);
return fileName;
}
function doFileExport(inName,inStr) {
var xlsWin = null;
if (!!document.all("glbHideFrm")) {
xlsWin = glbHideFrm;
}
else {
var width = 6;
var height = 4;
var openPara = "left=" + (window.screen.width / 2 - width / 2)
+ ",top=" + (window.screen.height / 2 - height / 2)
+ ",scrollbars=no,width=" + width + ",height=" + height;
xlsWin = window.open("","_blank",openPara);
}
xlsWin.document.write(inStr);
xlsWin.document.close();
xlsWin.document.execCommand('Saveas',true,inName);
xlsWin.close();
}



</script>


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//后台 :拼接成csv格式返回给前台



string CSV = Miracle.Web.Public.Export.ToExcel(datatable);
string strResHeader = "attachment; filename=" + Guid.NewGuid().ToString() + ".csv";
Response.AppendHeader("Content-Disposition",strResHeader);//attachment说明以附件下载,inline说明在线打开
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = Encoding.GetEncoding("GB2312");//Encoding.GetEncoding("GB2312"); // Encoding.UTF8;//
//把原来的转换为byte数组,而且是utf_8编码的

Response.Write(CSV);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.End();


-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public static string ToExcel(DataTable dt) { if (dt == null || dt.Rows.Count <= 0) return ""; StringBuilder sb = new StringBuilder(); for (int i = 0; i < dt.Columns.Count; i++) { sb.Append( dt.Columns[i].ColumnName + ","); } //sb.Append("\r\n"); sb.Append(@" "); foreach (DataRow dr in dt.Rows) { for (int j = 0; j < dt.Columns.Count; j++) { sb.Append( dr[j].ToString() + ","); } //sb.Append("\r\n"); sb.Append(@" "); } dt.Dispose(); return sb.ToString(); }

猜你在找的Ajax相关文章