Js import export excel of instance code

  • 2020-03-29 23:57:08
  • OfStack

Import:


<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
     <title>Untitled Page</title>
</head>
<script language="javascript" type="text/javascript">
function importXLS(fileName)
{  
     objCon = new ActiveXObject("ADODB.Connection");
     objCon.Provider = "Microsoft.Jet.OLEDB.4.0";
     objCon.ConnectionString = "Data Source=" + fileName + ";Extended Properties=Excel 8.0;";
     objCon.CursorLocation = 1;
     objCon.Open;
     var strQuery;
     //Get the SheetName
     var strSheetName = "Sheet1$";
     var rsTemp =   new ActiveXObject("ADODB.Recordset");
     rsTemp = objCon.OpenSchema(20);
     if(!rsTemp.EOF)
     strSheetName = rsTemp.Fields("Table_Name").Value;
     rsTemp = null;
     rsExcel =   new ActiveXObject("ADODB.Recordset");
     strQuery = "SELECT * FROM [" + strSheetName + "]";
     rsExcel.ActiveConnection = objCon;
     rsExcel.Open(strQuery);
     while(!rsExcel.EOF)
     {
     for(i = 0;i<rsExcel.Fields.Count;++i)
     {
     alert(rsExcel.Fields(i).value);
     }
     rsExcel.MoveNext; 
     }
     // Close the connection and dispose the file
     objCon.Close;
     objCon =null;
     rsExcel = null;
}
</script>
</head>
<body>
<input type="file" id="f" />
<input type="button" id="b" value="import" onclick="if(f.value=='')alert(' Please select a xls file ');else importXLS(f.value)" />
</body>
</html> 

Export:

function AutomateExcel()
{
  
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
  
oXL.Visible = true;
  
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
  
// Add table headers going cell by cell.
oSheet.Cells(1, 1).Value = "First Name";
oSheet.Cells(1, 2).Value = "Last Name";
oSheet.Cells(1, 3).Value = "Full Name";
oSheet.Cells(1, 4).Value = "Salary";
  
// Format A1:D1 as bold, vertical alignment = center.
oSheet.Range("A1", "D1").Font.Bold = true;
oSheet.Range("A1", "D1").VerticalAlignment = -4108; //xlVAlignCenter
  
// Create an array to set multiple values at once.
  
// Fill A2:B6 with an array of values (from VBScript).
oSheet.Range("A2", "B6").Value = CreateNamesArray();
  
// Fill C2:C6 with a relative formula (=A2 & " " & B2).
var oRng = oSheet.Range("C2", "C6");
oRng.Formula = "=A2 & " " & B2";
  
// Fill D2:D6 with a formula(=RAND()*100000) and apply format.
oRng = oSheet.Range("D2", "D6");
oRng.Formula = "=RAND()*100000";
oRng.NumberFormat = "$0.00";
  
// AutoFit columns A:D.
oRng = oSheet.Range("A1", "D1");
oRng.EntireColumn.AutoFit();
  
// Manipulate a variable number of columns for Quarterly Sales Data.
DispalyQuarterlySales(oSheet);
  
// Make sure Excel is visible and give the user control
// of Excel's lifetime.
oXL.Visible = true;
oXL.UserControl = true;
}<HTML>
<HEAD>
<TITLE> Imports the data from the specified table on the page into Excel In the </TITLE>
<SCRIPT LANGUAGE="javascript">
<!--
function AutomateExcel()
{
  
var oXL = new ActiveXObject("Excel.Application"); //Create should object
var oWB = oXL.Workbooks.Add();//Create a new Excel workbook
var oSheet = oWB.ActiveSheet;//Specifies that the worksheet to write to is the active worksheet
var table = document.all.data;//Specify the id of the data source to write to
var hang = table.rows.length;//Take the number of rows from the data source
var lie = table.rows(0).cells.length;//Takes the number of data source columns
  
// Add table headers going cell by cell.
for (i=0;i<hang;i++){//Write a line in Excel
for (j=0;j<lie;j++){//Write columns in Excel
//Define the format
oSheet.Cells(i+1,j+1).NumberFormatLocal = "@";
//!!!!!!!!!!!!!!!!!!! The above sentence defines the format of the cell as text
oSheet.Cells(i+1,j+1).Font.Bold = true;// bold 
oSheet.Cells(i+1,j+1).Font.Size = 10;//The font size
oSheet.Cells(i+1,j+1).value = table.rows(i).cells(j).innerText;//Writes a value to the cell
}
}
oXL.Visible = true;
oXL.UserControl = true;
}
//-->
</SCRIPT>
</HEAD>
  
<BODY>
<table border="0" width="300" id="data" bgcolor="black" cellspacing="1">
<tr bgcolor="white">
<td> Serial number </td>
<td> The name </td>
<td> age </td>
<td> gender </td>
</tr>
<tr bgcolor="white">
<td>0001</td>
<td> Zhang SAN </td>
<td>22</td>
<td> female </td>
</tr>
<tr bgcolor="white">
<td>0002</td>
<td> Li si </td>
<td>23</td>
<td> male </td>
</tr>
</table>
<input type="button" name="out_excel" onclick="AutomateExcel();" value=" Export to excel">
</BODY>
</HTML>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<!--  export excle Three ways   To put ie The browser " For those not marked as safe  ActiveX  Control to initialize and script run   Set to prompt or enable " --> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
    <title>WEB Page export as EXCEL Method of documentation </title> 
</head> 
<body> 
    <table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0"> 
        <tr> 
            <td colspan="5" align="center"> 
                WEB Page export as EXCEL Method of documentation  
            </td> 
        </tr> 
        <tr> 
            <td> 
                 Column headings 1 
            </td> 
            <td> 
                 Column headings 2 
            </td> 
            <td> 
                 Column headings 3 
            </td> 
            <td> 
                 Column headings 4 
            </td> 
            <td> 
                 Column headings 5 
            </td> 
        </tr> 
        <tr> 
            <td> 
                aaa 
            </td> 
            <td> 

            </td> 
            <td> 
                ccc 
            </td> 
            <td> 
                ddd 
            </td> 
            <td> 
                eee 
            </td> 
        </tr> 
        <tr> 
            <td> 
                AAA 
            </td> 
            <td> 

            </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=" The first method is imported into EXCEL"> 
    <input type="button" onclick="javascript:method2('tableExcel');" value=" The second method is imported into EXCEL"> 
    <input type="button" onclick="javascript:getXlsFromTbl('tableExcel',null);" value=" The third method is imported into EXCEL"> 
    <script language="javascript"> 
        function method1(tableid) {//Copy the entire table to EXCEL
            var curTbl = document.getElementById(tableid); 
            var oXL = new ActiveXObject("Excel.Application"); //Create the AX object excel
            var oWB = oXL.Workbooks.Add(); //Gets the workbook object
            var oSheet = oWB.ActiveSheet; //Activate the current sheet
            var sel = document.body.createTextRange(); 
            sel.moveToElementText(curTbl); //Move the contents of the table to TextRange
            sel.select(); //Select everything in TextRange
            sel.execCommand("Copy"); //Copy from TextRange
            oSheet.Paste(); //Paste into the EXCEL of the activity
            oXL.Visible = true; //Set the excel visibility properties
        } 
        function method2(tableid) //Read each cell in the table into EXCEL
        { 
            var curTbl = document.getElementById(tableid); 
            var oXL = new ActiveXObject("Excel.Application"); //Create the AX object excel
            var oWB = oXL.Workbooks.Add(); //Gets the workbook object
            var oSheet = oWB.ActiveSheet; //Activate the current sheet
            var Lenr = curTbl.rows.length; //Gets the number of rows in the table
            for (i = 0; i < Lenr; i++) { 
                var Lenc = curTbl.rows(i).cells.length; //Gets the number of columns per row
                for (j = 0; j < Lenc; j++) { 
                    oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText; // The assignment  
                } 
            } 
            oXL.Visible = true; //Set the excel visibility properties
        } 
        function getXlsFromTbl(inTblId, inWindow) { 
            try { 
                var allStr = ""; 
                var curStr = ""; 
                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 abnormal :" + e.name + "->" + e.description + "!"); 
            } 
        } 
        function getTblData(inTbl, inWindow) { 
            var rows = 0; 
            var tblDocument = document; 
            if (!!inWindow && inWindow != "") { 
                if (!document.all(inWindow)) { 
                    return null; 
                } 
                else { 
                    tblDocument = eval(inWindow).document; 
                } 
            } 
            var curTbl = tblDocument.getElementById(inTbl); 
            var outStr = ""; 
            if (curTbl != null) { 
                for (var j = 0; j < curTbl.rows.length; j++) { 
                    for (var i = 0; i < curTbl.rows[j].cells.length; 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 + " There is no !"); 
            } 
            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"; 
            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.document.write(inStr); 
            xlsWin.document.close(); 
            xlsWin.document.execCommand('Saveas', true, inName); 
            xlsWin.close(); 
        } 
    </script> 
</body> 
</html>


Related articles: