通過(guò)Javascript將數(shù)據(jù)導(dǎo)出到外部Excel文檔的函數(shù)代碼
更新時(shí)間:2012年06月15日 23:04:09 作者:
通過(guò)Javascript將數(shù)據(jù)導(dǎo)出到外部Excel文檔的函數(shù)代碼,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
function AutomateExcel() {
try {
//Start Excel and get Application object.
var oXL;
try
{
oXL = new ActiveXObject("Excel.Application");
}
catch(e)
{
alert("無(wú)法啟動(dòng)Excel!\n\n如果您確信您的電腦中已經(jīng)安裝了Excel," + "那么請(qǐng)調(diào)整IE的安全級(jí)別。\n\n具體操作:\n\n" + "工具 → Internet選項(xiàng) → 安全 → 自定義級(jí)別 → 對(duì)沒(méi)有標(biāo)記為安全的ActiveX進(jìn)行初始化和腳本運(yùn)行 → 啟用");
return false;
}
//Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var table = $("#GridView1")[0];
var rows = table.rows;
var columns = table.rows(0).cells;
var codes = "";
//設(shè)置標(biāo)題
var name = "第 <%= _CurrentPage %> 頁(yè)";
oXL.Caption = name;
oSheet.Name = name;
//設(shè)置表頭
oSheet.Cells(1, 1).Value = "申請(qǐng)單編號(hào)";
oSheet.Cells(1, 2).Value = "出貨金額";
oSheet.Cells(1, 3).Value = "營(yíng)銷(xiāo)部門(mén)";
oSheet.Cells(1, 4).Value = "業(yè)務(wù)人員";
oSheet.Cells(1, 5).Value = "內(nèi)部合同號(hào)";
oSheet.Cells(1, 6).Value = "客戶(hù)名稱(chēng)";
oSheet.Cells(1, 7).Value = "幣種";
oSheet.Cells(1, 8).Value = "客戶(hù)船期";
oSheet.Cells(1, 9).Value = "國(guó)別";
oSheet.Cells(1, 10).Value = "核銷(xiāo)單號(hào)";
oSheet.Cells(1, 11).Value = "發(fā)票號(hào)碼";
oSheet.Cells(1, 12).Value = "報(bào)關(guān)日期";
//獲取當(dāng)前頁(yè)申請(qǐng)單編號(hào)
for (var i = 2; i <= rows.length; i++) {
codes += "'" + rows(i - 1).cells(0).innerText + "',";
}
codes += "''";
//獲取數(shù)據(jù)并填充數(shù)據(jù)到EXCEL
$.post("../Handlers/ShippingApplyHandler.ashx",
{ Action: "ExportData", ExportCondition: codes },
function (views) {
if (views != null) {
var beginindex = 1;
var endindex = 1;
for (var i = 0; i < views.length; i++) {
endindex++;
oSheet.Cells(i + 2, 1).Value = views[i].SACode;
oSheet.Cells(i + 2, 2).Value = views[i].AmountSum;
oSheet.Cells(i + 2, 3).Value = views[i].Department;
oSheet.Cells(i + 2, 4).Value = views[i].SalesName;
oSheet.Cells(i + 2, 5).Value = views[i].ContractNo;
oSheet.Cells(i + 2, 6).Value = views[i].CustomerName;
oSheet.Cells(i + 2, 7).Value = views[i].CurrencyCode;
if (views[i].CustomerSchedule != null) {
oSheet.Cells(i + 2, 8).Value = ConvertToJSDate(views[i].CustomerSchedule).Format("yyyy-MM-dd");
}
oSheet.Cells(i + 2, 9).Value = views[i].Country;
oSheet.Cells(i + 2, 10).Value = views[i].VerificationNumber;
oSheet.Cells(i + 2, 11).Value = views[i].InvoiceNumber;
if (views[i].CustomsDate != null) {
oSheet.Cells(i + 2, 12).Value = ConvertToJSDate(views[i].CustomsDate).Format("yyyy-MM-dd");
}
if (i > 0 && views[i - 1].SACode == views[i].SACode) {
oSheet.Range(oSheet.Cells(beginindex, 1), oSheet.Cells(endindex, 1)).Merge();
oSheet.Range(oSheet.Cells(beginindex, 2), oSheet.Cells(endindex, 2)).Merge();
oSheet.Range(oSheet.Cells(beginindex, 3), oSheet.Cells(endindex, 3)).Merge();
oSheet.Range(oSheet.Cells(beginindex, 4), oSheet.Cells(endindex, 4)).Merge();
oSheet.Range(oSheet.Cells(beginindex, 5), oSheet.Cells(endindex, 5)).Merge();
oSheet.Range(oSheet.Cells(beginindex, 6), oSheet.Cells(endindex, 6)).Merge();
oSheet.Range(oSheet.Cells(beginindex, 7), oSheet.Cells(endindex, 7)).Merge();
oSheet.Range(oSheet.Cells(beginindex, 8), oSheet.Cells(endindex, 8)).Merge();
beginindex = endindex;
}
else {
beginindex++;
}
}
}
}, "json");
//設(shè)置自動(dòng)列寬
oSheet.Columns.AutoFit();
//設(shè)置excel為可見(jiàn)
oXL.Visible = true;
//將Excel交由用戶(hù)控制
oXL.UserControl = true;
//禁止提示
oXL.DisplayAlerts = false;
//釋放資源
//oXL = null;
//oWB = null;
//oSheet = null;
}
catch (e) {
}
}
您可能感興趣的文章:
- JS實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出到Excel的方法詳解
- js實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出為EXCEL(支持大量數(shù)據(jù)導(dǎo)出)
- Vue導(dǎo)出json數(shù)據(jù)到Excel電子表格的示例
- AngularJs導(dǎo)出數(shù)據(jù)到Excel的示例代碼
- JSP實(shí)現(xiàn)從數(shù)據(jù)庫(kù)導(dǎo)出數(shù)據(jù)到Excel下載的方法
- js導(dǎo)出table數(shù)據(jù)到excel即導(dǎo)出為EXCEL文檔的方法
- javascript 導(dǎo)出數(shù)據(jù)到Excel(處理table中的元素)
- javascript?實(shí)現(xiàn)純前端將數(shù)據(jù)導(dǎo)出excel兩種方式
相關(guān)文章
onmouseover事件和onmouseout事件全面理解
最近兩天在溫習(xí)onmouseover事件和onmouseout事件,其實(shí)里面有很多深?yuàn)W的知識(shí),接下來(lái)小編給大家?guī)?lái)了onmouseover事件和onmouseout事件全面了解,感興趣的朋友一起看下2016-08-08javascript設(shè)計(jì)模式 – 裝飾模式原理與應(yīng)用實(shí)例分析
這篇文章主要介紹了javascript設(shè)計(jì)模式 – 裝飾模式,結(jié)合實(shí)例形式分析了javascript裝飾模式基本概念、原理、應(yīng)用場(chǎng)景及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04JavaScript?Promise執(zhí)行流程深刻理解
這篇文章主要介紹了JavaScript?Promise執(zhí)行流程深刻理解,他是一個(gè)構(gòu)造函數(shù),每個(gè)創(chuàng)建的promise都有各自狀態(tài)和值,且狀態(tài)初始值為pending,值為undefined2022-06-06Js實(shí)現(xiàn)當(dāng)前點(diǎn)擊a標(biāo)簽變色突出顯示其他a標(biāo)簽回復(fù)原色
當(dāng)一個(gè)頁(yè)面有多個(gè)a標(biāo)簽,實(shí)現(xiàn)當(dāng)前點(diǎn)擊a標(biāo)簽變色,其他a標(biāo)簽回復(fù)原色,具體實(shí)現(xiàn)如下,喜歡的朋友可以參考下2013-11-11javascript實(shí)現(xiàn)的樣式表(CSS) 格式整理與壓縮
javascript實(shí)現(xiàn)的樣式表(CSS) 格式整理與壓縮,可以分為多行與單行,非常不錯(cuò)。2010-05-05原生js仿jq判斷當(dāng)前瀏覽器是否為ie,精確到ie6~8
這篇文章主要介紹了原生js仿jq判斷當(dāng)前瀏覽器是否為ie,精確到ie6~8,需要的朋友可以參考下2014-08-08