0

我正在研究phpexcel。我正在处理的文件来自 tally export file。我必须阅读这个文件并将数据保存给每个用户。

例如,从此文件中,我只需要 A2:G10 作为 HTML/TABLE。所以我可以单独向特定成员(Adv. Chandra Mogan)展示。

我只需要一张表格来显示表格的一部分

到目前为止我做了什么:

protected function doExcelUpdate() {
    $inputFileName = $this->getParameter('temp_directory') . '/file.xls';
    if (!file_exists($inputFileName)) {
        $this->addFlash('sonata_flash_error', 'File: not found in temp directory');
        return;
    }

    $this->addFlash('sonata_flash_info', 'File: exist');
    try {
        $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcel = $objReader->load($inputFileName);
    } catch (Exception $e) {
        $this->addFlash('sonata_flash_error', 'Error in PHPExcel');
        return;
    }

    $sheet = $objPHPExcel->getSheet(0);
    if (!$sheet) {
        $this->addFlash('sonata_flash_error', 'Error in reading sheet');
        return;
    }

    $objPHPExcel->getSheet(0)
        ->getStyle('A1:G10')
        ->getProtection()
        ->setLocked(
            PHPExcel_Style_Protection::PROTECTION_PROTECTED
        );

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
    $objWriter->setSheetIndex(0);
    $objWriter->save($this->getParameter('temp_directory') . '/output.html');        
}

A1:G10 未锁定。打印整张纸。

4

3 回答 3

0

第一点:“锁定”不会改变图纸大小,也不会设置“查看窗口”;它可以保护工作表的某些部分不被编辑。

第二点:“锁定”是 Excel 的一项功能,Excel 编写器支持,但 HTML 不支持。

第三点:没有直接的机制来只写工作表的一部分。


作为建议,您可以创建一个新的空白工作表,然后将数据/样式信息从主工作表中所需的单元格区域复制到从单元格 A1 开始的新工作表;然后将该工作表发送到 HTML Writer。

于 2018-01-17T09:14:24.983 回答
0

当您使用 HTML编写器时,您无法使用locked或单元格来实现此目的。hidden您可以使用解决方法创建新工作表并在添加要显示的部分之后。

对于新工作表上的维护样式(如字体、颜色、边框),您必须从原始工作表中为每个单元格提取它并应用于复制的单元格。旧工作表中的合并单元格也是如此。你的功能doExcelUpdate()应该是这样的:

protected function doExcelUpdate()
{
    $inputFileName = $this->getParameter('temp_directory').'/file.xls';
    if (!file_exists($inputFileName)) {
        $this->addFlash('sonata_flash_error', 'File: not found in temp directory');

        return;
    }

    $this->addFlash('sonata_flash_info', 'File: exist');
    try {
        $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $originalPHPExcel = $objReader->load($inputFileName);
    } catch (Exception $e) {
        $this->addFlash('sonata_flash_error', 'Error in PHPExcel');

        return;
    }

    $originalSheet = $originalPHPExcel->getSheet(0);
    if (!$sheet) {
        $this->addFlash('sonata_flash_error', 'Error in reading sheet');

        return;
    }

    // Get the data of portion you want to output
    $data = $originalSheet->rangeToArray('A1:G11');

    $newPHPExcel = new PHPExcel;
    $newPHPExcel->setActiveSheetIndex(0);
    $newSheet = $newPHPExcel->getActiveSheet();

    $newSheet->fromArray($data);

    // Duplicate style for each cell from original sheet to the new sheet
    for ($i = 1; $i < 11; $i++) {
        for ($j = 0; $j <= 6; $j++) {
            $style = $originalSheet->getStyleByColumnAndRow($j, $i);
            $newSheet->duplicateStyle($style, PHPExcel_Cell::stringFromColumnIndex($j).(string)$i);
        }
    }

    // Merge the same cells that are merged in the original sheet
    foreach ($originalSheet->getMergeCells() as $cells) {
        $inRange = false;
        foreach (explode(':', $cells) as $cell) {
            $inRange = $originalSheet->getCell($cell)->isInRange('A1:G11');
        }

        // Merge only if in range of the portion of file you want to output
        if ($inRange) {
            $newSheet->mergeCells($cells);
        }
    }

    $objWriter = PHPExcel_IOFactory::createWriter($newPHPExcel, 'HTML');
    $objWriter->save($this->getParameter('temp_directory').'/output.html');
}
于 2018-01-17T12:31:04.610 回答
0

