The Good Tech Companies - Importing and Exporting Excel XLSX Using Vue: A Guide

Episode Date: June 26, 2024

This story was originally published on HackerNoon at: https://hackernoon.com/importing-and-exporting-excel-xlsx-using-vue-a-guide. Learn how to import and export Excel X...LSX files using Vue. See more from SpreadJS today. Check more stories related to programming at: https://hackernoon.com/c/programming. You can also check exclusive content about #web-development, #devops, #excel-tutorial, #javascript-development, #javascript-tutorial, #javascript-frameworks, #good-company, #importing-and-exporting-excel, and more. This story was written by: @mesciusinc. Learn more about this writer by checking @mesciusinc's about page, and for more stories, please visit hackernoon.com. Learn how to import and export Excel XLSX files in a Vue application.

Transcript
Discussion (0)
Starting point is 00:00:00 This audio is presented by Hacker Noon, where anyone can learn anything about any technology. Importing and exporting Excel XLSX using Vue, a guide, by MESCIUS Inc. It looks great, but could you just add Excel import? If you've developed software for a long time, you've probably heard that question from a manager more than once. To a non-technical crowd, asking for Excel import, export doesn't sound like a big deal. How hard could it be, right? But all too often, this question strikes fear into the hearts of developers. On most platforms, working with Excel files requires a lot of work. Historically, this has been doubly true on the web. Working with Excel data in a web app has felt a bit like
Starting point is 00:00:43 this XKCD comic. It can be hard to explain the difference between the easy and the virtually impossible. Building your own in-browser spreadsheet with Excel import and Excel export feels like a problem that will take five years and a research team to solve. That's changing. We now have turnkey libraries that let you drop a fully functioning spreadsheet into your web app. Spread.js is one of them. We'll look at how to take an existing Vue app, a real-world app using a Vuex store, and enhance it using Spread.js. The rest of the article assumes that you already understand HTML, CSS, and JavaScript. It also assumes you have a working knowledge of the Vue.js progressive JavaScript framework for creating web UI. It will help if you've used Vuex for state management, but don't worry if you haven't.
Starting point is 00:01:30 It's intuitive and easy to understand, and you'll be able to figure out what's going on just by reading the code if you've used Vue. In this blog, we will cover how to add Excel import and export to your Vue app following these steps 1. Vue application structure, 2. Add spread.js to your Vue app following these steps. 1. Vue application structure. 2. Add SpreadJS to your Vue app. 3. Add Excel export. 4. Add Excel import. 5. Test your Vue app. The Vue app. The Vue app we'll be working on is a straightforward sales dashboard with a FES summary panels and a table of data. It's the kind of app that falls into the not bad category. Although it's just a demo, it's exactly the kind of app that falls into the, not bad, category. Although it's just a demo, it's exactly the kind of app that enterprise web developers have to create. It's also exactly the kind of app that we're typically asked to add Excel functionality to,
Starting point is 00:02:14 so it will be a perfect example for the rest of this article. The code for this application can be found here. If you want to see how to create a Vue application from scratch, check out this quick start guide. To set the stage, the dashboard is a single-page application made with Vue. IDIS is the latest and greatest Vue best practices, single-file components and AVUEX data store. It's also using Bootstrap for its CSS components and grid system. Bootstrap isn't as popular as it used to be, but in reality, Bootstrap is still everywhere. Especially in enterprise web apps where Excel support is usually needed. We'd wager
Starting point is 00:02:51 that plenty of new enterprise web apps will still use Bootstrap in 2030. If you'd rather use Bulma or Tachyons in your spreadsheet-enabled apps, go right ahead. Spread.js will work well with either of them. View application structure Let's take a look at how the code is structured. Our Vuex store and Vue application are both defined in main.js. We have several single file view components, all located in the components folder. If you look at our Vuex store, you'll see the following our store's initial state is set to the value of recent sales, a set of dummy data we imported. We also have a function that handles updating the recent sales when they are changed.
Starting point is 00:03:35 Wait a minute, if we've only got one set of data, how are we generating three charts in a table? To see what's happening, open up the dashboard, view component, in it, you'll see several computed properties are being generated based on the data in the Vuex store now, it makes more sense. The single data set contains everything we need to generate all of the numbers and tables for our dashboard. Since the data is in our active Vuex store, if the data updates, then all of the dashboard panels will update automatically. This reactivity will come in handy in the next section when we replace our boring old static table with an editable spreadsheet. Adding SPREADJS to your Vue app here's where the fun starts. We have our dashboard, but we want to eliminate that clunky old HTML table. So, we'll have to change things up a little bit. We have a great starting point, but we must run our app locally to use SpreadJS in development mode without a license. You can
Starting point is 00:04:25 download the finished code to see the final result. Start by opening the original project without SpreadJS. Open a terminal, navigate to the directory where you cloned the repository, and run npm install. This installs the dependencies needed to run the application. When the dependency installation finishes, run npm serve to see the updated app in action. If you want to specifically import the libraries used, you can use this command let's walk through the changes we'll have to make to upgrade our old app to ITS new and improved version. Since we're going to replace our sales table with ASP read sheet, we will put the sheet into our existing sales table. View component, but first, we'll have to get rid of our old table. Once it is gone, our sales table template will look like
Starting point is 00:05:10 this after eliminating the table, we've got our table panel ready and waiting for ASP read sheet, so let's add one. After adding a spread JS sheet, our template will look like this that's a lot to take in, so let's walk through it to understand what is happening. First, we create a spreadsheet by using the GC Spreadsheets element and binding it to two of our component's properties, Host Class and Workbook in it. Inside the spreadsheet, we create a new worksheet with the GC Worksheet element and bind it to our component's table data and auto generate columns properties. Note that table data is exactly the same table data we used to generate our plain HTML table. We can put our data into Spread.js as is with no changes required. Finally, inside the worksheet, we define columns that tell Spread.js how to display our data.
Starting point is 00:05:57 The data field property tells us which property of the underlying dataset this column should display, and header text gives SpreadJS a nicely formatted column name to use. The rest of the bindings for each column are straightforward. The SpreadJS documentation has a complete list of everything you can pass to a GC column. So, with our template in place, how much code will be needed to make this all work? Fortunately, not much at all. Here's our sales table. View components new script code due to view's simplicity. It takes very little code to make this work. If there anything here you're unfamiliar with, the components in-depth section of the view documentation explains view components
Starting point is 00:06:36 in detail. The only things that have changed are a few imports, some data properties, and a couple of methods. The data properties should look familiar, we saw them a moment ago in the template. Their configuration options we are binding to the components in our SpreadJS spreadsheet. The workbook init method is a callback that SpreadJS calls when the sheet is initialized. In this method, we save our sheet.js spreadsheet object as an instance variable on our components so we can interact with it directly if necessary. We also added a binding function for the value changed event to automatically update data whenever values are changed in the spread js instance. One last change. We give our component a scoped style to help the spreadsheet style itself. We saw this earlier when we passed the host class to the gc spreadsheets element. Since host class is set to Spreadsheet, we're going to create a CSS class named Spreadsheet
Starting point is 00:07:30 at this point. If we make no other changes and load our dashboard, it will look like this. But wait, there's more, remember how we passed our table data to the spreadsheet without making any changes to the dataset. Now that our data is in a spreadsheet, we can edit it. What will happen if we change the value of sale number 6 from $35,000 to $3,500? If we go into the sheet and edit the value, we get a dashboard that looks like this. Wow! What happened? We updated the SpreadJS sheet, and it automatically updated our Vuex store. It also looks like Angela went from having a spectacular sales month to having a mediocre one. Sorry about that, Angela, we now have an enhanced dashboard that a manager would be happy with. They can modify the data and watch the dashboard
Starting point is 00:08:16 update before their eyes, but we can dove in better by adding the ability to import and export Excel files. Next, we learn how to do that. Adding Excel export adding Excel export to our sheet is easy. First, let's add an export button to our dashboard. We're going to place it at the bottom of our table panel in the sales table. View file. Right after the GC spread sheets closing tag as you can see, our button is expecting a click handler named export sheet. We let it in a moment, but first, we will import a function from an npm package named filesaver next. Let's add export sheet to our components methods object here's what the code is doing. First, we get a reference to our sales data sheet.
Starting point is 00:08:56 Since it's the only sheet in our spreadsheet, it lives at index 0, and we access it by calling get sheet. This can be used elsewhere in the function should we need to interact directly with the sheet. We then instantiate spread.js, Excel I.O. library, convert our sheet to JSON, and ask spread.js to save it. Voila, we've exported an Excel file from our spreadsheet-enabled Vue app. Note that we're passing two serialization options to the sheets to JSON call. Include binding source and column headers as frozen rows. Together, these options censure that the data we bound to the sheet is exported correctly and that the sheet contains our column headers.
Starting point is 00:09:35 So, looking at the exported Excel file will understand each column. Adding Excel import next, it's time to add the ability to import Excel files. Right below our export button, we're going to add the ability to import Excel files. Right below our export button, we're going to add the following bit of markup as you can see, we will use a standard HTML file picker and trigger a component method named file change when a file is selected. Now that we've added the template, let's add the change handler to our component's methods object importing an Excel file is much the same as exporting it, except in reverse.
Starting point is 00:10:11 After a file is chosen, we ask Excel I.O. to import it. When done, it passes the sheet information to a callback function as a JavaScript object. Next, we pass the imported data through a custom function to extract the data we need from ITAND then commit it back to the Vuex store. Typically, importing a file is as simple as calling that ExcelIO open method but using the workbook, from JSON, method. In this case, we just want to parse the data from the imported file and update the store, which will then update spread.js. In our extract sheet data function, which you'll find in the src.util.util.js file, you'll see that we pull data out of the JavaScript object returned by Excel I.O. and restructure it to match the shape of the data in our Vuex store.
Starting point is 00:10:52 Our import function assumes that the data in the imported sheet will have the same columns as our original dataset. If someone uploads a spreadsheet that doesn't meet this requirement, our app won't be able to handle it. This is an acceptable limitation in most line of business apps. Since our dashboard IS designed to display a specific data type, it's reasonable to ask users to supply data in the format the app expects. When data extraction is complete, we call commit on the Vuex store and send the updated sales transaction data. The spread.js sheet and the dashboard panels the end update themselves to reflect the new data. We can actually use a different mutation function for importing versus
Starting point is 00:11:30 a value being changed so that we can add that to the main.js file as import underscore recent underscore sales. Testing your Vue app now that you have seen the code, let's test out Excel import and export in our Vue app. Start by clicking the export to Excel button. Your web browser will then download an Excel spreadsheet containing all of the data we saw in our dashboard spreadsheet. Open the sheet in Excel and add a couple of lines of data. It's okay if you use new countries or new salespeople, all our dashboard components can handle it. Just be careful not to change the column order or names. When you're finished, click the Choose File button at the bottom of the Recent Sales panel.
Starting point is 00:12:11 Select the Excel file you just edited. When you select the file, you'll see the updated dashboard components. Conclusion We're done. We took an ordinary Vue dashboard app and added a live spreadsheet to it. We can now edit the data in the sheet and watch our entire dashboard update itself. Our enhanced dashboard is also able to import and export Excel files. View, Vuex, and SpreadJS complement each other well. With Vue's easy templating and data binding, Vuex's reactive datastore, and SpreadJS' interactive spreadsheets, complex enterprise JavaScript apps can be created
Starting point is 00:12:45 in hours. As great as this might seem, we've barely scratched the surface of what Spread.js can do. To better understand what Spread.js can do for you, refer to the Spread.js demos, which include full demos of the different features of Spread.js, explanations, and live code showcasing those features. If you're looking to dive deeper into using SpreadJS in your own apps, the SpreadJS documentation has the information you need. Learn more about this JavaScript spreadsheet component. http://b.gvncglcxcac.embeddable="true". Thank you for listening to this Hackernoon story, read by Artificial Intelligence. Visit hackernoon.com to read, write, learn and publish.

There aren't comments yet for this episode. Click on any sentence in the transcript to leave a comment.