Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
30
Column Order
posted

I was wondering if there is a way to specify the column order in an excel spreadsheet when exported as the json output from my webservice might return the data not in the desired format. Also is there a way to format column like money etc ? Last one can the Header name be set to something else then the field name of the json doc ? for example i have a field called customer_name and i want in the header to show Names ?

Parents
  • 60
    Offline posted

    Hi Alex,

    Thank you for contacting Infragistics!

    The `IgxExcelExporterService` exports the array of objects that you've passed. To change the order, you can simply map the array with a transform function before passing it to the `exportData` method:

    const arrangeData: (obj: { [key:string] : any}) => {[key: string]: any} = (obj) => {
    //Return an object with the mapped keys in proper order
      return {
      name: obj.name,
      age: obj.age,
      uid: obj.uid
      }

    You can change the date that will be populated in the cells by subscribing to the `onRowExport` event and changing the data that you want to write:

    this.excelExportService.onRowExport.pipe(takeUntil(this.destroy$)).subscribe((args: IRowExportingEventArgs) => {
         
          args.rowData["account_balance"] = '$' + args.rowData["account_balance"].toFixed(2);
        })

    In the above example, `rowData.account_balance` is changed from `number` to `string`, with an appended dollar sign to the beginning. 

    As for the headers, you can subscribe to the `onColumnExport` event and change the header of the column that is being exported:

    this.excelExportService.onColumnExport.pipe(takeUntil(this.destroy$)).subscribe((args: IColumnExportingEventArgs) => {
          if (args.field === "uid") {
            args.header = "Unique ID";
          }
        })

    Here is a StackBlitz example - please have a look when you get the chance!

    I hope this helps!

Reply Children
  • 60
    Offline posted in reply to Alex Miller

    The IgxExcelExporterService I've used in my examples is a lightweight solution for exporting data to Excel. It handles information as strings and exports it as such. You can manipulate the date that will be populated in the spreadsheet fields to a limited extent, e.g. transforming the value before writing to the document, as shown in my example above.

    Using the IgxExcelExporterService is ideal if you want to export large amounts of string data to a .csv file, for example (as excel formatting is not applicable there).

    If you want to achieve more complex formatting and functionality, you can use the Excel Library. Under the topic, you can find example on how to set up the library and how you can use it to format cells or use worksheets in your export.

    Hope this helps!