如何使用PHPExcel打开Excel文件以进行读写? [英] How to open an Excel file with PHPExcel for both reading and writing?

查看:78
本文介绍了如何使用PHPExcel打开Excel文件以进行读写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHPExcel库,并且正在创建xls对象以进行写入或读取:

I'm using the PHPExcel library, and I'm creating xls objects either for writing or for reading:

PHPExcel_IOFactory::createReaderForFile('file.xlsx')
PHPExcel_IOFactory::createWriter('Excel2007')

如何打开XLSX文件进行读写?

How can I open an XLSX file for reading and writing?

推荐答案

您可以使用读取器和load()方法将文件加载到PHPExcel中,然后使用写入器和save()方法将文件保存到PHPExcel中...但是PHPExcel本身并不知道PHPExcel对象的来源...它不在乎是从文件(或哪种类型的文件)中加载它,还是手动创建它.

You load a file into PHPExcel using a reader and the load() method, then save that file using a writer and the save() method... but PHPExcel itself is unaware of the source of the PHPExcel object... it doesn't care whether you have loaded it from a file (or what type of file) or created it by hand.

因此,没有开放读/写"的概念.您只需按名称读取文件,然后保存为相同的文件名即可.这样,您在脚本中所做的任何更改都将覆盖原始文件.

As such, there is no concept of "opening for read/write". You simply read the file by name, and save to the same filename. That will overwrite the original file with any changes that you have made in your script.

编辑

示例

error_reporting(E_ALL);
set_time_limit(0);

date_default_timezone_set('Europe/London');
set_include_path(get_include_path() . PATH_SEPARATOR . './Classes/');

include 'PHPExcel/IOFactory.php';

$fileType = 'Excel5';
$fileName = 'testFile.xls';

// Read the file
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($fileName);

// Change the file
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Hello')
            ->setCellValue('B1', 'World!');

// Write the file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->save($fileName);

我是否可以建议您阅读文档,并查看/Tests中的示例代码

And can I suggest that you read the documentation, and look at the sample code in /Tests

这篇关于如何使用PHPExcel打开Excel文件以进行读写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