Silent Excel Spreadsheet XLS Printing with Pages Range Settings from Javascript
Product JSPrintManager Published 05/18/2020 Updated 11/05/2026 Author Neodynamic
Overview
In this walkthrough, you'll learn how to silently print XLS/x files from any web page (written on top of any Web Platform including ASP.NET MVC/CORE, PHP, Django, Ruby On Rails (RoR), Express.js, Angular, React, Vue) directly to the client printer without displaying any print dialog and by writing pure Javascript code. You'll be able to print XLS/x files to the Default client printer as well as to any other installed printer at the client machine with advanced settings like Pages Range!
- Available for Windows clients only
- Microsoft Excel 97+ must be installed at the client machine
- Spreadsheet files can be any of these file formats: *.xl, *.xlsx, *.xlsm, *.xlsb, *.xlam, *.xltx, *.xltm, *.xls, *.xla, *.xlt, *.xlm, *.xlw and *.ods
Step-by-step
-
JSPrintManager Client App
Be sure you install in your dev machine JSPrintManager (JSPM) (Available for Windows, Linux, Raspberry Pi & Mac)
IMPORTANT: This small app must be installed on each client!
Since Chrome 142, browsers ask the user to allow Local Network Access the first time your website connects to the JSPrintManager App. -
HTML & Script References
- Download JSPrintManager.js
- Copy all of these *.js files to the same folder where the next
index.htmlpage sample will be located in. - By using your favorite Web Development IDE or Text Editor, create a new HTML file naming it
index.html
Copy/paste the following code inside that html file:
<!DOCTYPE html> <html> <head> <title>JSPrintManager</title> <meta charset="utf-8" /> </head> <body> <div style="text-align:center"> <h1>Advanced XLS Printing from Javascript</h1> <hr /> <fieldset> <legend>XLS file to print</legend> <label>Local file: <input type="file" id="localFile" accept=".xls,.xlsx,.xlsm,.xlsb,.xltx,.xltm,.xlt,.ods" /></label> <p>or...</p> <label>File URL (must include the file extension): <input type="text" id="fileUrl" size="50" value="https://neodynamic.com/temp/Project-Scheduling-Monitoring-Tool.xls" /></label> </fieldset> <fieldset> <legend>Target printer</legend> <label>Printer: <select id="installedPrinterName"></select></label> </fieldset> <fieldset> <legend>XLS printing settings</legend> <label>From page: <input type="number" id="txtPageFrom" value="0" min="0" /></label> <label>To page: <input type="number" id="txtPageTo" value="0" min="0" /></label> <small>(0 = all pages)</small> </fieldset> <br /><br /> <button type="button" onclick="doPrinting();">Print Now...</button> </div> <!-- JSPrintManager.js (download it or install it with: npm install jsprintmanager) --> <script src="JSPrintManager.js"></script> <script> // Start the connection with the JSPrintManager Client App JSPM.JSPrintManager.auto_reconnect = true; JSPM.JSPrintManager.start(); JSPM.JSPrintManager.WS.onStatusChanged = function () { if (jspmWSStatus()) { // get the client installed printers JSPM.JSPrintManager.getPrinters().then(function (printersList) { fillSelect('installedPrinterName', printersList); }); } }; // Do printing... function doPrinting() { if (jspmWSStatus()) { // Create a ClientPrintJob var cpj = new JSPM.ClientPrintJob(); // Set the installed printer cpj.clientPrinter = new JSPM.InstalledPrinter(val('installedPrinterName')); // Set the XLS file and its printing settings var myXlsFile = getFileToPrint(JSPM.PrintFileXLS, 'myFileToPrint.xls'); myXlsFile.pageFrom = parseInt(val('txtPageFrom')); myXlsFile.pageTo = parseInt(val('txtPageTo')); cpj.files.push(myXlsFile); // Trace the print job status (optional) traceJob(cpj); // Send the print job to the client printer cpj.sendToClient(); } } // Check the connection status with the JSPrintManager Client App function jspmWSStatus() { var status = JSPM.JSPrintManager.websocket_status; if (status == JSPM.WSStatus.Open) { return true; } else if (status == JSPM.WSStatus.WaitingForUserResponse) { console.info('Waiting for the user to allow this website (JSPrintManager dialog or browser Local Network Access prompt)...'); } else if (status == JSPM.WSStatus.Blocked) { alert('JSPrintManager has blocked this website!'); } else if (status == JSPM.WSStatus.NotInstalled || status == JSPM.WSStatus.Closed) { alert('JSPrintManager Client App is not installed or not running! Download it from https://neodynamic.com/downloads/jspm'); } else if (status == JSPM.WSStatus.CertificateError) { alert('A TLS/SSL certificate error prevented the connection with JSPrintManager Client App.'); } else if (status == JSPM.WSStatus.ConnectionError) { alert('A network error (e.g. a firewall) prevented the connection with JSPrintManager Client App.'); } return false; } // Create the file to print from the selected local file or from the specified URL function getFileToPrint(FileClass, urlFileName) { var localFile = document.getElementById('localFile').files[0]; if (localFile) return new FileClass(localFile, JSPM.FileSourceType.BLOB, localFile.name, 1); return new FileClass(val('fileUrl'), JSPM.FileSourceType.ExternalURL, urlFileName, 1); } // Log the print job status updates (optional) function traceJob(cpj) { cpj.onUpdated = function (data) { console.info(data); }; cpj.onFinished = function (data) { console.info(data); }; cpj.onError = function (data, isCritical) { console.error(data); }; } // Helpers function val(id) { return document.getElementById(id).value; } function checked(id) { return document.getElementById(id).checked; } // Fill a <select> element with a list of items function fillSelect(id, items, getText) { var select = document.getElementById(id); select.innerHTML = ''; for (var i = 0; i < items.length; i++) { var text = getText ? getText(items[i]) : items[i]; select.add(new Option(text, text)); } } </script> </body> </html>
- That's it! Run your website and test it.
Useful? Share it!
| Tweet |
|
Related Articles
Silent Word DOC Printing with Duplex, Reverse & Pages Range Settings from JavascriptAdvanced Silent PDF Printing with Tray, Paper Size, Rotation & Pages Range Settings from Javascript
How to trace Print Job Status from Javascript
Need help? Contact Support
We provide best-in-class customer service and support directly from members of our dev team! If we are available when you contact us, you will get a response in few minutes; otherwise the maximum turnaround is 24hs in most cases.