2014-12-10 2 views
1

저는 엑셀 파일 또는 적어도 열리는 CSV 파일을 만드는 데 필요한 웹 사이트에서 작업하고 있습니다. 나는 많은 것을 시도했다. Google, stackoverflow 등에서 솔루션을 검색했지만 찾은 모든 것이 저에게 효과가없는 것 같습니다. 나는 jQuery 솔루션을 시도하고 PHP 솔루션을 시도했다.excel 또는 csv로 내보내기

JSFiddle : 아니 하나,

Example of the csv ouput

내가 출력은 별도의 colums에서 할 것을 권장합니다

나는이 바이올린을 사용 http://jsfiddle.net/terryyounghk/KPEGU/가 출력이되는 CSV를 만들기 위해.

header("Content-type: application/vnd.ms-excel"); 
    header("Content-Disposition: attachment;Filename=document_name.xls"); 

    echo "<html>"; 
    echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">"; 
    echo "<body>"; 
    echo "<table>"; 
    echo "<tr>"; 
    echo "<td>Henk</td>"; 
    echo "<td>Henk2</td>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>bob</td>"; 
    echo "<td>bob2</td>"; 
    echo "</tr>"; 
    echo "</table>"; 
    echo "</body>"; 
    echo "</html>"; 

이 출력 :

example of php output

여기서 출력 적절한 열 및 행에 여기

또한 I는 시도한 PHP 솔루션이다. 하지만 어떻게 든 그리드 라인을 얻을 수 없습니다.

이것은 내가 시도한 톤의 2 가지 예입니다. 누군가 이것을 고치는 방법을 알고 있습니까?

답변

1

JsFiddle 예에서 입력 한 문자열이 다른 셀에 속하게하려면 쉼표로 구분 된 값 ;이 필요하므로 colDelim = '","'에서 colDelim = '";"'으로 변경하십시오. 당신의 서식을 할 테이블을 사용하는 방법에 대한 생각과 함께가는 곳

Updated JsFiddle

+0

도움 주셔서 감사합니다! – SemperMemento

+0

여러분을 환영합니다! – urbz

0

내가 볼 수 있지만 CSV의 쉼표로 구분합니다.

// setup array. Note null values to create empty cells 
$list = array( 
    array('First row', NULL, 'some_data', NULL), 
    array('Second row', 'some data', NULL, 'some data'), 
); 

// set header types 
header("Content-type: text/csv"); 
header("Cache-Control: no-store, no-cache"); 
header('Content-Disposition: attachment; filename="most_popular-'.$_POST['start_date'].'-'.$_POST['end_date'].'.csv"'); 

// tell php to open the page as an output (download) 
$outstream = fopen("php://output",'w'); 

// loop through and put each array row into the csv, comma seperated. 
foreach($list as $row) 
{ 
    fputcsv($outstream, $row, ','); 
} 

exit; 

그냥 배열의 각 행이 매우 얻을 것이다 CSV를 생성하는 데 그렇지 않으면 루프를 다른 사람과 동일한 필요가 있음을 기억

내가 찾은 가장 좋은 방법은과 같이 배열을 사용하는 것입니다 혼란스러워.

0
**Try this** 




<?php 
error_reporting(E_ERROR); 
include("dbConnect.inc.php"); 
//$ip = $_SERVER['HTTP_CLIENT_IP 
    $select_table=mysql_query("SELECT * FROM `key_cat`"); 
    $setExcelName="Keyword_reports"; 
header('Content-Type: text/csv'); 
header('Content-Disposition: attachment;filename='.$setExcelName.'.csv'); 
/* 
$setExcelName="Keywords"; 
header("Content-type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=".$setExcelName."_Reoprt.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
*/ 
$rows = mysql_fetch_assoc($select_table); 
if($rows) 
{ 
getcsv(array_keys($rows)); 
} 
while($rows) 
{ 
getcsv($rows); 
$rows = mysql_fetch_assoc($select_table); 
} 
// get total number of fields present in the database 
function getcsv($no_of_field_names) 
{ 
$separate = ''; 
// do the action for all field names as field name 
foreach ($no_of_field_names as $field_name) 
{ 
if (preg_match('/\\r|\\n|,|"/', $field_name)) 
{ 
$field_name = '' . str_replace('', $field_name) . ''; 
} 
echo $separate . $field_name; 

//sepearte with the comma 
$separate = ','; 
} 

//make new row and line 
echo "\r\n"; 

} 


?>