javascript 在javascript中将表格html转换为Excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23242816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:36:40  来源:igfitidea点击:

Convert table html to Excel in javascript

javascriptjqueryhtmlmysqlexcel

提问by Xavi Gómez Canals

I have a problem to convert an HTML table to an Excel file. I use

我在将 HTML 表格转换为 Excel 文件时遇到问题。我用

window.open ('data: application / vnd.ms-excel,' + ...

in javascript to perform this conversion. The problem is that the Excel file generated only has a one cell with all the HTML code inside. I need a typical table with rows and columns in Excel.

在 javascript 中执行此转换。问题是生成的 Excel 文件只有一个包含所有 HTML 代码的单元格。我需要在 Excel 中包含行和列的典型表格。

Thanks in advance and greetings to the community. I am super newbie ... please be patient! ;)

在此先感谢并向社区致以问候。我是超级新手...请耐心等待!;)

JAVASCRIPT CODE:

爪哇代码:

function excelSeguimientos() {
window.open('data:application/vnd.ms-excel,' + $('#tablaSeguimientos').html());
e.preventDefault();
}

HTML CODE (TABLE):

HTML 代码(表格):

<table width="1000" id="tablaSeguimientos">
        <tr bgcolor="#CCC">
          <td width="100">Fecha</td>
          <td width="700">Seguimiento</td>
          <td width="170">Producto</td>
          <td width="30">&nbsp;</td>
        </tr>
        <tr bgcolor="#FFFFFF">
          <td><?php                 
                $date = date_create($row_Recordset3['fecha']);
                echo date_format($date, 'd-m-Y');
                ?></td>
          <td><?php echo $row_Recordset3['descripcion']; ?></td>
          <td><?php echo $row_Recordset3['producto']; ?></td>
          <td><img src="borrar.png" width="14" height="14" class="clickable" onClick="eliminarSeguimiento(<?php echo $row_Recordset3['idSeguimiento']; ?>)" title="borrar"></td>
        </tr>
      </table>

Thanks for all!!

谢谢大家!!

NOTE: Is it possible to remove automatic the column with the image? Because the table is autogenerated by a mysql query, and has many rows! Thanks!

注意:是否可以自动删除带有图像的列?因为该表是由 mysql 查询自动生成的,并且有很多行!谢谢!

回答by anonman

Should have done more research, Code is below:

应该做更多的研究,代码如下:

var tableToExcel = (function() {
    var uri = 'data:application/vnd.ms-excel;base64,',
        template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
        base64 = function(s) { 
          return window.btoa(unescape(encodeURIComponent(s))) 
        },
        format = function(s, c) { 
          return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; })
        }
    return function(table, name) {
        if (table.nodeType) table = document.getElementById("tablaSeguimientos")
        var ctx = {worksheet: name || 'tablaSeguimientos', table: tablaSeguimientos.innerHTML}
        window.location.href = uri + base64(format(template, ctx))
    }
})()

回答by Gaurang s

here is article Export html table to excel using javascript

这是文章使用 javascript 将 html 表导出到 excel

http://www.codeproject.com/Questions/433079/Export-html-table-to-excel-using-javascript

http://www.codeproject.com/Questions/433079/Export-html-table-to-excel-using-javascript

回答by Praveen Srinivasan

<script src="jquery.min.js"></script>
<table border="1" id="ReportTable" class="myClass">
    <tr bgcolor="#CCC">
      <td width="100">Fecha</td>
      <td width="700">Seguimiento</td>
      <td width="170">Producto</td>
      <td width="30">&nbsp;</td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td>
            <?php                 
            $date = date_create($row_Recordset3['fecha']);
            echo date_format($date, 'd-m-Y');
            ?>
      </td>
      <td><? php echo $row_Recordset3['descripcion']; ?></td>
      <td><? php echo $row_Recordset3['producto']; ?></td>
      <td><img src="borrar.png" width="14" height="14" class="clickable" onClick="eliminarSeguimiento(<?php echo $row_Recordset3['idSeguimiento']; ?>)" title="borrar"></td>
    </tr>
  </table>

  <input type="hidden" id="datatodisplay" name="datatodisplay">  
    <input type="submit" value="Export to Excel"> 

table_export.php

table_export.php

<?php
header('Content-Type: application/force-download');
header('Content-disposition: attachment; filename=export.xls');
header("Pragma: ");
header("Cache-Control: ");
echo $_REQUEST['datatodisplay'];
?>