0

How to read an excel file from the server - placed in the same place as the html and js files?

I can download xlsx file with input. But I need to download the file automatically then page load. I can't download the file directly from the server.

let input = document.getElementById('input');
let table = document.getElementById('tbl-data');
let htmlEnd = ``;

input.addEventListener('change', function () {
  readXlsxFile().then(function (data) {
    tableGeneratorHead(data[0]);
    htmlEnd += `<tbody> `;

    for (let i = 1; i < data.length; i++) {
      tableGenerator(data[i], i);
      console.log('1');
    }
    htmlEnd += ` </tbody>`;
    table.insertAdjacentHTML('beforeend', htmlEnd);
  });
});

const tableGeneratorHead = function (val1, val2) {
  htmlEnd += `<thead class="headTable"><tr> `;
  val1.forEach(function (val) {
    htmlEnd += `<th>${val} </th>`;
  });
  htmlEnd += `</tr></thead>`;
  console.log(htmlEnd);
};
const tableGenerator = function (val1, val2) {
  htmlEnd += `<tr> `;
  if (val2 % 2 == 0) {
    val1.forEach(function (val) {
      htmlEnd += `<td class="one">${val} </td>`;
    });
  }
  if (val2 % 2 > 0) {
    val1.forEach(function (val) {
      htmlEnd += `<td class="two">${val} </td>`;
    });
  }
  htmlEnd += `</tr>`;
  console.log(htmlEnd);
};

I tried to find an answer on the Internet but couldn't find anything I could use.

Dmitriy Popov
  • 2,150
  • 3
  • 25
  • 34
dpal
  • 1
  • [this](https://stackoverflow.com/a/52870648/4935162) looks like a good start, in addition to [fetching the actual file](https://www.freecodecamp.org/news/how-to-use-fetch-api/). I assume you don't have an *actual* backend and you only server files, – Yarin_007 Feb 22 '23 at 22:52

0 Answers0