# Excel IO Element

## Content

Using the Excel IO element in SpreadJS with Angular, you can quickly render and display the Excel sheets on the webpages while also executing the import and export operations without any hassle.

1. **Install the Angular CLI globally**
    Open the **Command Prompt** window and type the following command:

    ```Shell
    npm install -g @angular/cli
    ```
2. **Create a new project using Angular CLI**
    Create a new project using the following command and navigate to the project directory:

    ```Shell
    ng new spread-sheets-angular-cli --style css --ssr false
    cd spread-sheets-angular-cli
    ```
3. **Install SpreadJS NPM package**
    Install the SpreadJS Npm package using the following commands:

    ```Shell
    npm install @mescius/spread-sheets
    npm install @mescius/spread-sheets-angular
    ```
4. **Install Excel IO Angular Module and FileSaver Angular Module in the Project**
    Install the Excel I/O and file-saver modules in your project using the following commands:

    ```Shell
    npm install @mescius/spread-excelio
    npm install file-saver --save
    npm install @types/file-saver --save
    ```
5. **Configure SpreadJS CSS**
    Configure the SpreadJS CSS in **angular.json** as shown below:

    ```JSON
    {
      ...
      "projects":{
        "spread-sheets-angular-cli":{
          ...
          "architect":{
            ...
            "build":{
              ...
              "options":{
                ...
                "styles": [
                  "node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"
                ],
                ...
              }
              ...
            }
            ...
          }
          ...
        }
      }
      ...
    }
    ```
6. **Use Excel IO in an Angular application**
    Modify **app.component.html** for viewing the SpreadJS component as shown below:

    ```HTML
    <div class='maincontainer'>
      <gc-spread-sheets [hostStyle]="hostStyle" (workbookInitialized)="workbookInit($event)">
      </gc-spread-sheets>
      <div class='loadExcelInput'>
        <p>Open Excel File</p>
        <input type="file" name="files[]" multiple id="jsonFile" accept=".xlsx" (change)="onFileChange($event)" />
      </div>
      <div class='exportExcel'>
        <p>Save Excel File</p>
        <button (click)="onClickMe($event)">Save Excel!</button>
      </div>
    </div>
    ```

    Modify **app.component.ts** for using Excel IO element and preparing data for SpreadJS component as shown below:

    ```TypeScript
    import { Component } from '@angular/core';
    import { SpreadSheetsModule } from "@mescius/spread-sheets-angular";
    import * as GC from '@mescius/spread-sheets';
    import * as Excel from '@mescius/spread-excelio';
    import { saveAs } from 'file-saver';
    
    // LICENSE INFORMATION
    var SpreadJSKey = "xxxx";
    GC.Spread.Sheets.LicenseKey = SpreadJSKey;
    
    // Need to set SpreadJS Key to EXCELIO also
    // The GC needs to be mounted on a global object
    // So that excelIO will get the license through the global GC
    (window as any).GC = GC;
    @Component({
      selector: 'app-root',
      standalone: true,
      imports: [SpreadSheetsModule],
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      spreadBackColor = 'aliceblue';
      hostStyle = {
        width: '95vw',
        height: '80vh'
      };
      private spread!: GC.Spread.Sheets.Workbook;
      private excelIO;
      constructor() {
        this.excelIO = new Excel.IO();
      }
      workbookInit(args: { spread: GC.Spread.Sheets.Workbook; }) {
        const self = this;
        self.spread = args.spread;
        const sheet = self.spread.getActiveSheet();
        sheet.getCell(0, 0).text('Test ExcelIO').foreColor('blue');
      }
      onFileChange(args: any) {
        const self = this, file = args.srcElement && args.srcElement.files && args.srcElement.files[0];
        if (self.spread && file) {
          self.excelIO.open(file, (json: Object) => {
            self.spread.fromJSON(json, {});
            setTimeout(() => {
              alert('Excel loaded successfully');
            }, 0);
          }, (error: any) => {
            alert('load fail');
          });
        }
      }
      onClickMe(args: any) {
        const self = this;
        const filename = 'ExportedExcel.xlsx';
        const json = JSON.stringify(self.spread.toJSON());
        self.excelIO.save(json, function (blob: any) {
          saveAs(blob, filename);
        }, function (e: any) {
          console.log(e);
        });
      }
    }
    ```
7. **Build and run the project**
    Build and run the new project using the following command:

    ```Shell
    ng serve
    ```