Home > Web Front-end > JS Tutorial > body text

Function code to export data to external Excel document through Javascript_javascript skills

WBOY
Release: 2016-05-16 17:52:51
Original
1114 people have browsed it
Copy code 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("Excel cannot be started!nn If you are sure that Excel is already installed on your computer," "Then please adjust the security level of IE. nSpecific operations: n" "Tools → Internet Options → Security → Custom Level → Initialize and script ActiveX not marked as safe → 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 title
var name = "Page<%= _CurrentPage %> ";
oXL.Caption = name;
oSheet.Name = name;
//Set header
oSheet.Cells(1, 1).Value = "Application number";
oSheet.Cells(1, 2).Value = "Shipping Amount";
oSheet.Cells(1, 3).Value = "Marketing Department";
oSheet.Cells(1, 4).Value = "Business Personnel";
oSheet.Cells(1, 5).Value = "Internal Contract Number";
oSheet.Cells(1, 6).Value = "Customer Name";
oSheet. Cells(1, 7).Value = "Currency";
oSheet.Cells(1, 8).Value = "Customer shipping date";
oSheet.Cells(1, 9).Value = "Country Don't";
oSheet.Cells(1, 10).Value = "Verification note number";
oSheet.Cells(1, 11).Value = "Invoice number";
oSheet.Cells( 1, 12).Value = "Customs declaration date";
//Get the application number of the current page
for (var i = 2; i <= rows.length; i ) {
codes = " '" rows(i - 1).cells(0).innerText "',";
}
codes = "''";
//Get the data and fill it into 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 automatic column width
oSheet.Columns.AutoFit( );
//Set excel as visible
oXL.Visible = true;
//Put Excel under user control
oXL.UserControl = true;
//Suppress prompts
oXL.DisplayAlerts = false;
//Release resources
//oXL = null;
//oWB = null;
//oSheet = null;
}
catch (e ) {
}
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template