我如何在 php [codeigniter] 中将 utf-8 设置为 csv 文件
How can i setting utf-8 to csv file in php [codeigniter]
public function ExportCSV(){
$this->load->dbutil();
$this->load->helper('file');
$this->load->helper('download');
$delimiter = ",";
$newline = "\r\n";
$slash = "∕";
$filename = "testnaja.csv";
$query = "select * from student where room_id = 011";
$result = $this->db->query($query);
$data = $this->dbutil->csv_from_result($result, $delimiter, $newline);
force_download($filename, $data);
}
我想生成 excel 文件,将数据从我的 table 导出到它。
.xls 和 .csv 中的输出
csv 输出为:
请帮忙。谢谢
要设置 codeigniter 以下载 CSV 格式,请使用以下代码。
function _push_file($path, $name)
{
// make sure it's a file before doing anything!
if(is_file($path))
{
// required for IE
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
// get the file mime type using the file extension
$this->load->helper('file');
$mime = get_mime_by_extension($path);
// Build the headers to push out the file properly.
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($path)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$mime); // Add the mime type from Code igniter.
header('Content-Disposition: attachment; filename="'.basename($name).'"'); // Add the file name
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($path)); // provide file size
header('Connection: close');
readfile($path); // push it out
exit();
}
复制自:Codeigniter Force download files
还有来自:Download csv from codeigniter mysql
我不知道我是否理解正确,但如果您的问题是您希望 Excel 将 CSV 文件识别为 UTF-8 编码,则必须添加UTF-8 BOM 到您的文件。只需在 CSV 输出前加上
chr(239) . chr(187) . chr(191) // xEF xBB xBF
例如:
$data = chr(239) . chr(187) . chr(191) . $this->dbutil->csv_from_result($result, $delimiter, $newline);
如果您的问题与强制下载有关,也许 会有所帮助。
将你的force_download从
force_download($filename, $data);
为此:
force_download($filename, "\xEF\xBB\xBF" . $data);
这使得输出为 UTF-8 + BOM,因此它被 MS 识别 Excel
public function ExportCSV(){
$this->load->dbutil();
$this->load->helper('file');
$this->load->helper('download');
$delimiter = ",";
$newline = "\r\n";
$slash = "∕";
$filename = "testnaja.csv";
$query = "select * from student where room_id = 011";
$result = $this->db->query($query);
$data = $this->dbutil->csv_from_result($result, $delimiter, $newline);
force_download($filename, $data);
}
我想生成 excel 文件,将数据从我的 table 导出到它。
.xls 和 .csv 中的输出
csv 输出为:
请帮忙。谢谢
要设置 codeigniter 以下载 CSV 格式,请使用以下代码。
function _push_file($path, $name)
{
// make sure it's a file before doing anything!
if(is_file($path))
{
// required for IE
if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
// get the file mime type using the file extension
$this->load->helper('file');
$mime = get_mime_by_extension($path);
// Build the headers to push out the file properly.
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($path)).' GMT');
header('Cache-Control: private',false);
header('Content-Type: '.$mime); // Add the mime type from Code igniter.
header('Content-Disposition: attachment; filename="'.basename($name).'"'); // Add the file name
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($path)); // provide file size
header('Connection: close');
readfile($path); // push it out
exit();
}
复制自:Codeigniter Force download files
还有来自:Download csv from codeigniter mysql
我不知道我是否理解正确,但如果您的问题是您希望 Excel 将 CSV 文件识别为 UTF-8 编码,则必须添加UTF-8 BOM 到您的文件。只需在 CSV 输出前加上
chr(239) . chr(187) . chr(191) // xEF xBB xBF
例如:
$data = chr(239) . chr(187) . chr(191) . $this->dbutil->csv_from_result($result, $delimiter, $newline);
如果您的问题与强制下载有关,也许
将你的force_download从
force_download($filename, $data);
为此:
force_download($filename, "\xEF\xBB\xBF" . $data);
这使得输出为 UTF-8 + BOM,因此它被 MS 识别 Excel