用PHP读写XLSM文件

时间:2019-11-27 07:45:00

标签: php phpspreadsheet xlsm

我想使用PHP读写 XLSM 类型的文件。我尝试为此使用PHPSpreadsheet,但它不支持XLSM扩展。

一种可能的解决方案是使用 EasyXLS https://www.easyxls.com/manual/basics/import-from-xlsx-file-format.html

// Create an instance of the class that imports XLSM files
$workbook = new COM("EasyXLS.ExcelDocument");

// Import XLSM file
$workbook->easy_LoadXLSXFile("C:\\Samples\\Excel to import.xlsm");

// Get the table of the second worksheet
$xlsSecondTable = $workbook->easy_getSheet("Second tab")->easy_getExcelTable();

// Add more data to the second sheet
$xlsSecondTable->easy_getCell_2("A1")->setValue("Data added by Tutorial37");
for ($column=0; $column<5; $column++)
{
    $xlsSecondTable->easy_getCell(1, $column)->setValue("Data " . ($column + 1));
}

// Generate the XLSM file
$workbook->easy_WriteXLSXFile("C:\Samples\Excel with macro.xlsm");

但是我无法为此找到任何库。

有人对此有其他解决方案吗?

1 个答案:

答案 0 :(得分:0)

function excel($excelfile, $sheet = false){//from stores.blade.php
    $tempfile                   = resource_path("uploads/excel.xlsm");//it just needs a place to store the XML file temporarily, this function only works in Laravel, replace with a filename
    if($sheet){//load XML file
        //$XML = file_get_contents($tempfile);
        $XML                    = simplexml_load_file($excelfile);
        $XML                    = json_decode(json_encode((array)$XML), TRUE);
        $excelfile              = pathinfo($sheet)['basename'];
        if($excelfile == "workbook.xml"){
            $RET = [];
            foreach($XML["sheets"]["sheet"] as $data){
                $RET[ $data["@attributes"]["sheetId"] ] = $data["@attributes"]["name"];
            }
            return [
                'Filename'      => $excelfile,
                'SheetName'     => "sys_workbook",
                'SheetData'     => $RET,
            ];
        } else if($excelfile == "sharedStrings.xml"){
            foreach($XML["si"] as $index => $value){
                if(isset($value["t"])){
                    $value      = $value["t"];
                } else {
                    foreach($value["r"] as $index2 => $value2){
                        if(is_array($value2["t"])){
                            $value2["t"] = $value2["t"][0];
                        }
                        $value["r"][$index2] = $value2["t"];
                    }
                    $value = implode("", $value["r"]);
                }
                if(is_array($value)){
                    $value = $value[0];
                }
                $XML["si"][$index] = $value;
            }
            return [
                'Filename'      => $excelfile,
                'SheetName'     => "sys_strings",
                'SheetData'     => $XML["si"],
            ];
        } else if(isset($XML["sheetPr"])){
            return [
                'Filename'          => $excelfile,
                'SheetName'         => $XML["sheetPr"]["@attributes"]["codeName"],
                'SheetData'         => $XML["sheetData"]["row"],
            ];
        }
        return false;
    } else {//load ZIPped XLSM file
        $files                  = [];
        $zip                    = new ZipArchive;
        if ($zip->open($excelfile) === TRUE) {
            for($i = 0; $i < $zip->numFiles; $i++) {
                $filename       = $zip->getNameIndex($i);
                if(startswith($filename, "xl/worksheets/") || $filename == "xl/workbook.xml" || $filename = "xl/sharedStrings.xml"){
                    copy("zip://" . $excelfile . "#" . $filename, $tempfile);
                    $XML        = excel($tempfile, $filename);
                    if($XML !== false){
                        $files[ $XML["SheetName"] ] = $XML["SheetData"];
                    }
                }
            }
            @unlink($tempfile);
            $zip->close();
        }
        var_dump($files);
        die();
    }
}

我开始从事这项工作,到现在为止。问题是,我不知道如何引用共享字符串(sys_strings),并且您需要一个方程式计算器来处理这些函数。我希望工作表是按顺序排列的,因此Sheet1成为sys_workbook中的第一个数组值。