通過Javascript將數據導出到外部Excel文檔的函數代碼,需要的朋友可以參考下
代碼如下:
function AutomateExcel() {
try {
//Start Excel and get Application object.
var oXL;
try
{
oXL = new ActiveXObject("Excel.Application");
}
catch(e)
{
alert("無法啟動Excel!nn如果您確信您的電腦中已經安裝了Excel," + "那麼請調整IE的安全級別。nn具體操作:nn" + "工具 → Internet選項 → 安全 → 自定義級別 → 對沒有標記為安全的ActiveX進行初始化和腳本運行 → 啟用");
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 = "";
//設置標題
var name = "第 <%= _CurrentPage %> 頁";
oXL.Caption = name;
oSheet.Name = name;
//設置表頭
oSheet.Cells(1, 1).Value = "申請單編號";
oSheet.Cells(1, 2).Value = "出貨金額";
oSheet.Cells(1, 3).Value = "營銷部門";
oSheet.Cells(1, 4).Value = "業務人員";
oSheet.Cells(1, 5).Value = "內部合同號";
oSheet.Cells(1, 6).Value = "客戶名稱";
oSheet.Cells(1, 7).Value = "幣種";
oSheet.Cells(1, 8).Value = "客戶船期";
oSheet.Cells(1, 9).Value = "國別";
oSheet.Cells(1, 10).Value = "核銷單號";
oSheet.Cells(1, 11).Value = "發票號碼";
oSheet.Cells(1, 12).Value = "報關日期";
//獲取當前頁申請單