Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
Exportación de tabla HTML a Excel sin etiqueta <td> oculta

En mi aplicación web, tengo una tabla HTML que muestra algunos datos al usuario.

Aquí quiero exportarlo a excel y encontré un código jQuery para hacerlo.

Pero la cuestión es que también exporta los datos que escondo de la tabla.

¿Hay alguna forma de exportar la tabla sin <td style="display:none;"> @Html.DisplayFor(modelItem => item.Id) </td>

esta es la mesa

 <table id="tblParts" class="table table-striped"> <thead> <tr> <th> Part Number </th> <th> Description </th> <th> Model </th> <th> Order Qty </th> <th></th> </tr> </thead> <tbody> @foreach (var item in Model.OrderBy(i => i.PartNumber)) { <tr> <td style="display:none;"> @Html.DisplayFor(modelItem => item.Id) </td> <td> @Html.DisplayFor(modelItem => item.PartNumber) </td> <td> @Html.DisplayFor(modelItem => item.Description) </td> <td> @Html.DisplayFor(modelItem => item.Model) </td> <td> @Html.DisplayFor(modelItem => item.OrderQty) </td> </tr> } </tbody> </table>

Este es el Jquery.

 function ExportToExcel(type, fn, dl) { var elt = document.getElementById('tblParts'); var wb = XLSX.utils.table_to_book(elt, { sheet: "sheet1" }); return dl ? XLSX.write(wb, { bookType: type, bookSST: true, type: 'base64' }) : XLSX.writeFile(wb, fn || ('Order.' + (type || 'xlsx'))); }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Tal vez antes de exportar, puede eliminar la columna oculta dentro de la función de exportación. Asigne a la columna oculta un nombre de clase. He adjuntado un violín para su referencia:

ejemplo

HTML

 <table id="tblParts" class="table table-striped"> <thead> <tr> <th>Part Number </th> <th>Description </th> <th>Model </th> <th>Order Qty </th> <th></th> </tr> </thead> <tbody> <tr> <td style="display: none;" class="item-id">1 </td> <td>2</td> <td>3 </td> <td>4 </td> <td>5 </td> </tr> <tr> <td style="display: none;" class="item-id">2 </td> <td>2</td> <td>3 </td> <td>4 </td> <td>5 </td> </tr> </tbody> </table>

JS

 function ExportToExcel(type, fn, dl) { var elt = document.getElementById('tblParts'); const elements = elt.getElementsByClassName("item-id"); //get column with the class name item-id while (elements.length > 0) elements[0].remove(); //if element exist, remove the column var wb = XLSX.utils.table_to_book(elt, { sheet: "sheet1" }); return dl ? XLSX.write(wb, { bookType: type, bookSST: true, type: 'base64' }) : XLSX.writeFile(wb, fn || ('Order.' + (type || 'xlsx'))); }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda