PHPspreadsheet和JS:如何支持阿拉伯语?

问题描述 投票:-1回答:1

我正在从数据库中获取数据,有些字符串是英语,有些是阿拉伯语,有些是阿拉伯语一半英语。

我正在像这样生成电子表格.xlsx

function writeToFile($spreadsheet, $filename)
{
    header("Content-Encoding: UTF-8");
    header('Content-Type: application/vnd.ms-excel; charset=UTF-8"');
    header('Content-Disposition: attachment; filename=' . $filename);
    $writer = new Xlsx($spreadsheet);

    $writer->save("php://output");
}

但是我没有直接下载它,这会生成一个Blob,可以使用javascript下载。

fetch(url)
    .then(resp => resp.blob())
    .then(file => {
        if (file.size > 0) {
            const url = window.URL.createObjectURL(file);
            const a = document.createElement('a');
            a.style.display = 'none';
            a.href = url;
            a.download = filename;
            document.body.appendChild(a);
            a.click();
            window.URL.revokeObjectURL(url);
        } else {
             console.log('error');
        }
    })
    .catch(() => console.log('error'));

问题出在PHPspreadsheet上,我不知道是否解决了这个问题,我还会遇到另一个与JS有关的问题,但是我确实知道,如果不使用JS生成文件,我仍然会遇到阿拉伯问题。 。

php phpexcel phpspreadsheet
1个回答
0
投票

this answer,结果我需要添加

array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'")

到PDO连接,因此变成这样

 try {
        $db = new PDO("mysql:host=$server;dbname=$database", $user, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
        // set the PDO error mode to exception
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
    }
    return $db;
}
© www.soinside.com 2019 - 2024. All rights reserved.