如何使用PHP将*.xlsb文件转换为数组或*.csv文件

3
我正在尝试将 *.xlsb 文件转换为 php 数组或 *.csv 文件(或至少是 *.xls)。我尝试使用 PHPExcel,但似乎它无法识别文件内部的内容。
我注意到,你可以将 *.xlsb 文件重命名为 *.zip 文件,然后使用命令行 unzip *.zip 进行解压缩。之后,您将获得带有 sheet1.bin 文件的下一个文件夹:

enter image description here

看起来这个文件应该包含Excel单元格值,但我仍然无法使用PHPExcel解析它。有人能帮我吗?是否有可能解析*.xlsb文件并从中获取信息?或者也许可以解析这个sheet1.bin文件?
1个回答

6

PHPExcel不支持XLSB文件。 XLSB文件格式是一个压缩档案,与XLSX相同,但大多数存储在压缩档案中的文件都是二进制文件。这些二进制文件很难解析。

一个支持XLSB文件的PHP Excel库是EasyXLS。您可以从这里下载该库。

将XLSB文件转换为PHP数组

//Code for reading xlsb file
$workbook = new COM("EasyXLS.ExcelDocument");
$workbook->easy_LoadXLSBFile("file.xlsb");

//Code for building the php array
$xlsTable = $workbook->easy_getSheetAt(0)->easy_getExcelTable();
for ($row=0; $row<$xlsTable->RowCount(); $row++)
{
   for ($column=0; $column<$xlsTable->ColumnCount(); $column++)
   {
       $value = $xlsTable->easy_getCell($row, $column)->getValue();
       //transfer $value into your array
   }
}

或者

//Code for reading xlsb file    
$workbook = new COM("EasyXLS.ExcelDocument");
$rows = $workbook->easy_ReadXLSBActiveSheet_AsList("file.xlsb");

//Code for building the php array
for ($rowIndex=0; $rowIndex<$rows->size(); $rowIndex++)
    {
        $row = $rows->elementAt($rowIndex);
        for ($cellIndex=0; $cellIndex<$row->size(); $cellIndex++)
        {
            $value = $row->elementAt($cellIndex);
            //transfer $value into your array
        }
    }

将XLSB转换为CSV

//Code for reading xlsb file
$workbook = new COM("EasyXLS.ExcelDocument");
$workbook->easy_LoadXLSBFile("file.xlsb");

//Code for converting to CSV
$workbook->easy_WriteCSVFile("file.csv", $workbook->easy_getSheetAt(0)->getSheetName());

将XLSB文件转换为XLS

//Code for reading xlsb file
$workbook = new COM("EasyXLS.ExcelDocument");
$workbook->easy_LoadXLSBFile("file.xlsb");

//Code for converting to XLS
$workbook->easy_WriteXLSFile("file.xls");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接