JS Export formatted Excel instance method

Source: Internet
Author: User
Tags instance method xmlns
The most common way is to use the JS function to manipulate excle files, which may have compatibility problems due to different browsers.   Another easy way to do this is to export an XML file that Excel can recognize in a format and open it in Excel. Copy Code code as follows:


function Gettabledatabyxml (intable, Inwindow) {


var rows = 0;


//alert ("Gettbldata is" + Inwindow);


var tbldocument = document;


if (!! Inwindow && Inwindow!= "") {


if (!document.all (Inwindow)) {


return null;


        }


else {


tbldocument = eval (inwindow). Document;


        }


    }


var intbl = Tbldocument.getelementbyid (intable);


var outstr = "<?xml version=" 1.0 ">n";


outstr = outstr + "<?mso-application progid=" Excel.Sheet ">n";


outstr = outstr + "<workbook xmlns=" Urn:schemas-microsoft-com:office:spreadsheet "";


outstr = outstr + "xmlns:o=" Urn:schemas-microsoft-com:office:office "";


outstr = outstr + "xmlns:x=" Urn:schemas-microsoft-com:office:excel "";


outstr = outstr + "xmlns:ss=" Urn:schemas-microsoft-com:office:spreadsheet ">n";


outstr = outstr + "<worksheet ss:name=" Sheet1 ">n";


outstr = outstr + "<table ss:expandedcolumncount=" ">n";


var re =/^[0-9]+.? [0-9]*$/; is a digital


if (intbl!= null) {


for (var j = 0, J < InTbl.rows.length; J + +) {


outstr + + "<row ss:autofitheight=" 0 ">n";


for (var i = 0; i < intbl.rows[j].cells.length; i++) {


if (i = = 0 && rows > 0) {


outstr + = "<cell><data ss:type=" String "></data></cell>n";


rows = 1;


}


var cellvalue = Intbl.rows[j].cells[i].innertext;


//Less than 12 digits with number


if (re.test (Cellvalue) && (new String (cellvalue). Length < 11) {


outstr = outstr + "<cell><data ss:type=" number ">" + cellvalue + "</data></c Ell>n ";


}else{


outstr = outstr + "<cell><data ss:type=" String ">" + cellvalue + "</data></cell>n";


                }


if (Intbl.rows[j].cells[i].colspan > 1) {


for (var k = 0; k < intbl.rows[j].cells[i].colspan-1; k++) {


outstr + = "<cell><data ss:type=" String "></data></cell>n";


}


                }


if (i = = 0) {


if (rows = = 0 && intbl.rows[j].cells[i].rowspan > 1) {


rows = intbl.rows[j].cells[i].rowspan-1;


                    }


                }


}


outstr + = "</row>n";


        }


    }


else {


outstr = null;


alert ("The table you want to export does not exist!!");


return;


    }


outstr = outstr + "</Table>n</Worksheet>n</Workbook>";


return outstr;


}


This function is originally a function of exporting TXT file. By saving an Excel file as an XML file, you get an XML file in which Excel can identify the content format.

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.