我让管理员通过将.xlsx
.xls
或.csv
个文件上传到HTML表单来更新网站。问题是第二个工作表NORTH
未使用第一个工作表SOUTH
保存到服务器中。
我的代码:
<?php
require('./Classes/PHPExcel/IOFactory.php');
ini_set('max_execution_time', 800);
ini_set('memory_limit', 200M);
$inputFileType = 'Excel2007';
$inputFileName = $_FILES['uploaded']['tmp_name'];
class MyReadFilter implements PHPExcel_Reader_IReadFilter {
public function __construct($fromColumn, $toColumn) {
$this->columns = array();
$toColumn++;
while ($fromColumn !== $toColumn) {
$this->columns[] = $fromColumn++;
}
}
public function readCell($column, $row, $worksheetName = '') {
// Read columns from 'A' to 'AF'
if (in_array($column, $this->columns)) {
return true;
}
return false;
}
}
$filterSubset = new MyReadFilter('A', 'AF');
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadFilter($filterSubset);
$objReader->setLoadSheetsOnly( array("SOUTH", "NORTH") );
$objPHPExcelReader = $objReader->load($inputFileName);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');
$objWriter->save('abc.csv');
$files = fopen('abc.csv', 'r');
while (($line = fgetcsv($files)) !== FALSE) {
$csv_array[] = array_combine(range(1, count($line)), array_values($line));
}
?>
我做错了什么不允许我的代码保存两个工作表? TIA!
答案 0 :(得分:1)
CSV文件只能包含一个工作表,因此您需要将每个工作表保存到单独的文件中
$objReader->setLoadSheetsOnly( array("SOUTH", "NORTH") );
$objPHPExcelReader = $objReader->load($inputFileName);
for ($ws = 0; $ws < $objPHPExcelReader->getSheetCount(); $ws++) {
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');
$objWriter->setSheetIndex($ws);
$objWriter->save('abc' .$ws. '.csv');
}