我使用PHPExcel使用PHP将一组图像写入Excel文件。 插入图像似乎不是问题,但是我在设置适当的行高以匹配图像高度方面遇到了一些困难。
我设法使用pixelsToCellDimension来计算正确的列宽,但这似乎不适合计算行高。 在查看了PHPExcel_Shared_Drawing类中的其余函数并使用Google搜索后,我对于如何执行此操作感到茫然。
到目前为止我的代码:
// 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();
[生成我的图像并将其路径添加到数组的一些代码]
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;
答案 0 :(得分:2)
我使用pixelToPoints作为高度
$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);
版本1.8.0
答案 1 :(得分:0)
如果您不能使用 Drawing::pixelToPoint()
方法,请使用以下计算:
$image_height * 0.75
这是将像素转换为点的公式(使用pixelToPoint
方法)
答案 2 :(得分:-1)
我遇到了同样的问题。如果可能,请包含文字" \ n"在图片单元格中。例如,添加到单元格5次" \ n" => " \ n \ n \ n \ n \ n"对于100像素的图像高度尺寸。
$objPHPExcel->getActiveSheet()->setCellValue($colLetter.$idLine, "\n\n\n\n\n");
但它不适用于合并单元格
我希望它能帮到你