반응형
WordPress 플러그인의 Array-to-CSV 내보내기 기능에 문제가 있습니다.
플러그인 페이지에서 보고서 생성을 위해 간단한 배열-CSV 내보내기 기능을 사용하고 있습니다.
이 코드를 실행하면 예상한 배열과 함께 HTML 콘텐츠 전체를 내보낸다는 오류가 나타납니다.
코드는 다음과 같습니다.
function convert_to_csv($input_array, $output_file_name, $delimiter)
{
clearstatcache();
/** open raw memory as file, no need for temp files */
$temp_memory = fopen('php://memory', 'w');
/** loop through array */
foreach ($input_array as $line) {
/** default php csv handler **/
fputcsv($temp_memory, $line, $delimiter);
}
//echo '<pre>';
//print_r($temp_memory); exit;
/** rewrind the "file" with the csv lines **/
fseek($temp_memory, 0);
/** modify header to be downloadable csv file **/
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
/** Send file to browser for download */
fpassthru($temp_memory);
}
/** Array to convert to csv */
$array_to_csv = Array(
Array(12566,
'Enmanuel',
'Corvo'
),
Array(56544,
'John',
'Doe'
),
Array(78550,
'Mark',
'Smith'
)
);
clearstatcache();
convert_to_csv($array_to_csv, 'report.csv', ',');
이 함수가 호출된 후 WP는 정상적으로 동작하고 있기 때문에 CSV 후에 HTML 템플릿 출력을 얻을 수 있을 것입니다.를 삽입하다exit
뒤의 진술fpassthru
이렇게 하면 Wordpress가 각 페이지 응답 끝에 수행하는 다른 작업을 망치지 않도록 주의해야 합니다.예를 들어 Drupal은drupal_exit()
이 목적을 위해서만 기능합니다.WP에 대해서는 잘 모르지만 wp_die() 함수의 설명서에 따르면 PHP를 사용할 수 있습니다.exit
별 문제 없이
이 코드를 사용하여 확장자가 .xlsx인 Excel 파일을 생성할 수 있습니다.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/alasql/0.3/alasql.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.12/xlsx.core.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var insuranceArray = new Array(insuranceInfo);
var bills = db.bills;
//Create tabs in excel file
var opts = [{sheetid:'Insurance Information',header:true},{sheetid:'bills Info',header:false}];
alasql('SELECT * INTO XLSX("Data.xlsx",?) FROM ?', [opts, insuranceArray,bills]]);
// Close the window once you save the file
window.onfocus=function(){
window.close();
}
</script>
이 코드는 나에게 유효하다...
내보내기 기능의 맨 위에서 이 기능을 사용해 보십시오.
global $wpdb;
$wpdb->hide_errors();
@set_time_limit(0);
if ( function_exists( 'apache_setenv' ) )
@apache_setenv( 'no-gzip', 1 );
@ini_set('zlib.output_compression', 0);
//@ob_clean();
@ob_end_clean(); // to prevent issue that unidentified characters when opened in MS-Excel in some servers
header( 'Content-Type: text/csv; charset=UTF-8' );
header( 'Content-Disposition: attachment; filename=export.csv' );
header( 'Pragma: no-cache' );
header( 'Expires: 0' );
$fp = fopen('php://output', 'w');
서버에 파일을 저장하지 않고 CSV를 생성하는 가장 간단한 방법
<?php
$array = Array(
Array(
12566,
'Enmanuel',
'Corvo'
),
Array(
56544,
'John',
'Doe'
),
Array(
78550,
'Mark',
'Smith'
)
);
arr_to_csv($array);
function arr_to_csv($array)
{
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=test.csv");
$fp = fopen('php://output', 'w');
foreach ($array as $row) {
fputcsv($fp, $row);
}
}
언급URL : https://stackoverflow.com/questions/29816189/array-to-csv-export-function-is-facing-an-issue-in-wordpress-plugin
반응형
'programing' 카테고리의 다른 글
Oracle이 없는 경우 삽입 (0) | 2023.02.22 |
---|---|
Preact에 의해 잘못된 컴포넌트가 렌더링됨 (0) | 2023.02.22 |
VirtualAlloc() 실패: [0x00000008] 스토리지가 부족하여 이 명령을 처리할 수 없습니다. (0) | 2023.02.22 |
Angular에서의 약속 오브젝트 캐시JS 서비스 (0) | 2023.02.22 |
ReactJs Router Link와 material-ui 컴포넌트(버튼 등)를 조합하는 방법 (0) | 2023.02.22 |