PHPExcel:设置行高度(以像素为单位)

时间:2022-08-24 07:35:17

I'm using PHPExcel to write a set of images to an Excel file using PHP. Inserting the images doesn't seem to be a problem, however I'm having some difficulties setting the appropriate row height to match the image height.

我使用PHPExcel用PHP将一组图像写到Excel文件中。插入图像似乎不是一个问题,但是我在设置适当的行高度以匹配图像高度方面有困难。

I managed to use pixelsToCellDimension to calculate the correct column width, but this doesn't seem to be appropriate for calculating row height. After checking out the rest of the functions in the PHPExcel_Shared_Drawing class and Googling around, I'm left dumbfounded as to how to do this.

我设法使用pixelsToCellDimension来计算正确的列宽度,但是这似乎不适用于计算行高度。在检查了PHPExcel_Shared_Drawing类中的其余函数并在周围搜索之后,我对如何执行这一操作感到惊讶。

My code so far:

到目前为止我的代码:

// 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 generate my images and add their paths to an 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;

2 个解决方案

#1


1  

I used pixelsToPoints for the 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.8.0

#2


-1  

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

我也有同样的问题。如果可能的话,在图片单元格中包含文本“\n”。例如,向单元格中添加5倍的“\n”=> \n\n\n\n\n \n\n\n\n”以获得100像素的图像高度。

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

But it doesn't works with merges cells

但是它不能与合并单元格一起工作

I hope it will help you

我希望它能对你有所帮助

#1


1  

I used pixelsToPoints for the 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.8.0

#2


-1  

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

我也有同样的问题。如果可能的话,在图片单元格中包含文本“\n”。例如,向单元格中添加5倍的“\n”=> \n\n\n\n\n \n\n\n\n”以获得100像素的图像高度。

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

But it doesn't works with merges cells

但是它不能与合并单元格一起工作

I hope it will help you

我希望它能对你有所帮助