Silent Excel Spreadsheet XLS Printing with Pages Range Settings from Javascript
Product JSPrintManager Published 05/18/2020 Updated 08/01/2022 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
Follow up these steps
-
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! -
HTML & Script References
- Download JSPrintManager.js
- Copy all of these *.js files to the same folder where the next
index.html
page 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:
<!XLSTYPE 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><strong>XLS File to print</strong></legend> <label> Local XLS file: <input id="input-local-xls-file" type="file" /> </label> <br /><br /> <strong>OR...</strong> <br /><br /> <label> XLS File from URL <span> (e.g. <a href="https://neodynamic.com/temp/Project-Scheduling-Monitoring-Tool.xls" target="_blank"> https://neodynamic.com/temp/Project-Scheduling-Monitoring-Tool.xls </a>) </span> <input id="input-file-url" /> </label> </fieldset> <br /><br /> <fieldset> <legend><strong>Target Printer</strong></legend> <label>Printer:</label> <select id="printerName"> </select> <label>Page From:</label> <input type="text" id="pageFrom" /> <label>Page To:</label> <input type="text" id="pageTo" /> </fieldset> <br /> <button onclick="doPrinting();"><h2>Print XLS Now...</h2></button> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.5/bluebird.min.js"></script> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="scripts/JSPrintManager.js"></script> <script> var clientPrinters = null; var _this = this; //WebSocket settings JSPM.JSPrintManager.auto_reconnect = true; JSPM.JSPrintManager.start(); JSPM.JSPrintManager.WS.onStatusChanged = function () { if (jspmWSStatus()) { //get client installed printers JSPM.JSPrintManager.getPrinters().then(function (printersList) { clientPrinters = printersList; var options = ''; for (var i = 0; i < clientPrinters.length; i++) { options += '<option>' + clientPrinters[i] + '</option>'; } $('#printerName').html(options); }); } }; //Check JSPM WebSocket status function jspmWSStatus() { if (JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Open) return true; else if (JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Closed) { console.warn('JSPrintManager (JSPM) is not installed or not running! Download JSPM Client App from https://neodynamic.com/downloads/jspm'); return false; } else if (JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Blocked) { alert('JSPM has blocked this website!'); return false; } } //Do printing... function doPrinting() { if (jspmWSStatus()) { //create ClientPrintJob var cpj = new JSPM.ClientPrintJob(); //Set Printer info var myPrinter = new JSPM.InstalledPrinter($('#printerName').val()); cpj.clientPrinter = myPrinter; //Set XLS file var local_file = $("#input-local-xls-file").prop('files'); var my_file = null; if (local_file && local_file.length > 0) { my_file = new JSPM.PrintFileXLS(local_file[0], JSPM.FileSourceType.BLOB, local_file[0].name, 1); } else if ($("#input-file-url").val().length > 0) { my_file = new JSPM.PrintFileXLS($("#input-file-url").val(), JSPM.FileSourceType.URL, "myFileToPrint.xls", 1); } else { alert('Must specify a local file or a URL!'); return; } //set file printing settings... my_file.pageFrom = parseInt($('#pageFrom').val()); my_file.pageTo = parseInt($('#pageTo').val()); //add file to ClientPrintJob cpj.files.push(my_file); //Send print job to printer! cpj.sendToClient(); } } </script> </body> </html>
- That's it! Run your website and test it.