31
Views
6
Comments
Solved
Excel with dynamic number of columns to be displayed
Application Type
Reactive

My requirement is that I would get an excel daily with different number of columns each time and I need to display the contents of this excel. Is there a method to convert the Excel's binary data to a record with having a preset structure you need to put this data in?

Any suggestions will be appreciated.

Thanks in Advance.

Regards,

Akash Balamurugan

2024-02-08 07-22-26
Sudeep Agarwal
Solution

Hello @Akash Balamurugan ,

To achieve the functionality you're looking for, you'll need to follow these steps:

  1. Script Inclusion: Begin by adding the necessary script to the 'Scripts' section of your project. This script will be responsible for reading the Excel file and converting its contents into JSON format.
  2. Script Requirement: Next, ensure that the script you've just included is set as a 'Required Script' in the properties of the screen where you intend to use it.
  3. Excel Upload and Button Click: Once you've uploaded the Excel file, you'll use a button to trigger the conversion process. The script below should be executed when the button is clicked.
  4. Dynamic HTML Generation: The script will read the uploaded Excel file and dynamically generate HTML to display the contents as a table on your webpage.

Here's the script you'll need to use:


````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

function convertExcelToJson(ele) {

  try {

    const input = document.querySelector("#" + ele + " > input");

    const file = input.files[0];

    const reader = new FileReader();

    reader.onload = (event) => {

      let data = new Uint8Array(reader.result);

      let work_book = XLSX.read(data, {   type: 'array'  });

      let sheet_name = work_book.SheetNames;

      let sheet_data = XLSX.utils.sheet_to_json(work_book.Sheets[sheet_name[0]], {  header: 1 });

      if (sheet_data.length > 0) {

        let table_output = '';

        for (let row = 0; row < sheet_data.length; row++) {

          table_output += '';

          for (let cell = 0; cell < sheet_data[row].length; cell++) {

            if (row == 0) {

              table_output += '' + sheet_data[row][cell] + '';

            } else {

              table_output += '' + sheet_data[row][cell] + '';

            }

          }

          table_output += '';

        }

        table_output += '';

        document.getElementById($parameters.ContainerId).innerHTML = table_output;

      }

    };

    reader.readAsArrayBuffer(file);

  } catch (error) {

    $reject(error.me)

  }

}

convertExcelToJson($parameters.File_Input)

```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

Parameters:

$parameters.File_Input: The parameter that holds the ID of the file input element.

$parameters.ContainerId: The parameter that holds the ID of the container where the table will be displayed.

Optional Styling:

You can add custom CSS classes to the generated table for styling purposes. Just modify the 

<table>, <th>, and <td> tags in the table_output string to include your class names 


Remember to replace $parameters.File_Input and $parameters.ContainerId with the actual IDs used in your project.

2025-02-10 17-24-13
Arun Rajput

Hi @Akash Balamurugan ,

You just need to show excel data only or there is search functionality and sorting as well with the data?

2024-02-15 04-20-44
Akash Balamurugan

It would be a good start to first show data only.

2025-02-10 17-24-13
Arun Rajput

for showing excel with dynamic columns I would suggest you to convert your excel sheet into HTML table then use those table directly into web page.

There are some forge component which can convert excel into html table.

Find link of them below:

https://www.outsystems.com/forge/component-overview/17889/excel2html-o11

This component built in traditional and it's created using javascript so you can use this javascript in reactive as well.


And you can use this service action-

https://www.outsystems.com/forge/component-overview/10779/excel-to-html-o11

Thanks

2022-12-30 09-46-57
Deepika Patel

Hi @Akash Balamurugan,

Please refer attached OML. You get some idea.

I have a question:  do we know all the columns from the excel or it can be any dynamic columns?

ExcelToRecords.oml
2024-02-15 04-20-44
Akash Balamurugan

It can be dynamic like for eg. one day i might get 5 columns and the day after that i could get 8 which also may include the previous days 5 or not.


In the Oml you have sent there is a entity "ExcelRecords" that is used as record definition i need to get the data with out that record definition.

2024-02-08 07-22-26
Sudeep Agarwal
Solution

Hello @Akash Balamurugan ,

To achieve the functionality you're looking for, you'll need to follow these steps:

  1. Script Inclusion: Begin by adding the necessary script to the 'Scripts' section of your project. This script will be responsible for reading the Excel file and converting its contents into JSON format.
  2. Script Requirement: Next, ensure that the script you've just included is set as a 'Required Script' in the properties of the screen where you intend to use it.
  3. Excel Upload and Button Click: Once you've uploaded the Excel file, you'll use a button to trigger the conversion process. The script below should be executed when the button is clicked.
  4. Dynamic HTML Generation: The script will read the uploaded Excel file and dynamically generate HTML to display the contents as a table on your webpage.

Here's the script you'll need to use:


````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

function convertExcelToJson(ele) {

  try {

    const input = document.querySelector("#" + ele + " > input");

    const file = input.files[0];

    const reader = new FileReader();

    reader.onload = (event) => {

      let data = new Uint8Array(reader.result);

      let work_book = XLSX.read(data, {   type: 'array'  });

      let sheet_name = work_book.SheetNames;

      let sheet_data = XLSX.utils.sheet_to_json(work_book.Sheets[sheet_name[0]], {  header: 1 });

      if (sheet_data.length > 0) {

        let table_output = '';

        for (let row = 0; row < sheet_data.length; row++) {

          table_output += '';

          for (let cell = 0; cell < sheet_data[row].length; cell++) {

            if (row == 0) {

              table_output += '' + sheet_data[row][cell] + '';

            } else {

              table_output += '' + sheet_data[row][cell] + '';

            }

          }

          table_output += '';

        }

        table_output += '';

        document.getElementById($parameters.ContainerId).innerHTML = table_output;

      }

    };

    reader.readAsArrayBuffer(file);

  } catch (error) {

    $reject(error.me)

  }

}

convertExcelToJson($parameters.File_Input)

```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

Parameters:

$parameters.File_Input: The parameter that holds the ID of the file input element.

$parameters.ContainerId: The parameter that holds the ID of the container where the table will be displayed.

Optional Styling:

You can add custom CSS classes to the generated table for styling purposes. Just modify the 

<table>, <th>, and <td> tags in the table_output string to include your class names 


Remember to replace $parameters.File_Input and $parameters.ContainerId with the actual IDs used in your project.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.