Use PHPExcel read \ modify xls file

Sources of information and the Internet, have not found a specific source

Made a simple to read and modify, not to continue to study the document format, etc., there is a need to see the document PHPExcel

PHPExcel github address

https://github.com/PHPOffice/PHPExcel

<?php
header("content-type:text/html; charset=utf-8");
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
include 'Classes/PHPExcel.php';
include 'Classes/PHPExcel/IOFactory.php';
include_once 'Classes/PHPExcel/Reader/Excel5.php';
include_once 'Classes/PHPExcel/Writer/Excel5.php';

$PHPExcel = new PHPExcel();
$filename = "test.xls";

$PHPReader = new \PHPExcel_Reader_Excel5();
// 载入文件
$PHPExcel = $PHPReader->load($filename);
// 获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推

$currentSheet = $PHPExcel->getSheet(0);
// 获取总列数
$allColumn = $currentSheet->getHighestColumn();
// 获取总行数
$allRow = $currentSheet->getHighestRow();

// 创建写入
$objWriter = new PHPExcel_Writer_Excel5($PHPExcel);
$ii = 0;
// 循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
for ($currentRow = 31; $currentRow <= $allRow; $currentRow ++) {
    // 从哪列开始,A表示第一列
    for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn ++) {
        // 数据坐标
        $address = $currentColumn . $currentRow;
        // 读取到的数据,保存到数组$arr中
        
        $data[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
    }

    //修改表S1的内容
    //根据实际业务中的内容框修改
    $currentSheet->setCellValue("S1", "修改内容");
    $objWriter->save($filename);
}

Such example has .xls files, other versions if excel file requires reading and writing reference different classes

Read Classes / PHPExcel / Reader /

Written in Classes / PHPExcel / Writer /


Published 27 original articles · won praise 53 · views 160 000 +

Guess you like

Origin blog.csdn.net/auspi12341/article/details/76051337