Drupal将数据从表格导出到Excel
我正在使用以下 2 个函数将数据从名为 eco_customers 的表导出到 excel 文件,
但我遇到阿拉伯字符问题,它们显示损坏,如何修改编码以纠正 excel 文件中的阿拉伯字符问题
function cleanData(&$str) {
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"'))
$str = '"' . str_replace('"', '""', $str) . '"';
}
function customer_export(){
$filename = "cutomers_data_" . date('Ymd') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel, charset=UTF-8; encoding=UTF-8");
$flag = false;
$result = db_query("SELECT name, arabic_name, phones, fax, address, country, city, email, website, registration_no FROM eco_customers ") or die('Query failed!');
while(false !== ($row = db_fetch_array($result))) {
if(!$flag) {
// display field/column names as first row
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
array_walk($row, 'cleanData');
echo implode("\t", array_values($row)) . "\r\n";
}
}
I am using the following 2 functions to export data from a table called eco_customers to excel file
but I have a problem with Arabic characters, they show corrupt, how can I modify the encoding to correct the Arabic characters problem in the excel file
function cleanData(&$str) {
$str = preg_replace("/\t/", "\\t", $str);
$str = preg_replace("/\r?\n/", "\\n", $str);
if(strstr($str, '"'))
$str = '"' . str_replace('"', '""', $str) . '"';
}
function customer_export(){
$filename = "cutomers_data_" . date('Ymd') . ".xls";
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel, charset=UTF-8; encoding=UTF-8");
$flag = false;
$result = db_query("SELECT name, arabic_name, phones, fax, address, country, city, email, website, registration_no FROM eco_customers ") or die('Query failed!');
while(false !== ($row = db_fetch_array($result))) {
if(!$flag) {
// display field/column names as first row
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
array_walk($row, 'cleanData');
echo implode("\t", array_values($row)) . "\r\n";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用了 phpExcel 库,现在一切正常
I used phpExcel library and everything worked fine for me now