简体   繁体   中英

How can I export a HTML table to Excel with a grid

I'm very new in exporting table/data to Excel or PDF. However so far I have written jQuery for exporting a HTML table to Excel (xls). The problem is that it does not show the grid of the excel. Here is the example of the output in Excel.

出口问题

Here is my code to export:

$(document).ready(function() {
  $("#btnExport").click(function(e) {
    e.preventDefault();

    //getting data from our table
    var data_type = 'data:application/vnd.ms-excel';
    var table_div = document.getElementById('table_id');
    var table_html = table_div.outerHTML.replace(/ /g, '%20');

    var a = document.createElement('a');
    a.href = data_type + ', ' + table_html;
    a.download = 'exported_salmon_' + Math.floor((Math.random() * 9999999) + 1000000) + '.xls';
    a.click();
  });
});

Here is my HTML

<table  id="table_id" class="data-table">
<caption class="title">Sales Salmon Usage Per 1K</caption>
<thead>
  <tr>
    <th><center>NO</center></th>
    <th><center>OUTLET</center></th>
    <th><center>DATE</center></th>
    <th><center>SALES</center></th>
    <th><center>USAGE</center></th>
    <th><center>USAGE PER 1K</center></th>
  </tr>
</thead>
<tbody>
<?php



$storenum=$_POST['search'];
$store_name=$_POST['storename'];
$store_region=$_POST['region'];
$date1 = $_POST['date1'];
  $date11 = strtotime($date1);
  $date111 = date('Y-m',$date11);
  $date112 = date('d',$date11);
  //date2
  $date2 = $_POST['date2'];
  $date21 = strtotime($date2);
  $date211 = date('Y-m',$date21);
  $date2111= date('F Y',$date21);
  $date212 = date('d',$date21);
  $t = date('t',$date21);
  $x=(($date212-$date112)+1)/$t;



 //input date1 and date2
 if ((!empty($date1)) && (!empty($date2)) && (empty($storenum)) && 
(empty($store_region))){
    //my$query = "";

 }

//input storenum, date1,date2
else if ((!empty($storenum)) && (!empty($date1)) && (!empty($date2)) && 
(empty($store_region))){
   //my$query = "";
 }


//input region, date1,date2
else if ((!empty($store_region)) && (!empty($date1)) && (!empty($date2)) && 
(empty($storenum))){
  //my$query = "";


 } 


  // Execute the query, or else return the error message.
  $result = $dbhandle->query($query) or exit("Error code ({$dbhandle->errno}): {$dbhandle->error}");

$no   = 1;
while ($row = mysqli_fetch_array($result))
{
  //someofmycode...


   echo "<tr class='y_n';>"; 
   echo "<td><center>".$no."</center></td>"; 
   echo "<td>".$store_name."</td>"; 
   echo "<td>".$date2111."</td>";
   echo "<td>".$netamt11."</td>"; 
   echo "<td>".$usage11."</td>";  
   echo "<td>".$duh1."</td>";
   echo "</tr>"; 
  $no++;
   } ?>

</tbody>
<tfoot>
  <tr>
    <th colspan="5">TOTAL</th>
    <th><?=($total)?></th>
  </tr>
  <tr>
  <th colspan="5">AVERAGE <?php echo $actionstatus2 ?></th>

  <?php   

   //someofmycodecondition...                                                               


  ?>
  <th><?php echo number_format($X,4)?></th></tr>
 <tr>
 <th colspan="6"><center><?php echo $actionstatus?></center></th>
 </tr>

</tfoot>

  </table>

</h4></center>


   <div class="box">
   <center><input id="btnExport" type="submit" class="button" value="  EXPORT  "></center>
   </div>
   </body>
            $objPHPExcel->createSheet();
            $objPHPExcel->setActiveSheetIndex(6);
            $objPHPExcel->getActiveSheet()->setTitle('Test')->setCellValue('A1', 'Chapter')
                ->setCellValue('B1', 'Topic')
                ->setCellValue('C1', 'Test')
                ->setCellValue('D1', 'Total Questions')
                ->setCellValue('E1', 'Time');
            $o=2;
            foreach($test as $testval)
            {
                $connection = Yii::$app->db;
                $chapname2 = "SELECT name as 'chapter' from chapter  Where id = '".$testval['chapter_id']."' ORDER BY name ASC";
                $command = $connection->createCommand($chapname2);
                $value = $command->queryOne();

                $objPHPExcel->setActiveSheetIndex(6)
                    ->setCellValue('A'.$o, $value['chapter'])
                    ->setCellValue('B'.$o, $testval['name'])
                    ->setCellValue('C'.$o, $testval['test'])
                    ->setCellValue('D'.$o, $testval['total_questions'])
                    ->setCellValue('E'.$o, $testval['test_time']);
                $topicID[] = $testval['id'];

                $o++;
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM