Export Excel-3 using JavaScript

Source: Internet
Author: User

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> How to export an Excel document from a Web page </title>
</Head>
<Body>
<Table id = "tableexcel" width = "100%" border = "1" cellspacing = "0" cellpadding = "0">
<Tr>
<TD colspan = "5" align = "center">
How to export an Excel document from a Web page </TD>
</Tr>
<Tr>
<TD>
Column Title 1 </TD>
<TD>
Column Title 2 </TD>
<TD>
Column Title 3 </TD>
<TD>
Column Title 4 </TD>
<TD>
Column Title 5 </TD>
</Tr>
<Tr>
<TD>
AAA </TD>
<TD>
Bbb </TD>
<TD>
CCC </TD>
<TD>
Ddd </TD>
<TD>
Eee </TD>
</Tr>
<Tr>
<TD>
AAA </TD>
<TD>
Bbb </TD>
<TD>
CCC </TD>
<TD>
Ddd </TD>
<TD>
Eee </TD>
</Tr>
<Tr>
<TD>
Fff </TD>
<TD>
Ggg </TD>
<TD>
Hhh </TD>
<TD>
III </TD>
<TD>
Jjj </TD>
</Tr>
</Table>
<Input type = "button" onclick = "javascript: Method1 ('tableexcel ');" value = "method 1 import to excel">
<Input type = "button" onclick = "javascript: method2 ('tableexcel ');" value = "method 2 import to excel">
<Input type = "button" onclick = "javascript: getxlsfromtbl ('tableexcel ', null);" value = "method 3">
<MCE: script language = "JavaScript"> <! --
 
Function Method1 (tableid) {// copy the entire table to excel
VaR curtbl = Document. getelementbyid (tableid );
VaR oxl = new activexobject ("Excel. application ");
// Create an ax object Excel
VaR owb = oxl. workbooks. Add ();
// Obtain the workbook object
VaR osheet = owb. activesheet;
// Activate the current sheet
VaR sel = Document. Body. createTextRange ();
Sel. movetoelementtext (curtbl );
// Move the table content to textrange
Sel. Select ();
// Select all content in textrange
Sel.exe ccommand ("copy ");
// Copy the content in textrange
Osheet. paste ();
// Paste it to the Excel file of the activity
Oxl. Visible = true;
// Set the Excel visible attribute
}
Function method2 (tableid) // read each unit in the table to excel.
{
VaR curtbl = Document. getelementbyid (tableid );
VaR oxl = new activexobject ("Excel. application ");
// Create an ax object Excel
VaR owb = oxl. workbooks. Add ();
// Obtain the workbook object
VaR osheet = owb. activesheet;
// Activate the current sheet
VaR lenr = curtbl. Rows. length;
// Obtain the number of rows in the table
For (I = 0; I <lenr; I ++)
{
VaR lenc = curtbl. Rows (I). cells. length;
// Obtain the number of columns in each row
For (j = 0; j <lenc; j ++)
{
Osheet. cells (I + 1, J + 1). value = curtbl. Rows (I). cells (j). innertext;
// Assign a value
}
}
Oxl. Visible = true;
// Set the Excel visible attribute
}
Function getxlsfromtbl (intblid, inwindow ){
Try {
VaR allstr = "";
VaR curstr = "";
// Alert ("getxlsfromtbl ");
If (intblid! = NULL & intblid! = "" & Intblid! = "Null "){
Curstr = gettbldata (intblid, inwindow );
}
If (curstr! = NULL ){
Allstr + = curstr;
}
Else {
Alert ("the table you want to export does not exist! ");
Return;
}
VaR filename = getexcelfilename ();
Dofileexport (filename, allstr );
}
Catch (e ){
Alert ("Export exception:" + E. Name + "->" + E. Description + "! ");
}
}
Function gettbldata (intbl, 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 curtbl = tbldocument. getelementbyid (intbl );
VaR outstr = "";
If (curtbl! = NULL ){
For (var j = 0; j <curtbl. Rows. length; j ++ ){
// Alert ("J is" + J );
For (VAR I = 0; I <curtbl. Rows [J]. cells. length; I ++ ){
// Alert ("I is" + I );
If (I = 0 & rows> 0 ){
Outstr + = "/t ";
Rows-= 1;
}
Outstr + = curtbl. Rows [J]. cells [I]. innertext + "/t ";
If (curtbl. Rows [J]. cells [I]. colspan> 1 ){
For (var k = 0; k <curtbl. Rows [J]. cells [I]. colspan-1; k ++ ){
Outstr + = "/t ";
}
}
If (I = 0 ){
If (rows = 0 & curtbl. Rows [J]. cells [I]. rowspan> 1 ){
Rows = curtbl. Rows [J]. cells [I]. rowspan-1;
}
}
}
Outstr + = "/R/N ";
}
}
Else {
Outstr = NULL;
Alert (intbl + "does not exist! ");
}
Return outstr;
}
Function getexcelfilename (){
VaR d = new date ();
VaR curyear = D. getyear ();
VaR curmonth = "" + (D. getmonth () + 1 );
VaR curdate = "" + D. getdate ();
VaR curhour = "" + D. gethours ();
VaR curminute = "" + D. getminutes ();
VaR cursecond = "" + D. getseconds ();
If (curmonth. Length = 1 ){
Curmonth = "0" + curmonth;
}
If (curdate. Length = 1 ){
Curdate = "0" + curdate;
}
If (curhour. Length = 1 ){
Curhour = "0" + curhour;
}
If (curminute. Length = 1 ){
Curminute = "0" + curminute;
}
If (cursecond. Length = 1 ){
Cursecond = "0" + cursecond;
}
VaR filename = "leo_zhang" + "_" + curyear + curmonth + curdate + "_"
+ Curhour + curminute + cursecond + ". CSV ";
// Alert (filename );
Return filename;
}
Function dofileexport (inname, instr ){
VaR xlswin = NULL;
If (!! Document. All ("glbhidefrm ")){
Xlswin = glbhidefrm;
}
Else {
VaR width = 6;
VaR Height = 4;
VaR openpara = "Left =" + (window. Screen. width/2-width/2)
+ ", Top =" + (window. Screen. Height/2-height/2)
+ ", Scrollbars = No, width =" + width + ", Height =" + height;
Xlswin = Window. Open ("", "_ blank", openpara );
}
Xlswin.doc ument. Write (instr );
Xlswin.doc ument. Close ();
Xlswin.document.exe ccommand ('saveas', true, inname );
Xlswin. Close ();
}

// --> </MCE: SCRIPT>
</Body>
</Html>

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.