Function Code _ Javascript tips for exporting data to an external Excel file using javascript

Source: Internet
Author: User
Use Javascript to export data to the function code of the external Excel document. For more information, see The Code is as follows:


Function AutomateExcel (){
Try {
// Start Excel and get Application object.
Var oXL;
Try
{
OXL = new ActiveXObject ("Excel. Application ");
}
Catch (e)
{
Alert ("cannot start Excel! \ N if you are sure that Excel, "+" is installed on your computer, adjust the security level of IE. \ N specific operation: \ n "+" tool → Internet Options → Security → Custom Level → initialize ActiveX without being marked as secure and run scripts → enable ");
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 = "";
// Set the title
Var name = "page <% = _ CurrentPage %> ";
OXL. Caption = name;
OSheet. Name = name;
// Set the header
OSheet. Cells (1, 1). Value = "Application No ";
OSheet. Cells (1, 2). Value = "shipment amount ";
OSheet. Cells (1, 3). Value = "Marketing Department ";
OSheet. Cells (1, 4). Value = "business personnel ";
OSheet. Cells (1, 5). Value = "Internal Contract No ";
OSheet. Cells (1, 6). Value = "customer name ";
OSheet. Cells (1, 7). Value = "currency ";
OSheet. Cells (1, 8). Value = "customer Schedule ";
OSheet. Cells (1, 9). Value = "country ";
OSheet. Cells (1, 10). Value = "Statement No ";
OSheet. Cells (1, 11). Value = "invoice number ";
OSheet. Cells (1, 12). Value = "date declared ";
// Obtain the application number on the current page
For (var I = 2; I <= rows. length; I ++ ){
Codes + = "'" + rows (I-1). cells (0). innerText + "',";
}
Codes + = "''";
// Obtain the data and fill in the data to 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 ");
// Set the automatic column width
OSheet. Columns. AutoFit ();
// Set excel to visible
OXL. Visible = true;
// Submit the Excel file to the user for control
OXL. UserControl = true;
// Forbidden message
OXL. DisplayAlerts = false;
// Release resources
// OXL = null;
// OWB = null;
// OSheet = null;
}
Catch (e ){
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.