最迅速的是渲染陣列,以Excel 2007中,只是在臺式機上安裝谷歌雲端硬盤應用訪問數據。 Spreadsheets API無法創建文檔,需要使用Docs API來完成;目前PHP的API封裝不支持這兩個端點 - 就像我可以修改這些文件一樣。及時的努力:1小時。
更新:當我使用Excel2007渲染XLSX時,轉換失敗。當我用MS-Excel和「另存爲」打開同一個文件時 - 即使在Drive API上,我也可以將其轉換。
我注意到解壓文件時,相比一些事情:
一)MS-Excel使用值 '0/1',而不是 '假/真'
二)目錄「XL /工作表/ _rels'被刪除了(可能沒用?)。
c)XML的行尾從UNIX轉換到DOS。
d)MS-Excel將所有空白單元格添加到XML-- PHPExcel沒有寫出。
e)關係ID似乎被移位......在代碼中有+3的計算?
問題是生成的文件格式與Drive API的XML解析器不匹配。
我還不能完全確定什麼是錯的,生成的文件格式, 但修補呈現XLSX與MS-Excel的本地創建的XML文件的文件使得它的工作:
/* how-to patch the generated XLSX: */
$zip = new ZipArchive();
$zip->open($xlsx_path);
$zip->extractTo($export_dir.'temp');
$zip->close();
$this->deleteDir($export_dir.'temp/xl/worksheets/_rels');
$files =array(
'_rels/.rels',
'docProps/app.xml',
'docProps/core.xml',
'xl/workbook.xml',
'xl/_rels/workbook.xml.rels',
'xl/theme/theme1.xml'
);
foreach($files as $file){
copy($export_dir.'template/'.$file, $export_dir.'temp/'.$file);
}
unlink($xlsx_path);
if($zip->open($xlsx_path, ZIPARCHIVE::CREATE)) {
$source = str_replace('\\', '/', realpath($export_dir.'temp'));
if(is_dir($source) === true) {
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$file = str_replace('\\', '/', $file);
if(in_array(substr($file, strrpos($file, '/')+1), array('.', '..'))){continue;}
$file = realpath($file);
if(is_dir($file) === true){$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));}
else if(is_file($file) === true){
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
}
else if(is_file($source) === true) {
$zip->addFromString(basename($source), file_get_contents($source));
}
$zip->close();
}
細胞格式不太適合 - 但轉換&預覽正在工作。
甚至可以從修補中跳過一些XML文件,而其他一些XML文件則會使其中斷。
一些說明: - 1.您是否說過完全相同的文件可以通過Web界面進行上傳/轉換,但通過API失敗50?如果是這樣,請檢查您的Base64編碼是否正確。 2.您可以上傳而無需轉換。如果您嘗試在網絡界面中「轉換爲Google文檔」,會發生什麼情況? – pinoyyid 2012-09-05 02:09:46
如果我通過網頁瀏覽器上傳轉換 - 它的工作原理,轉換已保存在API上的文件不會轉換(進度條卡住);我正在使用PHP的google-api包裝器。 – 2012-09-05 14:51:54