2014-07-26 2 views
0

json에서 새로운 기능입니다. PHP를 사용하여 mysql 테이블에서 jason 데이터를 생성하고 생성 된 json을 .xls 또는 .csv 형식으로 내보내려고합니다. 나는 JSON 데이터를 생성하기 위해 다음과 같은 PHP 코드를 사용 : examexport.php 내가 CSV 형식으로 JSON을 보내는 코드를 가지고 유래에 여기Json to Excel CSV 또는 jquery 및 PHP를 사용하는 XLS

<?php 
    include 'conn.php'; 

    $page = isset($_POST['page']) ? intval($_POST['page']) : 1; 
    $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10; 
    $semester = isset($_POST['semester']) ? 
    mysql_real_escape_string($_POST['semester']) : ''; 
    $entry = isset($_POST['entry']) ? mysql_real_escape_string($_POST['entry']) : ''; 
    $batch = isset($_POST['batch']) ? mysql_real_escape_string($_POST['batch']) : ''; 

    $offset = ($page-1)*$rows; 

    $result = array(); 

$where = "semester like '$semester%' and entry like '$entry%' and batch like '$batch%'"; 
    $rs = mysql_query("select count(*) from ba_test where " . $where); 
    $row = mysql_fetch_row($rs); 
    $result["total"] = $row[0]; 

$rs = mysql_query("select * from ba_test where " . $where . " limit $offset,$rows"); 

$items = array(); 
while($row = mysql_fetch_object($rs)){ 
     array_push($items, $row); 
    } 
    $result["rows"] = $items; 

    echo json_encode($result); 
?> 

을하지만이 작업을 만들 수 없습니다. 수출 능가하는

{"total":"6","rows": 
[{"id":"2","regd":"25","name":"Lalhmangaihsangi","class":"BA", 
"rollno":"3","univ_roll":"UNVI573","univ_no":"MZU876","core":"Education", 
"semester":"First","batch":"2014","subject":"Education", 
"entry":"Second Internal Test","date":"2014-07-23", 
"score":"55","fm":"100","remark":"She is guarded"}]} 

코드 :하십시오 JSON을 만드는 대신

<input type="button" onclick="exportExcel()" value="Export to Excel " /> 
<script>     
    function exportExcel(){ 
    $('#dg').datagrid('load',{ //load data by semester/batch/entry 
    semester: $('#semester').val(), 
    batch: $('#batch').val(), 
    entry: $('#entry').val(), 
    document.location='excel/examexport.php'// How do I include entry/batch/ here? 
}); 
} 
</script> 
+0

http://stackoverflow.com/questions/24972547/export-datagrid-to-excel-using-을 확인하시기 바랍니다, CSV 창출에 PHP에 대한 자세한 설명이 SO 질문을 참조 할 수 있습니다 jquery-easyui 같은 방법으로 CSV를 얻을 수 있습니다. –

답변

0

을 다음 CSV 파일로 변환, 직접 CSV의 문자열/텍스트를 만들 수 있습니다 내 PHP는 JSON을 생성 데이터베이스에서 가져온 값과 비교하십시오.

당신은 당신이 파일에 기록 할 수 있습니다 http://en.wikipedia.org/wiki/Comma-separated_values#Example

문자열/텍스트를 생성 한 후, 여기에서 CSV 형식에서 모양과 그에 따라 CSV 문자열을 만들 수 있습니다. 그런 다음 브라우저에서 파일을 저장하도록 헤더 내용을 설정하십시오.

header("Content-type: text/csv"); 
header("Content-Disposition: attachment; filename=file.csv"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 

그런 다음 응답을 보내주십시오.

는 또한 Create a CSV File for a user in PHP

+0

문제는 검색 기능이있는 DataGrid를 표시하기 위해 json을 사용했고 검색 결과를 Excel 파일로 내보내려는 것입니다. –

+0

이 SO 답변을 참조해야합니다. http://stackoverflow.com/questions/20667418/converting-json-to-csv-format-using-php – WisdmLabs