1

私は次の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, 227, 235);">
                Date of Service
            </th>
            <th style="background-color: rgb(215, 227, 235);">
                Provider
            </th>
            <th style="background-color: rgb(215, 227, 235);">
                Claim Type
            </th>
            <th style="background-color: rgb(215, 227, 235);">
                Status
            </th>
            <th style="background-color: rgb(215, 227, 235);">
                Billed Amount
            </th>
            <th style="background-color: rgb(215, 227, 235);">
                Paid by Plan
            </th>
            <th style="background-color: rgb(215, 227, 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, 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などでこれを行うことはできますか?

4

5 に答える 5

5

jQueryプラグインの使用を検討してください:HTMLテーブルからCSVへ

それはワンライナーです:

$('#mytable').table2CSV();

この動作は、このHTML TableToCSVデモで確認できます。

于 2011-05-31T17:35:27.687 に答える
1

ページの生成に使用している言語は、私がエクスポートする言語です。すでに別の場所にデータがある場合は、HTMLページを解析してCSVを生成することは意味がありません。ページはどのように生成されますか?

于 2011-05-31T17:35:18.563 に答える
1

この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'); 
    }
}
于 2011-05-31T17:44:33.943 に答える
0

PHPの場合:

  1. PHPのDOMDocumentクラスを使用して、HTMLファイルをDOMオブジェクト構造にロードします。

  2. DomDocumentを解析し、必要な要素を見つけて、関連する値を配列に抽出します。

  3. PHPのfputsvc()関数を使用して、配列をCSVファイルとして保存します。

于 2011-05-31T17:41:24.867 に答える
0

@ 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...
?> 
于 2011-05-31T17:43:56.627 に答える