doubi5127 2018-08-21 13:27
浏览 388
已采纳

php导出xls:文件格式和扩展名不匹配

I'm exporting xls with this code:

<?php
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: inline; filename=filename.xls");
?>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
my export
</body>
</html>

but when I open it, I get:
the file format and extension of don't match. the file could be corrupted or unsafe. unless you trust its source, don't open it. do you want to open it anyway?
is there a way to avoid it?

  • 写回答

1条回答 默认 最新

  • dongwei7913 2018-08-22 21:40
    关注

    What you've got there is HTML, not an Excel file. As Excel says, the file format and extension don't match. If you want to avoid the warning message, save it as an HTML file and open it from Excel.

    <?php
    header("Content-Disposition: inline; filename=filename.html");
    ?>
    <html>
    <head>
    <meta charset="utf-8">
    </head>
    <body>
    my export
    </body>
    </html>
    

    Or output a different file format such as CSV. It's very simple.

    <?php
    $my_data = [
        [1, 2, 3, 4, 5, 7],
        [9, 8, 7, 6, 5, 3],
    ];
    header("Content-Type: text/csv");
    header("Content-Disposition: attach; filename=filename.csv");
    $h = fopen("php://output", "w");
    foreach($my_data as $data) {
        fputcsv($h, $data);
    }
    fclose($h);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启