简体   繁体   中英

Save Excel file via web page using java script

var myApp = new ActiveXObject("Excel.Application");
                myApp.visible = true;
                var myWorkbook;             
            var xlCellTypeLastCell = 11;
                myObject = new ActiveXObject("Scripting.FileSystemObject");
                    if(myObject.FileExists("xyz.xlsx")){
                myWorkbook = myApp.Workbooks.Open("xyz.xlsx");
                                }
                else{

                    myWorkbook=myApp.Workbooks.Add();
                    }

                var myWorksheet = myWorkbook.Worksheets(1);

                myWorksheet.Activate;

                objRange = myWorksheet.UsedRange;

                objRange.SpecialCells(xlCellTypeLastCell).Activate ;

                newRow = myApp.ActiveCell.Row + 1;

                strNewCell = "A" + newRow;

                myApp.Range(strNewCell).Activate;

                myWorksheet.Cells(newRow,1).value=document.getElementById("table1").rows[1].cells.item(0).innerHTML;                myWorksheet.Cells(newRow,2).value=document.getElementById("table1").rows[1].cells.item(4).innerHTML;    
        myWorksheet.Cells(newRow,3).value=document.getElementById("table1").rows[1].cells.item(5).innerHTML;

                myWorkbook.SaveAs("xyz.xlsx");

                myApp.Quit();

The above code saves the excel for the first time, in the second loop it pops the window asking to save manually, i am trying to save the file automatically without manual intervention.

You need to switch off display alerts temporarily. This is in VBA. Change it to use myApp

Application.DisplayAlerts = False
' save here
Application.DisplayAlerts = True

The DisplayAlerts documentation is short and worth reading: https://msdn.microsoft.com/en-us/library/office/ff839782.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM