Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have an ASP.Net website which lets the user write to an excel spreadsheet. The values I wish to write are calculated by a client-side Javascript code. As many of you know, I have submitted multiple questions about problems to do with Excel.

I think I may have finally found a solution to my dilemma by writing to Excel using client-side Javascript. I found a library called Excel-JS which has many of the features I am looking for. However, I reached a snag because I'm not sure how to open an existing Excel spreadsheet. I followed the guide in the documentation on npm, but the approach hasn't worked. For context, the spreadsheet I am talking about is accessible to all users and is stored on the website directly under the main directory.

Here is my javascript code:
var Excel = require('exceljs');
var openExcel() {
    var workbook = new Excel.Workbook("../Erics_takeoff_101718.xlsx");
    var worksheet = workbook.getWorksheet('PLAN # 1');
    workbook.views = [
        {
            x: 0, y: 0, width: 100, height: 200,
            firstSheet: 0, activeTab: 1, visibility: 'visible'
        }
    ]

    worksheet.state = 'show';
    return false;
}

What changes do I need to make?

What I have tried:

I have tried embedding the spreadsheet on the website, but I was quickly shut down in that endeavor due to a misunderstanding of how servers and browsers work together.
I have also tried writing to excel with EPPlus, but again there was a disconnect between the client and server.
Posted
Updated 18-Oct-18 7:09am
v2
Comments
MadMyche 18-Oct-18 14:33pm    
Is there any reason that you are not using ASP.NET to open/read the worksheet and passing that to the browser?
Member 13396929 18-Oct-18 15:47pm    
The Javascript script is part of the ASP.NET application
Also, I haven't been able to open and write to the worksheet on the browser
MadMyche 19-Oct-18 9:32am    
I don't think you understand that I meant server vs client. You can use the Microsoft.Office.Interop namespaces for near complete automation.

Javascript is a browser component and in all reality does not rely on ASP.NET. You could do the exact same thing in PHP, CFM or in plain static HTML.
Nathan Minier 18-Oct-18 14:52pm    
This looks like it's meant to work with xlsx files programatically. You need to use the loader to import the data, then iterate to provide results to your view. The code you have is to configure the workbook for how Excel will render it.

It looks like it's up to you how you're going to handle values supplied by the script.

// read from a file
var workbook = new Excel.Workbook();
workbook.xlsx.readFile(filename)
.then(function() {
var worksheet = workbook.getWorksheet('PLAN # 1');
worksheet.eachRow(function(row, rowNumber) {
console.log('Row ' + rowNumber + ' = ' + JSON.stringify(row.values));
});
});
Member 13396929 18-Oct-18 15:48pm    
By opening the spreadsheet I mean making it show up on the screen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900