如何创建可以将html导出到CSV文件的功能?

如何解决如何创建可以将html导出到CSV文件的功能?

| 我有以下html文件:
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" dir=\"ltr\" lang=\"en\">
    <head>
    </head>
    <body>
         <p align=\"center\"><strong>Claims Search Results</strong></p>
         <table id=\"ClaimsListPrint\" class=\"printClaims\">
         <thead>
        <tr style=\"background-color: rgb(241,241,241);\">
            <th style=\"background-color: rgb(215,227,235);\">
                Member
            </th>
            <th style=\"background-color: rgb(215,235);\">
                Date of Service
            </th>
            <th style=\"background-color: rgb(215,235);\">
                Provider
            </th>
            <th style=\"background-color: rgb(215,235);\">
                Claim Type
            </th>
            <th style=\"background-color: rgb(215,235);\">
                Status
            </th>
            <th style=\"background-color: rgb(215,235);\">
                Billed Amount
            </th>
            <th style=\"background-color: rgb(215,235);\">
                Paid by Plan
            </th>
            <th style=\"background-color: rgb(215,235);\">
                Member Responsiblity
            </th>
        </tr>
    </thead>
    <tbody>
        <tr style=\"background-color: rgb(255,255,240);\" class=\"claim\">
            <td class=\"claim-member\">
                John Sample
            </td>
            <td class=\"claim-dateOfService\">
                02/27/2011
            </td>
            <td class=\"claim-provider\">
                TestProv1
            </td>
            <td class=\"claim-claimType\">
                Medical
            </td>
            <td class=\"claim-status\">
                Processed
            </td>
            <td class=\"claim-billedAmount\">
                $145
            </td>
            <td class=\"claim-paidByPlan\">
                $125
            </td>
            <td class=\"claim-memberResponsibility\">
                $25
            </td>
        </tr>
        <tr style=\"background-color: rgb(241,241);\" class=\"claim\">
            <td class=\"claim-member\">
                John Sample
            </td>
            <td class=\"claim-dateOfService\">
                02/27/2011
            </td>
            <td class=\"claim-provider\">
                TestProv1
            </td>
            <td class=\"claim-claimType\">
                Medical
            </td>
            <td class=\"claim-status\">
                In-Process
            </td>
            <td class=\"claim-billedAmount\">
                -
            </td>
            <td class=\"claim-paidByPlan\">
                -
            </td>
            <td class=\"claim-memberResponsibility\">
                -
            </td>
        </tr>
      </tbody>
    </table>
  </body>
</html>
我希望最终用户能够按如下所示链接文件中的链接:
<a id=\"link3\" class=\"links\" href=\"home.html\">Export to Microsoft Excel</a>
当他们单击它时,我希望将表导出为csv文件。我该怎么做?我可以使用Javascript或PHP或其他方式执行此操作吗?     

解决方法

        考虑使用jQuery插件:HTML表到CSV 它是单线的:
$(\'#mytable\').table2CSV();
您可以在此HTML Table To CSV演示中看到此操作。     ,        无论您使用哪种语言来生成页面,我都会使用它导出。如果您已经在其他地方拥有数据,则解析HTML页面以生成CSV没有任何意义。页面如何生成?     ,        如果要下载此CSV文件,则可以使用PHP修改文档标题,以告知浏览器强制下载文件。我使用以下功能来强制下载现有文件,但是您可以通过进行一些修改将其修改为输出即时生成的CSV文件。另外,您可以保存一个CSV文件并使用它下载它。 顺便说一句,这不是我的原始代码。我在StackOverflow上的其他地方找到了它,对我来说非常有用。
function download($filePath) {
    if(headers_sent()) die(\'Headers alread sent. Download cannot initialize.\');

    // Required for some browsers
    if (ini_get(\'zlib.output_compression\')) ini_set(\'zlib.output_compression\',\'Off\');

    // File Exists?
    if( file_exists($filePath) ){
        // Parse Info / Get Extension
        $fsize = filesize($filePath);
        $path_parts = pathinfo($filePath);
        $ext = strtolower($path_parts[\"extension\"]);

        // Determine Content Type
        switch ($ext) {
            case \"pdf\": $ctype=\"application/pdf\"; break;
            case \"exe\": $ctype=\"application/octet-stream\"; break;
            case \"zip\": $ctype=\"application/zip\"; break;
            case \"doc\": $ctype=\"application/msword\"; break;
            case \"xls\": $ctype=\"application/vnd.ms-excel\"; break;
            case \"ppt\": $ctype=\"application/vnd.ms-powerpoint\"; break;
            case \"gif\": $ctype=\"image/gif\"; break;
            case \"png\": $ctype=\"image/png\"; break;
            case \"jpeg\":
            case \"jpg\": $ctype=\"image/jpg\"; break;
            default: $ctype=\"application/force-download\";
        }

        header(\"Pragma: public\"); // required
        header(\"Expires: 0\");
        header(\"Cache-Control: must-revalidate,post-check=0,pre-check=0\");
        header(\"Cache-Control: private\",false); // required for certain browsers
        header(\"Content-Type: $ctype\");
        header(\"Content-Disposition: attachment; filename=\\\"\".basename($filePath).\"\\\";\" );
        header(\"Content-Transfer-Encoding: binary\");
        header(\"Content-Length: \".$fsize);
        ob_clean();
        flush();
        readfile( $filePath );

    } else {
        die(\'File Not Found\'); 
    }
}
    ,        在PHP中: 使用PHP的DOMDocument类将HTML文件加载到DOM对象结构中。 解析DomDocument,找到所需的元素,然后将相关值提取到数组中。 使用PHP的fputsvc()函数将数组另存为CSV文件。     ,        尽管@ p.campbell是一个很好的解决方案,它可以为您提供格式化的CSV,但不能为您提供CSV下载。如果希望最终用户以CSV格式下载,则可以使用服务器技术(例如PHP)并返回CSV作为响应类型。 我也同意@mrkmg。如果有数据,请编写一些PHP以CSV格式返回数据。
http://mysite.com/data/csv
传递格式化数据,然后在PHP中将响应类型更改为CSV。用户将获得一个很好的“另存为...”对话框。我相信代码看起来像这样:
<?php
// We\'ll be outputting a CSV
header(\'Content-type: application/csv\');

// It will be called downloaded.csv
header(\'Content-Disposition: attachment; filename=\"downloaded.csv\"\');

// Write CSV...
?> 
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-