PHPExcel: setting line height in pixels

I am using PHPExcel to write a set of images to an Excel file using PHP. Inserting images does not seem to be a problem, however, I am having difficulty setting the appropriate line height according to the image height.

I managed to use pixelsToCellDimension to calculate the correct column width, but this does not seem to be suitable for calculating the row height. After checking the rest of the functions of the PHPExcel_Shared_Drawing and Googling class, I was stunned how to do this.

My code is:

// Create new PHPExcel object $objPHPExcel = new PHPExcel(); // Set document properties $objPHPExcel->getProperties()->setCreator("creator") ->setLastModifiedBy("modifiedby") ->setTitle("Title") ->setSubject("Subject") ->setDescription("Description") ->setKeywords("keyword1 keyword2") ->setCategory("category"); // Get default font $defaultFont = $objPHPExcel->getDefaultStyle()->getFont(); 

[some code to create my images and add their paths to the array]

 foreach( $filePaths as $i => $filePath ) { $objDrawing = new PHPExcel_Worksheet_Drawing(); $objDrawing->setWorkSheet( $objPHPExcel->getActiveSheet() ); $objDrawing->setName("name"); $objDrawing->setDescription("Description"); $objDrawing->setPath( $filePath ); $size = getimagesize($filePath); $columnWidth = PHPExcel_Shared_Drawing::pixelsToCellDimension($size[0], $defaultFont); $rowHeight = PHPExcel_Shared_Drawing::pixelsToCellDimension($size[1], $defaultFont); $objDrawing->setCoordinates('A' . ($i+1) ); $objDrawing->setOffsetX(0); $objDrawing->setOffsetY(0); // set row/column (cell) sizes to match image size $objPHPExcel->getActiveSheet() ->getColumnDimension('A') ->setWidth( $columnWidth ); $objPHPExcel->getActiveSheet() ->getRowDimension( $i+1 ) ->setRowHeight( $rowHeight ); } // Rename worksheet $objPHPExcel->getActiveSheet()->setTitle('Worksheet 1'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // Redirect output to a client's web browser (Excel5) header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="myMCS Scancode Export.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit; 
+6
source share
2 answers

I used pixels toPoints for height

 $default_font = $spreadsheet->getDefaultStyle()->getFont(); $image_height = imageSY($image); $image_width = imageSX($image); $image_height_pt = Drawing::pixelsToPoints($image_height); $image_width_pt = Drawing::pixelsToCellDimension($image_width,$default_font); $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing(); $drawing->setImageResource($image); $drawing->setRenderingFunction(\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::RENDERING_JPEG); $drawing->setMimeType(\PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::MIMETYPE_DEFAULT); $drawing->setCoordinates($col.$row); $drawing->setHeight($image_height); $drawing->setWorksheet($spreadsheet->getActiveSheet()); $spreadsheet->getActiveSheet()->getRowDimension($row)->setRowHeight($image_height_pt); $spreadsheet->getActiveSheet()->getColumnDimension($col)->setWidth($image_width_pt); 

Version 1.8.0

+1
source

I had the same problem. If possible, specify the text "\ n" in the image cell. For example, add "\ n" => "\ n \ n \ n \ n \ n" to the cell 5 times for an image height of 100px.

 $objPHPExcel->getActiveSheet()->setCellValue($colLetter.$idLine, "\n\n\n\n\n"); 

But it does not work with cell merging

I hope this helps you

-1
source

Source: https://habr.com/ru/post/981267/


All Articles