我正在使用 phpexcel 创建一些工作表,一切正常,但是在我创建的所有工作表之后创建了一个额外的空工作表,我不知道它为什么会发生,也不知道如何阻止它。你知道为什么会这样吗?
为了生成我的电子表格,我有两个静态数组来存储工作表索引和名称,如下所示:
//List of Enter Data sheets.
const IRRIGATION_SHEET_INDEX = 0;
const CANOPY_SHEET_INDEX = 1;
const CLUSTER_SHEET_INDEX = 2;
const OBSERVATIONS_SHEET_INDEX = 3;
const WATERPOTENTIAL_SHEET_INDEX = 4;
const FRUITMATURITY_SHEET_INDEX = 5;
//list of hidden sheets created only to fill data validation lists in the bulk_upload_sheet
const BLOCKS_LIST_SHEET_INDEX = 6;
const RANCHES_LIST_SHEET_INDEX = 7;
const SUB_BLOCKS_LIST_SHEET_INDEX = 8;
/**
* This is a static array to store the indexes and titles of each usable sheet (by usable sheet, it means that these are the sheets the user can see)
* @var array
*/
public static $usableSheetTitles = array(
//SELF::BULK_UPLOAD_SHEET_INDEX => 'BulkUploadTemplate',
SELF::IRRIGATION_SHEET_INDEX => 'Irrigation',
SELF::CANOPY_SHEET_INDEX => 'Canopy',
SELF::CLUSTER_SHEET_INDEX => 'Cluster',
SELF::OBSERVATIONS_SHEET_INDEX => 'Observations',
SELF::WATERPOTENTIAL_SHEET_INDEX => 'WaterPotential',
SELF::FRUITMATURITY_SHEET_INDEX => 'FruitMaturity'
);
public static $auxiliarSheets = array (
SELF::BLOCKS_LIST_SHEET_INDEX => 'BlocksList',
SELF::RANCHES_LIST_SHEET_INDEX => 'RanchesList',
SELF::SUB_BLOCKS_LIST_SHEET_INDEX => 'SubBlocksList'
);
然后我调用createSheets()
读取这些数组并生成我需要的工作表的方法:
public static function createSheets($objPHPExcel, $ranches, $subBlocks) {
count($ranches) <= 1 ? $hasSeveralRanches = false : $hasSeveralRanches = true;
count($subBlocks) <= 0 ? $hasSubBlocks = false : $hasSubBlocks = true;
foreach (self::$usableSheetTitles as $sheetIndex => $sheetTitle) {
$objPHPExcel->createSheet($sheetIndex);
$objPHPExcel->setActiveSheetIndex($sheetIndex);
$objPHPExcel->getActiveSheet()->setTitle($sheetTitle);
self::insertTitleAndLegend($objPHPExcel, $sheetTitle, $hasSeveralRanches, $hasSubBlocks);
}
foreach (self::$auxiliarSheets as $sheetIndex => $sheetTitle) {
$objPHPExcel->createSheet($sheetIndex);
$objPHPExcel->setActiveSheetIndex($sheetIndex);
$objPHPExcel->getActiveSheet()->setTitle($sheetTitle);
}
return $objPHPExcel;
}
你知道我在 xls 生成的模板中可能会生成这张最后一张空纸吗?这是正常行为还是我的代码中缺少我没有看到的东西?