Begin composing in Excel

I am looking to manipulate an existing Excel file by writing values from an array and closing it all on the client side. Here is the code snippet I have been using:

for (var c=0; c<arrMCN.length; c++)
{
var mcnCreate = arrMCN[c];

var mcnNumber =mcnCreate.getProperty("agu2a_nb");
var mcnType=mcnCreate.getProperty("pdm_ecn_type");
var mcnProjectCode=mcnCreate.getProperty("pdm_project_code");
var mcnState=mcnCreate.getProperty("state");

var Excel = new ActiveXObject("Excel.Application");
Excel.Visible = true;
Excel.Workbooks.Open("C:\\test.xls");
Excel.Activesheet.Cells(1,1).Value = mcnNumber;
var data =  Excel.Activesheet.Cells(1,1).Value;
return data;

}

Encountering the "ActiveX component cannot create an object" error was a setback for me initially. However, after tweaking the security settings in the Internet options, the error was resolved.

After successfully writing just one value into the Excel file, my next goal is to figure out how to efficiently write all the data from the array into the spreadsheet.

Any insights or suggestions on this matter would be greatly appreciated!

Answer №1

   var Excel = new ActiveXObject("Excel.Application");
   Excel.Visible = true;
   Excel.Workbooks.Open("C:test.xls");
   var excel_sheet = excel_file.Worksheets("Sheet11");
   var data = excel_sheet.Cells(1,1).Value = create;
   return data;

Make sure the URL was called properly:C:test.xls You can confirm using some Alert Function

Answer №2

Below is the effective solution I found for my query.

let ExcelApp = new ActiveXObject("Excel.Application");
let fileSystemObj = new ActiveXObject("Scripting.FileSystemObject");
let checkFileExistence = fileSystemObj.FileExists("C:\\Report.xlsx");

if(checkFileExistence) {
    fileSystemObj.DeleteFile("C:\\Report.xlsx", true);
}

let ExcelSheet = new ActiveXObject("Excel.Sheet");
let offset = 0;
let row = 2;

for (let index = 0; index < arrToExcel.length; index++) {
    let createExcel = arrToExcel[index];
    let NumberValue = createExcel.getProperty("number");
    let TypeValue = createExcel.getProperty("type");
    let CodeValue = createExcel.getProperty("code");

    ExcelSheet.ActiveSheet.Range("A" + (row + 1 + offset)).Value = NumberValue;
    ExcelSheet.ActiveSheet.Range("B" + (row + 1 + offset)).Value = TypeValue;
    ExcelSheet.ActiveSheet.Range("C" + (row + 1 + offset)).Value = CodeValue;

    row += 1;
}

offset += 1;
ExcelApp.DisplayAlerts = false;
ExcelSheet.SaveAs("C:\\EcnExcelReport.xlsx");
ExcelSheet.Application.Quit();
ExcelApp.DisplayAlerts = true;

A big thank you to everyone who provided suggestions.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to incorporate the .then method in a promise?

I'm currently working on some JS Express code that looks like this: app.get('/lists2', (req, res) => { mongo.getDB() .then(db => db.collection('dogs')) .then(collection => collection.find().toArray()) .then ...

Can mouseenter and mouseleave events be implemented in Chart.js?

Currently, I am using the onHover function on each pie to implement some scale/zoom effect. However, I would like to switch to using mouseenter and mouseleave. When there is a mouseenter event on a pie, it should enlarge with scale/zoom effect, and when th ...

Tips for retrieving the count from HTML using JavaScript:

I need to determine the count of list items in an unordered list within dir-pagination-controls. How can I achieve this using JavaScript? <dir-pagination-controls min-size="1" direction-links="true" boundary-links="true" class="pull-right ng-isolate- ...

Is Valums Ajax file Upload capable of handling the uploaded file?

