在javascript中将表格html转换为Excel

时间:2014-04-23 11:23:28

标签: javascript jquery html mysql excel

将HTML表格转换为Excel文件时出现问题。我用

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

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

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

JAVASCRIPT CODE:

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

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>

谢谢大家!!

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

3 个答案:

答案 0 :(得分:2)

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

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))
    }
})()

答案 1 :(得分:2)

这里有文章使用javascript将html表导出为ex​​cel

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

答案 2 :(得分:1)

<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

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