5.20241.9

グリッド:CSVにエクスポート

FlexGridをCSVにエクスポートするには、FlexGrid.getClipStringメソッドを呼び出してグリッド全体または指定の範囲を表す文字列を取得し、それをファイルに保存します。

グリッド全体をCSVにエクスポートするには、次のように記述します。

import * as wjGrid from '@mescius/wijmo.grid';

//CSV文字列をダウンロード可能なファイルに変換する関数
function exportFile(csv, fileName) {
    var fileType = 'txt/csv;charset=utf-8';
    if (navigator.msSaveBlob) { // IE 
        navigator.msSaveBlob(new Blob([csv], {
        type: fileType
        }), fileName);
    } 
    else {
        var e = document.createElement('a');
        e.setAttribute('href', 'data:' + fileType + ',' + encodeURIComponent(csv));
        e.setAttribute('download', fileName);
        e.style.display = 'none';
        document.body.appendChild(e);
        e.click();
        document.body.removeChild(e);
    }
}

//グリッドをCSVにエクスポートします
var rng = new wjGrid.CellRange(0, 0, theGrid.rows.length - 1, theGrid.columns.length - 1),
    csv = theGrid.getClipString(rng, true, true);
exportFile(csv, 'FlexGrid.csv');

また、エクスポートするセルの範囲を選択することで、グリッドの一部をエクスポートすることもできます。

//選択したセル範囲をCSVにエクスポートします
var rng = theGrid.selection,
    csv = theGrid.getClipString(rng, true, true);
exportFile(csv, 'Selection.csv');