到现在为止,为了完成这项工作,我已经这样做了,

private function doExcelUpdate() {
    $inputFileName = $this->getParameter('temp_directory') . '/file.xls';
    $synopsis = PHPExcel_IOFactory::load($inputFileName)->getSheet(0);
    $column = $synopsis->getHighestColumn();
    $row = $synopsis->getHighestRow();
    $this->cleanUserPayment();
    $this->doExcelUpdateTable($synopsis, $column, $row);
    $this->deleteExcelFile();
}

private function cleanUserPayment() {
    $em = $this->getDoctrine()->getManager();
    $classMetaData = $em->getClassMetadata('AppBundle\Entity\UserPayment');
    $connection = $em->getConnection();
    $dbPlatform = $connection->getDatabasePlatform();
    $connection->beginTransaction();
    try {
        $connection->query('SET FOREIGN_KEY_CHECKS=0');
        $q = $dbPlatform->getTruncateTableSql($classMetaData->getTableName());
        $connection->executeUpdate($q);
        $connection->query('SET FOREIGN_KEY_CHECKS=1');
        $connection->commit();
    } catch (\Exception $e) {
        $connection->rollback();
    }
}

private function doExcelUpdateTable($synopsis, $column, $row) {
    set_time_limit(300);
    $t = [];
    for ($r = 1; $r <= $row; $r++) {
        for ($c = "A"; $c <= $column; $c++) {
            $cell = $synopsis->getCell($c . $r)->getFormattedValue();
            if ($cell == 'Ledger:') {
                $t[] = $r;
            }
        }
    }
    $t[] = $row+1;
    $numItems = count($t);
    $i = 0;
    $em = $this->getDoctrine()->getManager();
    foreach ($t as $key => $value) {
        if (++$i != $numItems) {
            $up = new UserPayment();
            $up->setName($synopsis->getCell('B' . $value)->getFormattedValue());
            $up->setMessage($this->doExcelUpdateTableCreate($synopsis, $column, $value, $t[$key + 1]));
            $em->persist($up);
             // $this->addFlash('sonata_flash_error', 'Output: ' . $synopsis->getCell('B' . $value)->getFormattedValue() . $this->doExcelUpdateTableCreate($synopsis, $column, $value, $t[$key + 1]));
        }
    }
    $em->flush();
    $this->addFlash('sonata_flash_success', "Successfully updated user bills. Total data updated::" . count($t));
}

private function doExcelUpdateTableCreate($synopsis, $column, $rowS, $rowE) {
    $mr = NULL;
    $x = 0;
    $alphas = range('A', $column);
    $oneTable = '<table border="1">';
    for ($r = $rowS; $r < $rowE; $r++) {
        $oneTable .= "<tr>";
        for ($c = "A"; $c <= $column; $c++) {
            if ($x > 0) {
                $x--;
                continue;
            }
            $mr = NULL;
            $x = 0;
            $cell = $synopsis->getCell($c . $r);
            $cellVal = $cell->getFormattedValue();
            if ($cellVal == NULL) {
                $cellVal = "&nbsp;";
            }
            $cellRange = $cell->getMergeRange();
            if ($cellRange) {
                $mr = substr($cellRange, strpos($cellRange, ":") + 1, 1);
                $upto = array_search($mr, $alphas);
                $x = ($upto - array_search($c, $alphas));
                $oneTable .= "<td colspan=" . ($x + 1) . " style='text-align:right;'>" . $cellVal . "</td>";
            } else {
                $oneTable .= "<td>" . $cellVal . "</td>";
            }
        }
        $oneTable .= "</tr>";
    }
    $oneTable .= "</table>";
    return $oneTable;
}
private function deleteExcelFile() {
    $filesystem = new \Symfony\Component\Filesystem\Filesystem();
    $filesystem->remove($this->getParameter('temp_directory') . '/file.xls');
}
于 2018-01-18T16:53:25.940 回答