douyuben9434 2019-03-29 14:06
浏览 137
已采纳

Ajax将数据发布到Excel工作表中

So I try to put data into a Excel sheet, I do it this way,

first is send the data via a ajax post:

  <!doctype html>
  <html lang="en">
  <head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 </head>
 <script>
 $(document).ready(function(){
$("#form").on('submit', function() {
    $(function() {
        var hello = "hello world";
        $.ajax ({
            type: 'POST',
            url: 'example.php',
            data: {hello: hello},
            success: function(result) {
                console.log('success');
            }
        });
    });
   });
    });
    </script>
   <body>
   <form id="form" action="example.php" method="post">
   <input type="submit" value="submit">
   </form>
   </body>
   </html>

Then I put it into a excel sheet:

   <?php 
   include "../includes/PHPExcel.php";

   $title = "Verrijking ";

   $hello = $_POST['hello'];

   $objPHPExcel = new PHPExcel();
   $objPHPExcel->getProperties()->setCreator("RM Netherlands B.V.")
        ->setLastModifiedBy("RM Netherlands B.V.")
        ->setTitle($title)
        ->setSubject($title)
        ->setDescription($title)
        ->setKeywords($title)
        ->setCategory($title);

   $pcbestand = date('Ymdhis') . ".xlsx";

   $objPHPExcel->getActiveSheet()
        ->setCellValue("A"."1", $hello);
   $objPHPExcel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);


   $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
   $objWriter->save($pcbestand);

   $file = $pcbestand;
   header('Content-disposition: attachment; filename='.$file);
   header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
   header('Content-Length: ' . filesize($file));
   header('Content-Transfer-Encoding: binary');
   header('Cache-Control: must-revalidate');
   header('Pragma: public');
   ob_clean();
   flush();
   readfile($file);

  $DelFilePath = $setup['/var/www/clients/client1/web1/web/nordin/'.$pcbestand.''] . $pcbestand;

  if (file_exists($DelFilePath)) { unlink ($DelFilePath); }
  ?>

the last part create's a save as file dialog and make's sure it isn't uploaded to the server.

But this is the problem I don't get the ajax post ($_POST['hello']) into my excel sheet even tho the ajax post is successful. What am I doing wrong? please help.

  • 写回答

1条回答 默认 最新

  • douzhang2092 2019-03-29 14:16
    关注

    There's two issues here. The first is that you don't have any e.preventDefault() in your click() callback, which will make your form to be submitted "normally" without your JS (which is why you get the download prompt). The second issue is that you can't use Ajax if you want to download the response. Put your hello in a hidden input in your form and remove your JS and it will work. – Magnus Eriksson

    This worked all credit to Magnus Eriksson.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog