A Simple Method for exporting and formatting excel files using js

Source: Internet
Author: User

1. The most common method is to use js functions to operate excle files. This method may vary with browsers and may cause compatibility issues.
2. Another simple method is to export an xml file that can be recognized by excel and open it in excel.
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).doc ument;
}
}

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 \"";
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 = \" 30 \ "> \ n ";
Var re =/^ [0-9] + .? [0-9] * $/; // whether it is a number

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;
// Number for a Number smaller than 12 digits
If (re. test (cellValue) & (new String (cellValue). length <11 ){
OutStr = outStr + "<Cell> <Data ss: Type = \" Number \ ">" + cellValue + "</Data> </Cell> \ 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;
}
 
The above functions are originally used to export txt files. Save an excel file as an xml file to get an xml file in which excel can recognize the content format.

Author: "ios2007"

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.