php PhpExcel 创建多个工作表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19213640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PhpExcel creates multiple worksheets
提问by Anar Bayramov
hello I am trying to create an excel template by using phpexcel
你好,我正在尝试使用 phpexcel 创建一个 excel 模板
For some reason image creates new worksheet instead of using current one. so when I open excel file I've created there are worksheetand worksheet1instead of single one.
出于某种原因,图像创建新的工作表而不是使用当前的工作表。所以当我打开我创建的 excel 文件时,有 工作表和工作表 1而不是单个。
objPHPExcel = new PHPExcel();
$objWorkSheet = $objPHPExcel->createSheet();
// Set the active Excel worksheet to sheet 0
$objPHPExcel->setActiveSheetIndex(0);
//Taslak Verileri
$objPHPExcel->getActiveSheet()->SetCellValue('D'.'1', 'Firm');
$objPHPExcel->getActiveSheet()->SetCellValue('J'.'1', 'SFUFORMU - FR.PS.21');
$objPHPExcel->getActiveSheet()->SetCellValue('J'.'3', 'NO:');
$objPHPExcel->getActiveSheet()->SetCellValue('D'.'2', 'Name Surname Signature');
$objPHPExcel->getActiveSheet()->SetCellValue('A'.'4', 'Date');
$objPHPExcel->getActiveSheet()->SetCellValue('A'.'5', 'Stock No:');
$objPHPExcel->getActiveSheet()->SetCellValue('C'.'5', 'Image');
$objPHPExcel->getActiveSheet()->SetCellValue('E'.'5', 'Image');
$objPHPExcel->getActiveSheet()->SetCellValue('G'.'5', 'Resim');
$objPHPExcel->getActiveSheet()->SetCellValue('I'.'5', 'Image');
$objPHPExcel->getActiveSheet()->SetCellValue('K'.'5', 'Quantity');
$objPHPExcel->getActiveSheet()->SetCellValue('M'.'5', 'Price');
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setWorksheet($objWorkSheet);
$objDrawing->setName("name");
$objDrawing->setDescription("Description");
$objDrawing->setPath('temp/3.jpeg');
$objDrawing->setCoordinates('F9');
$objDrawing->setOffsetX(1);
$objDrawing->setOffsetY(5);
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('some_excel_file.xlsx');




回答by vikingmaster
You basically create a PHPExcelobject which has already en empty sheet with index 0.
您基本上创建了一个PHPExcel已经带有 index 的空表的对象0。
Then you create a new sheet with index 1.
然后您创建一个带有 index 的新工作表1。
Then you write all your stuff to sheet with index 0and add the picture on second sheet (newly created).
然后,您将所有内容写入带有索引的工作表,0并将图片添加到第二张工作表(新创建的)上。
This should solve your problem:
这应该可以解决您的问题:
$objPHPExcel->setActiveSheetIndex(1);
Note, that you still create a new sheet, even the first one already exists. If you want to use already existing worksheet, just do the following:
请注意,您仍然会创建一个新工作表,即使第一个工作表已经存在。如果您想使用已经存在的工作表,只需执行以下操作:
Remove:
消除:
$objWorkSheet = $objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(0);
And do all the stuff with already existing sheet.
并使用现有的工作表完成所有工作。
$sheet = $objPHPExcel->getSheet(0);
$sheet->setCellValue('D'.'1', 'Firm')//Etc all the stuff.
Give the drawing the same sheet:
给图纸同一张纸:
$objDrawing->setWorksheet($sheet);
回答by Adrián Pulido del Castillo
I don't know how rules phpexcelbut in your context appears such you were creating two worksheets with this method cause you are calling two times...
我不知道phpexcel 的规则如何,但在您的上下文中出现这样您正在使用此方法创建两个工作表,因为您要调用两次...
$objWorkSheet = $objPHPExcel->createSheet();
$objDrawing->setWorksheet($objWorkSheet);
回答by senthilkumar
write 2 sheet a single excel workbook using PHPEXCEL
使用 PHPEXCEL 将 2 个工作表写入单个 Excel 工作簿
for ($i=0; $i <2 ; $i++) {
$objPHPExcel->getActiveSheet()->setTitle('Worksheet'); //sheetname
$newsheet = $objPHPExcel->createSheet(); //sheet create
}

