I’m using phpexcel to create some sheets and everything is working fine, but there’s an extra empty sheet being created after all the sheets I create and I don’t know either why it’s happening, or how to stop it. Do you know why is it happening?
To generate my spreadsheet, I have two static arrays storing the sheet indexes and names like this:
//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'
);
Then I call the method createSheets() that reads these arrays and generates the sheets I need:
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;
}
Do you know what could possibly be generating this final empty sheet I have in my xls generated template? Is it a normal behavior or is there something missing in my code that I didn’t see?
This is normal behaviour. When you instantiate a new PHPExcel object, it automatically creates it with a single worksheet (“Worksheet”) because that is what most users are likely to need. The sheets thet you’re creating are being inserted “in front” of it because you’re specifying their index position which is pushing it “down” the series of worksheets.
If you don’t need the automatically created worksheet because you’re creating all your own sheets, then use the PHPExcel object’s removeSheetByIndex() method.