Currently, I am utilizing the Valums Ajax Fileupload script found at These are the settings I have configured: function createUploader(){ var uploader = new qq.FileUploader({ element: document.getElementById('file-uploader-de ...

The error message that keeps popping up is saying that it cannot access the property "username" as it is undefined

signup.js const SignupForm = () => { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const handleSignup = () => { fetch("/signup", { method: "post", header ...

Beware: The use of anonymous arrow functions in Next.js can disrupt Fast Refresh and lead to the loss of local component state

I am currently encountering a warning that is indicating an anonymous object in a configuration file, and even specifying a name for it does not resolve the warning. Below you will find the detailed warning message along with examples. Warning: Anonymous ...

Avoid running another process before the current one finishes in jQuery

I have a situation where I am using $.ajax inside a for loop in jQuery. for(var i=0; i < 2; i++) { $.ajax({ url :"/models/getdata"+i, dataType:"json", success:function(data) { } }); } The issue is that before the success function for i=0 completes, ...

Sorting alphanumeric strings in React Bootstrap Table Next on a dynamic table

I am facing an issue with sorting columns in a dynamic table with over 70 columns using React-Bootstrap-Table-Next. The problem arises when trying to sort the columns in alphanumerical order, as some columns contain numbers and others contain letters. The ...

What is the method for retrieving the name of the currently selected HTML element?

After using jQuery to select certain tags, I am now trying to obtain the name of each tag: $('select, :checkbox, :radio').each(function(){ // ... }); To accomplish this, I have attempted the following code: $('select, :checkbox, :radio ...

Navigating a roster of users in JavaScript

I'm dealing with a collection of user data stored in an array accountsade: [ { id: 0.4387810413935975, name: "Adrian", password: "345", userName: "nathanael" }, { id: 0. ...

React does not allow objects as child elements. Instead, render a collection of children by using an array

Encountering an error with this React class Error: Objects are not valid as a React child (found: object with keys {_id, name}). If you meant to render a collection of children, use an array instead. Is there anything amiss here? I am passing the movies ...

Masking Aadhaar numbers

I need to find a way to detect when the back button is pressed on an input field. The methods I have tried, e.key and e.which, are returning as undefined in mobile Chrome. How can I make this work? It's functioning properly on desktop. jQuery(funct ...

Protractor is able to achieve successful test results without actually executing the tests

SYMPTOMS When running Protractor, the tests pass successfully, but the pages do not load. Instead, they appear blank with "data:text/html" in the address bar (refer to the screenshot). Although the tests show as passed, there are 0 assertions being made. ...

Issue with PHP's ng-click not functioning properly with Angular JS's dynamic HTML generation

I'm just starting out with Angular JS. I'm trying to create an ng-click event in dynamically generated HTML from PHP, here's the code snippet. var app = angular.module('pageapp',[]); angular.module('pageapp') .filter(& ...

Tips for transferring the ID from one element to another using JavaScript

I'm attempting to create a slideshow using icons all within one class. The "active" style class will have an ID and represent the current picture. By clicking either the left or right button, I aim to change the style of the active class. const l ...

Can you import folders containing *.vue files in your project?

Repeating code is my least favorite thing. Currently, I am importing vue files in a repetitive manner in my main.js file. import Default from '../../src/components/default.vue'; import Home from '../../src/components/home.vue&apo ...

The Canvas Clear function is malfunctioning; the items in the array are not being deleted

Even after being deleted from the array, the squares are still drawn. Shouldn't they disappear when removed from the Array? Does the array not update within the go function? Javascript: var canvas; var ctx; $(document).ready(function(){ $("#t ...

tips for navigating through an AngularJS $resource instance

I am facing a frustrating issue that I need assistance with. The problem arises when I try to extract specific data from each element of the stock data fetched by my controller from Yahoo Stocks. Although the data is stored in $scope.stocks and can be disp ...

Using jQuery to reference my custom attribute---"How to Use jQuery to reference My

Can you explain how to reference a tag using a custom attribute in jQuery? For example, if I have a tag like this: <a user="kasun" href="#" id="id1">Show More...</a> I want to reference the tag without using the id. So instead of using: $( ...

Unusual hue in the backdrop of a daisyui modal

https://i.stack.imgur.com/fLQdY.png I have tried various methods, but I am unable to get rid of the strange color in the background of the Modal. Just for reference, I am using Tailwind CSS with Daisy UI on Next.JS. <> <button className='btn ...